Skip to content

Feat: Add Support for schemars #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -28,6 +29,7 @@ serde = { version = "1", features = ["derive"] }
default = []
macros = []
serde = ["dep:serde"]
schemars09 = ["dep:schemars"]


[lints.rust]
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ mod serde_support;

#[cfg(feature = "macros")]
mod macros;
#[cfg(feature = "schemars09")]
mod schemars_support;

mod trait_impls;

Expand Down
25 changes: 25 additions & 0 deletions src/schemars_support.rs
Original file line number Diff line number Diff line change
@@ -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"
})
}
}