Stranger6667/jsonschema-rs

Validating individual definitions

Opened this issue · 6 comments

sztomi commented

I'm trying to validate JSON snippets against the DAP JSON schema: https://microsoft.github.io/debug-adapter-protocol/debugAdapterProtocol.json

Each type of message is a separate definition. I can extract and compile the relevant definition, but references are not resolved. I think I need to use the with_document function, but I'm not sure what the ID is supposed to be.

fn main() {
    let schema = include_str!("../debugAdapterProtocol.json");
    let schema: Value = serde_json::from_str(schema).unwrap();

    // Get the specific definition from the schema
    let next_request_schema = schema.get("definitions").unwrap().get("NextRequest").unwrap().clone();
    
    // Compile this specific schema
    let compiled = JSONSchema::options()
        .should_validate_formats(true)
        .should_ignore_unknown_formats(false)
        .with_document("#".to_string(), schema)
        .compile(&next_request_schema)
        .unwrap();

    let instance = json!({
        "seq": 153,
        "type": "request",
        "command": "next",
        "arguments": {
        "threadId": 3
        }
    });
    let result = compiled.validate(&instance);
    if let Err(errors) = result {
        for error in errors {
            println!("Validation error: {}", error);
            println!("Instance path: {}", error.instance_path);
        }
    }
}

This results in

Validation error: Invalid reference: json-schema:///#/definitions/Request
Instance path:
Validation error: Invalid reference: json-schema:///#/definitions/NextArguments
Instance path:

I tried an empty string as well, but that didn't work either. Any clues?

I am facing the same problem with internal references. Were you able to figure it out?

@VoltaireNoir with a very ugly hack: https://github.com/sztomi/dap-rs/blob/main/integration_tests/src/lib.rs#L27

Ah, I arrived at the same solution: to replace the references with the appropriate schema. I guess I'll stick with it until jsonschema is able to resolve local/internal references correctly.

I have complex schema with dependencies between files and defined objects. I can't extract an object like @sztomi has shown in his example. Is it still possible to use this subschema to validate?

I have complex schema with dependencies between files and defined objects. I can't extract an object like @sztomi has shown in his example. Is it still possible to use this subschema to validate?

I believe this library supports resolving web and file based references. Try enabling the resolve-http and resolve-file feature flags.

Try enabling the resolve-http and resolve-file feature flags.

This solves a small portion of my goal.

I have several files common.yml, schema1.yml and schema2.yml (json schema is saved in safe yaml without references), all of these files contain all objects under $defs. Main schema contains only schema version, it's $id and minimal description. My goal is to create a validator object to validate by a definition for an object defined as above. Obviously loader is restricted by a folder where schemas are located and other locations are deliberately disabled to avoid security issues.

In current setup I use another validator (as it's a Python project) where I able to load given files into registry and then during application startup add referencing schemas to create an actual validator. Referencing schemas are anonymous schemas with references to an actual object defined in the registry, e.g. containing only $ref pointing to schema1.yml:#/$defs/obj1 or urn:schema:2:id:#/$defs/obj2 (where urn:schema:2:id is an $id value from schema2.yml file).

Could you please help me how to do a similar thing using this library?