Skip to content

Commit d970082

Browse files
Feat: Add Support for schemars
1 parent 9687d29 commit d970082

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ name = "non_empty_string"
1818
[dependencies]
1919
serde = { version = "1", optional = true }
2020
delegate = { version = "0.8" }
21+
schemars = { version = "1", optional = true }
2122

2223
[dev-dependencies]
2324
assert_matches = "1.5.0"
@@ -28,6 +29,7 @@ serde = { version = "1", features = ["derive"] }
2829
default = []
2930
macros = []
3031
serde = ["dep:serde"]
32+
schemars = ["dep:schemars"]
3133

3234

3335
[lints.rust]

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ mod serde_support;
1919

2020
#[cfg(feature = "macros")]
2121
mod macros;
22+
#[cfg(feature = "schemars")]
23+
mod schemars_support;
2224

2325
mod trait_impls;
2426

src/schemars_support.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use std::borrow::Cow;
2+
3+
use schemars::{json_schema, JsonSchema};
4+
5+
use crate::NonEmptyString;
6+
7+
impl JsonSchema for NonEmptyString {
8+
fn schema_name() -> std::borrow::Cow<'static, str> {
9+
"nonemtpy_string".into()
10+
}
11+
fn inline_schema() -> bool {
12+
true
13+
}
14+
fn schema_id() -> Cow<'static, str> {
15+
"nonempty_string".into()
16+
}
17+
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
18+
json_schema!({
19+
"type": "string",
20+
"minLength": 1,
21+
"title": "Non-Empty String",
22+
"description": "A string that must contain at least one character"
23+
})
24+
}
25+
}

0 commit comments

Comments
 (0)