tdegrunt / jsonschema

JSON Schema validation
Other
1.82k stars 262 forks source link

Nested Schema in the definitions object unsupported attribute when allowUnknownAttributes is set to false #379

Open jonbaxt opened 1 year ago

jonbaxt commented 1 year ago

When attempting to make validation strict and not allow any extra attributes on the object I'm validating allowUnknownAttributes set to false works as expected until you are having a ref to an additional schema and accessed in the definitions object on the Schema object.

Example interface Nested { fullName: string; isValid: boolean; nested: PostNewUser[]; } translating this typescript into a schema results in looking like this:

{ "type": "object", "properties": { "fullName": { "type": "string" }, "isValid": { "type": "boolean" }, "nested": { "type": "array", "items": { "$ref": "#/definitions/PostNewUser" } } }, "required": [ "fullName", "isValid", "nested" ], "definitions": { "PostNewUser": { "type": "object", "properties": { "firstName": { "type": "string" }, "lastName": { "type": "string" }, "address": { "type": "array", "items": { "type": "string" } }, "phone": { "type": "number" } }, "required": [ "address", "firstName", "lastName", "phone" ] } }, "$schema": "http://json-schema.org/draft-07/schema#" }

Here is a test example of the JSON object I'm validating that should return true: { "fullName": "Jed Clampet", "isValid": true, "nested": [ { "firstName": "Jed", "lastName": "Clampet", "address": ["123 Fake Street"], "phone": 82155482 } ] }

When I run a valid object to this with allowUnknownAttributes being false I get an error like this:

SchemaError: Unsupported attribute: definitions\n at Validator.validateSchema

I think it makes sense for referenced schemas to be supported when allowUnknownAttributes is set to false. Is this possible to get fixed, or is there another way to get this to work?