1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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()
    }
}