networknt / json-schema-validator

A fast Java JSON schema validator that supports draft V4, V6, V7, V2019-09 and V2020-12
Apache License 2.0
855 stars 325 forks source link

Using $ref with custom Path like $id of schema #921

Closed SudBisht closed 9 months ago

SudBisht commented 9 months ago

Use Case I have storage where the schema is stored as <key : value>, key as Schema Id, value as JSON Schema

in the below Schema where $id (http://ab.cd.com/example/string/exampleID, http://ab.cd.com/example/string/justId) is just a string, not any URL so validating schema, getting java.io.FileNotFoundException exception

Any recommendations to solve this use case would be highly appreciated.

don't have option to update the $ref:$id part.

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "$id": "http://ab.cd.com/example/string/exampleID",
    "type": "object",
    "required" : ["ExampleProperties"],
    "properties": {
        "ExampleProperties": {
            "type": "object",
            "properties": {
                "CommonProperties": {
                    "$ref": "http://ab.cd.com/example/string/justId". // id of another Json schema
                }
            },
            "required": ["CommonProperties"]
            "additionalProperties": false
        }
    },
}
SudBisht commented 9 months ago

able to solve this like :


JsonSchemaFactory factory = JsonSchemaFactory
        .builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V6))
        .uriFetcher(uri -> uri.equals(URI.create("http://ab.cd.com/example/string/justId")) 
                          ? Thread.currentThread().getContextClassLoader().getResourceAsStream("schema file path in local system");
                          : null, "http").build();