s-panferov / valico

Rust JSON Schema validator and JSON coercer
MIT License
297 stars 55 forks source link

add support for V4 boolean exclusiveMaximum and exclusiveMinimum #68

Closed tjh-bluroot closed 1 year ago

tjh-bluroot commented 3 years ago

I found when using a JSON schema with boolean exclusiveMinimum and exclusiveMaximum (based on JSON schema draft 4, see numbers) valico will panic when attempting to compile the schema.

Raising this issue to see if support can be added for the draft 4, i.e. boolean, version of these parameters.

Here is MVCE:

use serde_json::from_str;
use valico::json_schema;

fn main() {
    let j_schema = from_str(r#"
    {
        "type": "object",
        "properties": {
            "amount": {
                "type": "number",
                "minimum": 0,
                "exclusiveMinimum": true,
                "maximum": 100
            }
        },
        "required": ["amount"]
    }
    "#).unwrap();

    let mut scope = json_schema::Scope::new();
    scope.compile_and_return(j_schema, true).unwrap();
}

The resulting panic, after cargo run:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Malformed { path: "properties/amount", detail: "the `minimum/maximum/exclusiveMinimum/exclusiveMaximum` value must be a number" }', src/main.rs:21:5
s-panferov commented 3 years ago

I don't actually use valico for my current projects, so I cannot justify what decisions are correct for this library. It seems that spect have changed in this particular case, but changing the behavior will mean that we introduce a breaking change for everybody. Not sure what is a good way to address that. Do you think it's ok to just enforce the new version of the spec?

tjh-bluroot commented 3 years ago

Hey, I think it could get pretty tangled trying to support all the previous drafts of the spec. i.e. this is one specific example of something that has changed since draft 4, but I suspect there would be many others. I think it appropriate to enforce a newer version up front. Or at the least add a note in the README/docs to indicate that previous drafts (prior to the supported draft 7) may not be supported.