Stranger6667/jsonschema-rs

Incorrect `schema_path` in errors

Closed this issue · 2 comments

Validating this json with this schema yields a schema_path of [PathChunk::Keyword("oneOf")] which points to nothing, I'd expect the schema path to be something like /definitions/Neg or /definitions/Node/3, something that actually points to the definition at hand

tommy commented

I've encountered what I beleive is the same error. I believe it's related to multiple iterations of $ref schemas. In the following minimal repro, you can see the first error has the correct schema_path, but the second just returns "/enum"

Cargo.toml

[package]
name = "scratch"
version = "0.1.0"
edition = "2021"

[dependencies]
jsonschema = "0.17.0"
serde_json = "1.0.96"

main.rs

fn main() {
    let json = serde_json::json!({
        "things": [
            { "code": "CC" },
            { "code": "CC" },
        ]
    });

    let schema = serde_json::json!({
        "type": "object",
        "properties": {
            "things": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "code": {
                            "type": "string",
                            "$ref": "#/$defs/codes"
                        }
                    },
                    "required": ["code"]
                }
            }
        },
        "required": ["things"],
        "$defs": { "codes": { "enum": ["AA", "BB"] } }
    });

    let compiled = jsonschema::JSONSchema::options().compile(&schema).unwrap();

    let result = compiled.validate(&json);
    if let Err(err_iter) = result {
        for err in err_iter {
            println!("Schema path {}", err.schema_path);
        }
    }
}

output

Schema path /properties/things/items/properties/code/enum
Schema path /enum

Sorry for the delay - the fix will be released soon