Skip to content

Commit

Permalink
Support jiff types
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Jan 11, 2025
1 parent ae4fe29 commit f67ad00
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ Schemars can implement `JsonSchema` on types from several popular crates, enable
- `chrono04` - [chrono](https://crates.io/crates/chrono) (^0.4)
- `either1` - [either](https://crates.io/crates/either) (^1.3)
- `indexmap2` - [indexmap](https://crates.io/crates/indexmap) (^2.0)
- `jiff01` - [jiff](https://crates.io/crates/jiff) (^0.1)
- `rust_decimal1` - [rust_decimal](https://crates.io/crates/rust_decimal) (^1.0)
- `semver1` - [semver](https://crates.io/crates/semver) (^1.0.9)
- `smallvec1` - [smallvec](https://crates.io/crates/smallvec) (^1.0)
Expand Down
2 changes: 2 additions & 0 deletions schemars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bytes1 = { version = "1.0", default-features = false, optional = true, package =
chrono04 = { version = "0.4", default-features = false, optional = true, package = "chrono" }
either1 = { version = "1.3", default-features = false, optional = true, package = "either" }
indexmap2 = { version = "2.0", default-features = false, optional = true, package = "indexmap" }
jiff01 = { version = "0.1", default-features = false, optional = true, package = "jiff" }
rust_decimal1 = { version = "1", default-features = false, optional = true, package = "rust_decimal" }
semver1 = { version = "1.0.9", default-features = false, optional = true, package = "semver" }
smallvec1 = { version = "1.0", default-features = false, optional = true, package = "smallvec" }
Expand All @@ -51,6 +52,7 @@ bytes1 = { version = "1.0", default-features = false, features = ["serde"], pack
chrono04 = { version = "0.4", default-features = false, features = ["serde"], package = "chrono" }
either1 = { version = "1.3", default-features = false, features = ["serde"], package = "either" }
indexmap2 = { version = "2.0", default-features = false, features = ["serde"], package = "indexmap" }
jiff01 = { version = "0.1", default-features = false, features = ["serde"], package = "jiff" }
rust_decimal1 = { version = "1", default-features = false, features = ["serde"], package = "rust_decimal" }
semver1 = { version = "1.0.9", default-features = false, features = ["serde"], package = "semver" }
smallvec1 = { version = "1.0", default-features = false, features = ["serde"], package = "smallvec" }
Expand Down
37 changes: 37 additions & 0 deletions schemars/src/json_schema_impls/jiff01.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::{json_schema, JsonSchema, Schema, SchemaGenerator};
use alloc::borrow::Cow;
use jiff01::civil::{Date, DateTime, Time};
use jiff01::{SignedDuration, Timestamp, Zoned};

macro_rules! formatted_string_impl {
($ty:ident, $format:literal) => {
formatted_string_impl!($ty, $format, JsonSchema for $ty);
};
($ty:ident, $format:literal, $($desc:tt)+) => {
impl $($desc)+ {
always_inline!();

fn schema_name() -> Cow<'static, str> {
stringify!($ty).into()
}

fn schema_id() -> Cow<'static, str> {
stringify!(jiff::$ty).into()
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
json_schema!({
"type": "string",
"format": $format
})
}
}
};
}

formatted_string_impl!(SignedDuration, "duration");
formatted_string_impl!(Timestamp, "date-time");
formatted_string_impl!(Zoned, "date-time");
formatted_string_impl!(Date, "date");
formatted_string_impl!(Time, "partial-time");
formatted_string_impl!(DateTime, "partial-date-time");
3 changes: 3 additions & 0 deletions schemars/src/json_schema_impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ mod either1;
#[cfg(feature = "indexmap2")]
mod indexmap2;

#[cfg(feature = "jiff01")]
mod jiff01;

#[cfg(feature = "semver1")]
mod semver1;

Expand Down
43 changes: 43 additions & 0 deletions schemars/tests/integration/jiff.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::prelude::*;
use jiff01::civil::{Date, DateTime, Time};
use jiff01::{Timestamp, Zoned};

#[derive(JsonSchema, Serialize, Deserialize)]
struct JiffTypes {
date_time_ts: Timestamp,
date_time_zoned: Zoned,
naive_date: Date,
naive_date_time: DateTime,
naive_time: Time,
}

#[test]
fn jiff() {
test!(JiffTypes).assert_snapshot();

test!(Timestamp)
.assert_allows_ser_roundtrip_default()
.assert_matches_de_roundtrip(arbitrary_values());

// test!(Zoned)
// .assert_allows_ser_roundtrip_default()
// .assert_matches_de_roundtrip(arbitrary_values());

test!(Date)
.assert_allows_ser_roundtrip_default()
.assert_matches_de_roundtrip(arbitrary_values());

test!(DateTime)
.assert_allows_ser_roundtrip_default()
.assert_matches_de_roundtrip(arbitrary_values_except(
Value::is_string,
"Custom format 'partial-date-time', so arbitrary strings technically allowed by schema",
));

test!(Time)
.assert_allows_ser_roundtrip_default()
.assert_matches_de_roundtrip(arbitrary_values_except(
Value::is_string,
"Custom format 'date-time', so arbitrary strings technically allowed by schema",
));
}
2 changes: 2 additions & 0 deletions schemars/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ mod garde;
#[cfg(feature = "indexmap2")]
mod indexmap;
mod inline_subschemas;
#[cfg(feature = "jiff01")]
mod jiff;
mod macros;
mod remote_derive;
mod same_name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "JiffTypes",
"type": "object",
"properties": {
"date_time_ts": {
"type": "string",
"format": "date-time"
},
"date_time_zoned": {
"type": "string",
"format": "date-time"
},
"naive_date": {
"type": "string",
"format": "date"
},
"naive_date_time": {
"type": "string",
"format": "partial-date-time"
},
"naive_time": {
"type": "string",
"format": "partial-time"
}
},
"required": [
"date_time_ts",
"date_time_zoned",
"naive_date",
"naive_date_time",
"naive_time"
]
}

0 comments on commit f67ad00

Please sign in to comment.