Skip to content
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

Add optional support for vec1::Vec1 #270

Open
wants to merge 1 commit 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
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 }
vec1 = { version = "1.10.1", features = ["serde"], 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 = "vec1"
required-features = ["vec1"]

[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 @@ -86,4 +86,6 @@ mod url;
mod uuid08;
#[cfg(feature = "uuid1")]
mod uuid1;
#[cfg(feature = "vec1")]
mod vec1;
mod wrapper;
27 changes: 27 additions & 0 deletions schemars/src/json_schema_impls/vec1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use vec1::Vec1;

impl<T> JsonSchema for Vec1<T>
where
T: JsonSchema,
{
no_ref_schema!();

fn schema_name() -> String {
format!("Array_at_least_size_1_of_{}", T::schema_name())
}

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::Array.into()),
array: Some(Box::new(ArrayValidation {
items: Some(gen.subschema_for::<T>().into()),
..Default::default()
})),
..Default::default()
}
.into()
}
}
9 changes: 9 additions & 0 deletions schemars/tests/expected/vec1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Array_at_least_size_1_of_int32",
"type": "array",
"items": {
"type": "integer",
"format": "int32"
}
}
7 changes: 7 additions & 0 deletions schemars/tests/vec1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod util;
use util::*;

#[test]
fn vec1() -> TestResult {
test_default_generated_schema::<vec1::Vec1<i32>>("vec1")
}
Loading