From d970082d0eafa50bb47fc6c9ea3a284cac941b0e Mon Sep 17 00:00:00 2001 From: Nicole Venner Date: Tue, 12 Aug 2025 13:13:43 -0600 Subject: [PATCH 1/3] Feat: Add Support for schemars --- Cargo.toml | 2 ++ src/lib.rs | 2 ++ src/schemars_support.rs | 25 +++++++++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 src/schemars_support.rs diff --git a/Cargo.toml b/Cargo.toml index a33cee6..c0e8e5b 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 = "1", optional = true } [dev-dependencies] assert_matches = "1.5.0" @@ -28,6 +29,7 @@ serde = { version = "1", features = ["derive"] } default = [] macros = [] serde = ["dep:serde"] +schemars = ["dep:schemars"] [lints.rust] diff --git a/src/lib.rs b/src/lib.rs index 8477b8a..0730695 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,6 +19,8 @@ mod serde_support; #[cfg(feature = "macros")] mod macros; +#[cfg(feature = "schemars")] +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" + }) + } +} From 7f58c95e46b20bb3ce3e8e946087097862f33a0f Mon Sep 17 00:00:00 2001 From: Nicole Venner Date: Tue, 12 Aug 2025 13:30:15 -0600 Subject: [PATCH 2/3] force revert version to 0.9.0 since that seems to be what all the major crates use --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index c0e8e5b..5dbe27d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ name = "non_empty_string" [dependencies] serde = { version = "1", optional = true } delegate = { version = "0.8" } -schemars = { version = "1", optional = true } +schemars = { version = "0.9.0", optional = true } [dev-dependencies] assert_matches = "1.5.0" From bed27bd9069d8be11eb73aeadecf10e039858a5b Mon Sep 17 00:00:00 2001 From: Nicole Venner Date: Fri, 22 Aug 2025 03:38:16 -0600 Subject: [PATCH 3/3] rename feature to schemars09 for future compatability --- Cargo.toml | 2 +- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5dbe27d..f06cc0e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ serde = { version = "1", features = ["derive"] } default = [] macros = [] serde = ["dep:serde"] -schemars = ["dep:schemars"] +schemars09 = ["dep:schemars"] [lints.rust] diff --git a/src/lib.rs b/src/lib.rs index 0730695..e59143b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ mod serde_support; #[cfg(feature = "macros")] mod macros; -#[cfg(feature = "schemars")] +#[cfg(feature = "schemars09")] mod schemars_support; mod trait_impls;