santhosh-tekuri/jsonschema

About using jsonschema.CompileString

Zacama opened this issue · 2 comments

Hello, I have a question about using jsonschema: Why do I need to pass a URL parameter when calling CompileString?

the url you pass to CompileString gets used in error messages:

jsonschema.CompileString("schema.json", schemaString)
[I#] [S#] doesn't validate with file:///Users/santhosh/jsonschema/schema.json#
  [I#] [S#/$ref] doesn't validate with 'file:///Users/santhosh/jsonschema/t.json#/definitions/employee'
    [I#] [S#/definitions/employee/type] expected string, but got number

in above error message you can see file:///Users/santhosh/jsonschema/schema.json

also in detailed and verbose output formats, the absoluteKeywordLocation property uses this url

then you might ask, why can't we just internally use some default say schema.json and do not ask user. there is one reason why we are not assuming some default uri. see explanation below:

jsonschema.CompileString(`{
    "type": "object",
    "properties": {
        "p": { "$ref": "schema.json" }
    }
}`)

assume that we have schema.json in current working directory. and in above code the user intension is the $ref to load schema.json from current working directory. But if we assume default uri of passed schema string as schema.json then the $ref resolves the schema string itself, instead of schema.json from current working directory.

I hope I am able to explain why you need to pass URL parameter to CompileString

@santhosh-tekuri Thank you very much! I get it.