Skip to content

Commit

Permalink
feat: Add impls for the time crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
jetuk committed Dec 1, 2023
1 parent 979833f commit 675f2af
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 1 deletion.
5 changes: 5 additions & 0 deletions schemars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ bigdecimal04 = { version = "0.4", default-features = false, optional = true, pac
enumset = { version = "1.0", optional = true }
smol_str = { version = "0.1.17", optional = true }
semver = { version = "1.0.9", features = ["serde"], optional = true }
time = { version = "0.3.30", default-features = false, optional = true }

[dev-dependencies]
pretty_assertions = "1.2.1"
Expand Down Expand Up @@ -127,5 +128,9 @@ required-features = ["semver"]
name = "decimal"
required-features = ["rust_decimal", "bigdecimal03", "bigdecimal04"]

[[test]]
name = "time"
required-features = ["time"]

[package.metadata.docs.rs]
all-features = true
2 changes: 2 additions & 0 deletions schemars/src/json_schema_impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ mod smallvec;
#[cfg(feature = "smol_str")]
mod smol_str;
mod std_time;
#[cfg(feature = "time")]
mod time;
mod tuple;
#[cfg(feature = "url")]
mod url;
Expand Down
35 changes: 35 additions & 0 deletions schemars/src/json_schema_impls/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use std::borrow::Cow;
use time::{Date, OffsetDateTime, PrimitiveDateTime, Time};

macro_rules! formatted_string_impl {
($ty:ident, $format:literal) => {
impl JsonSchema for $ty {
no_ref_schema!();

fn schema_name() -> String {
stringify!($ty).to_owned()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed(stringify!(time::$ty))
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::String.into()),
format: Some($format.to_owned()),
..Default::default()
}
.into()
}
}
};
}

formatted_string_impl!(Date, "date");
formatted_string_impl!(PrimitiveDateTime, "partial-date-time");
formatted_string_impl!(Time, "time");
formatted_string_impl!(OffsetDateTime, "date-time");
29 changes: 29 additions & 0 deletions schemars/tests/expected/time-types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "TimeTypes",
"type": "object",
"required": [
"date",
"offset_date_time",
"primitive_date_time",
"time"
],
"properties": {
"date": {
"type": "string",
"format": "date"
},
"time": {
"type": "string",
"format": "time"
},
"primitive_date_time": {
"type": "string",
"format": "partial-date-time"
},
"offset_date_time": {
"type": "string",
"format": "date-time"
}
}
}
18 changes: 18 additions & 0 deletions schemars/tests/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
mod util;
use schemars::JsonSchema;
use time::{Date, OffsetDateTime, PrimitiveDateTime, Time};
use util::*;

#[allow(dead_code)]
#[derive(JsonSchema)]
struct TimeTypes {
date: Date,
time: Time,
primitive_date_time: PrimitiveDateTime,
offset_date_time: OffsetDateTime,
}

#[test]
fn time_types() -> TestResult {
test_default_generated_schema::<TimeTypes>("time-types")
}
2 changes: 1 addition & 1 deletion schemars/tests/validate_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Struct<'a> {
#[schemars(inner(length(min = 5, max = 100)))]
array_str_length: [&'a str; 2],
#[schemars(inner(contains(pattern = "substring...")))]
slice_str_contains: &'a[&'a str],
slice_str_contains: &'a [&'a str],
#[schemars(inner(regex = "STARTS_WITH_HELLO"))]
vec_str_regex: Vec<String>,
#[schemars(inner(length(min = 1, max = 100)))]
Expand Down

0 comments on commit 675f2af

Please sign in to comment.