Implement JsonSchema on smallvec + arrayvec types

Documentation still needs updating.
This commit is contained in:
Graham Esau 2019-12-28 20:11:38 +00:00
parent aec4824425
commit a97d54bcad
9 changed files with 99 additions and 0 deletions

View file

@ -21,6 +21,10 @@ chrono = { version = "0.4", default-features = false, optional = true }
indexmap = { version = "1.2", optional = true }
either = { version = "1.3", default-features = false, optional = true }
uuid = { version = "0.8", default-features = false, optional = true }
smallvec = { version = "1.0", optional = true }
arrayvec = { version = "0.5", default-features = false, optional = true }
# TODO implement JsonSchema on bytes types. bytes 0.5 requires Rust 1.39+
# bytes = { version = "0.5", default-features = false, optional = true }
[dev-dependencies]
pretty_assertions = "0.6.1"
@ -46,6 +50,14 @@ required-features = ["either"]
name = "uuid"
required-features = ["uuid"]
[[test]]
name = "smallvec"
required-features = ["smallvec"]
[[test]]
name = "arrayvec"
required-features = ["arrayvec"]
[[test]]
name = "schema_for_schema"
required-features = ["impl_json_schema"]

View file

@ -0,0 +1,33 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use arrayvec::{Array, ArrayString, ArrayVec};
use std::convert::TryInto;
// Do not set maxLength on the schema as that describes length in characters, but we only
// know max length in bytes.
forward_impl!((<A> JsonSchema for ArrayString<A> where A: Array<Item = u8> + Copy) => String);
impl<A: Array> JsonSchema for ArrayVec<A>
where
A::Item: JsonSchema,
{
no_ref_schema!();
fn schema_name() -> String {
format!("Array_up_to_size_{}_of_{}", A::CAPACITY, A::Item::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::<A::Item>().into()),
max_items: A::CAPACITY.try_into().ok(),
..Default::default()
})),
..Default::default()
}
.into()
}
}

View file

@ -42,6 +42,10 @@ mod indexmap;
mod either;
#[cfg(feature = "uuid")]
mod uuid;
#[cfg(feature = "smallvec")]
mod smallvec;
#[cfg(feature = "arrayvec")]
mod arrayvec;
mod core;
mod ffi;
mod maps;

View file

@ -0,0 +1,6 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use smallvec::{Array, SmallVec};
forward_impl!((<A: Array> JsonSchema for SmallVec<A> where A::Item: JsonSchema) => Vec<A::Item>);

View file

@ -0,0 +1,13 @@
mod util;
use arrayvec::{ArrayString, ArrayVec};
use util::*;
#[test]
fn arrayvec() -> TestResult {
test_default_generated_schema::<ArrayVec::<[i32; 16]>>("arrayvec")
}
#[test]
fn arrayvec_string() -> TestResult {
test_default_generated_schema::<ArrayString::<[u8; 16]>>("arrayvec_string")
}

View file

@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Array_up_to_size_16_of_int32",
"type": "array",
"items": {
"type": "integer",
"format": "int32"
},
"maxItems": 16
}

View file

@ -0,0 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "String",
"type": "string"
}

View file

@ -0,0 +1,8 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Array_of_String",
"type": "array",
"items": {
"type": "string"
}
}

View file

@ -0,0 +1,8 @@
mod util;
use smallvec::SmallVec;
use util::*;
#[test]
fn smallvec() -> TestResult {
test_default_generated_schema::<SmallVec<[String; 2]>>("smallvec")
}