tdegrunt / jsonschema

JSON Schema validation
Other
1.82k stars 262 forks source link

$id is incompatible with allowUnknownAttributes: false #389

Open natnat-mc opened 11 months ago

natnat-mc commented 11 months ago

Setting allowUnknownAttributes: false in settings causes Validator.validate to fail if the schema or a subschema uses the $id property, making the use of subschemas impossible in this case. This can technically be worked around by using id instead of $id.

Minimal example:

const {Validator} = require('jsonschema')
const v = new Validator()
v.addSchema({$id: '/Test', type: 'string'})
console.log(v.validate('a', {$ref: '/Test'}))
console.log(v.validate('a', {$ref: '/Test'}, {allowUnknownAttributes: false}))

Expected result: not crashing, validating correctly Actual result: crashes on line 5, SchemaError: Unsupported attribute: $id, schema: { '$id': '/Test', type: 'string' } (this is not dependant on line 4 being present, but this shows that line 4 works as expected)

Adding $id: true in lib/attribute.js, attribute.ignoreProperties seems to fix this.

If needed, i can create a PR for this, but I don't know what to write for the tests as there doesnt seem to be any for allowUnknownAttributes yet.