diff --git a/Cargo.toml b/Cargo.toml index a33cee6..f06cc0e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ name = "non_empty_string" [dependencies] serde = { version = "1", optional = true } delegate = { version = "0.8" } +schemars = { version = "0.9.0", optional = true } [dev-dependencies] assert_matches = "1.5.0" @@ -28,6 +29,7 @@ serde = { version = "1", features = ["derive"] } default = [] macros = [] serde = ["dep:serde"] +schemars09 = ["dep:schemars"] [lints.rust] diff --git a/src/lib.rs b/src/lib.rs index 8477b8a..e59143b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,6 +19,8 @@ mod serde_support; #[cfg(feature = "macros")] mod macros; +#[cfg(feature = "schemars09")] +mod schemars_support; mod trait_impls; diff --git a/src/schemars_support.rs b/src/schemars_support.rs new file mode 100644 index 0000000..7b617f6 --- /dev/null +++ b/src/schemars_support.rs @@ -0,0 +1,25 @@ +use std::borrow::Cow; + +use schemars::{json_schema, JsonSchema}; + +use crate::NonEmptyString; + +impl JsonSchema for NonEmptyString { + fn schema_name() -> std::borrow::Cow<'static, str> { + "nonemtpy_string".into() + } + fn inline_schema() -> bool { + true + } + fn schema_id() -> Cow<'static, str> { + "nonempty_string".into() + } + fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema { + json_schema!({ + "type": "string", + "minLength": 1, + "title": "Non-Empty String", + "description": "A string that must contain at least one character" + }) + } +}