architect_schemars/json_schema_impls/
decimal.rs

1use crate::r#gen::SchemaGenerator;
2use crate::schema::*;
3use crate::JsonSchema;
4use std::borrow::Cow;
5
6macro_rules! decimal_impl {
7    ($type:ty) => {
8        impl JsonSchema for $type {
9            no_ref_schema!();
10
11            fn schema_name() -> String {
12                "Decimal".to_owned()
13            }
14
15            fn schema_id() -> Cow<'static, str> {
16                Cow::Borrowed("Decimal")
17            }
18
19            fn json_schema(_: &mut SchemaGenerator) -> Schema {
20                SchemaObject {
21                    instance_type: Some(InstanceType::String.into()),
22                    string: Some(Box::new(StringValidation {
23                        pattern: Some(r"^-?[0-9]+(\.[0-9]+)?$".to_owned()),
24                        ..Default::default()
25                    })),
26                    ..Default::default()
27                }
28                .into()
29            }
30        }
31    };
32}
33
34#[cfg(feature = "rust_decimal")]
35decimal_impl!(rust_decimal::Decimal);
36#[cfg(feature = "bigdecimal03")]
37decimal_impl!(bigdecimal03::BigDecimal);
38#[cfg(feature = "bigdecimal04")]
39decimal_impl!(bigdecimal04::BigDecimal);