architect_schemars/json_schema_impls/
arrayvec07.rs

1use crate::r#gen::SchemaGenerator;
2use crate::schema::*;
3use crate::JsonSchema;
4use arrayvec07::{ArrayString, ArrayVec};
5use std::convert::TryInto;
6
7// Do not set maxLength on the schema as that describes length in characters, but we only
8// know max length in bytes.
9forward_impl!((<const CAP: usize> JsonSchema for ArrayString<CAP>) => String);
10
11impl<T, const CAP: usize> JsonSchema for ArrayVec<T, CAP>
12where
13    T: JsonSchema,
14{
15    no_ref_schema!();
16
17    fn schema_name() -> String {
18        format!("Array_up_to_size_{}_of_{}", CAP, T::schema_name())
19    }
20
21    fn json_schema(generator: &mut SchemaGenerator) -> Schema {
22        SchemaObject {
23            instance_type: Some(InstanceType::Array.into()),
24            array: Some(Box::new(ArrayValidation {
25                items: Some(generator.subschema_for::<T>().into()),
26                max_items: CAP.try_into().ok(),
27                ..Default::default()
28            })),
29            ..Default::default()
30        }
31        .into()
32    }
33}