Fix schemas for bigdecimal/rust_decimal (#248)

Both of these crates serialise decimals as strings, not as plain numbers
This commit is contained in:
Graham Esau 2023-09-17 20:31:24 +01:00 committed by GitHub
parent a5e51b22b3
commit db1dd47039
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 5 deletions

View file

@ -120,5 +120,9 @@ required-features = ["smol_str"]
name = "semver"
required-features = ["semver"]
[[test]]
name = "decimal"
required-features = ["rust_decimal", "bigdecimal"]
[package.metadata.docs.rs]
all-features = true

View file

@ -4,19 +4,20 @@ use crate::JsonSchema;
macro_rules! decimal_impl {
($type:ty) => {
decimal_impl!($type => Number, "Number");
};
($type:ty => $instance_type:ident, $name:expr) => {
impl JsonSchema for $type {
no_ref_schema!();
fn schema_name() -> String {
$name.to_owned()
"Decimal".to_owned()
}
fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::$instance_type.into()),
instance_type: Some(InstanceType::String.into()),
string: Some(Box::new(StringValidation {
pattern: Some(r"^-?[0-9]+(\.[0-9]+)?$".to_owned()),
..Default::default()
})),
..Default::default()
}
.into()

12
schemars/tests/decimal.rs Normal file
View file

@ -0,0 +1,12 @@
mod util;
use util::*;
#[test]
fn rust_decimal() -> TestResult {
test_default_generated_schema::<rust_decimal::Decimal>("rust_decimal")
}
#[test]
fn bigdecimal() -> TestResult {
test_default_generated_schema::<bigdecimal::BigDecimal>("bigdecimal")
}

View file

@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Decimal",
"type": "string",
"pattern": "^-?[0-9]+(\\.[0-9]+)?$"
}

View file

@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Decimal",
"type": "string",
"pattern": "^-?[0-9]+(\\.[0-9]+)?$"
}