Skip Menu |

This queue is for tickets about the JSON-Schema CPAN distribution.

Report information
The Basics
Id: 132625
Status: open
Priority: 0/
Queue: JSON-Schema

People
Owner: Nobody in particular
Requestors: LNATION [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Feature request - Strict mode
Would it be possible to pass an option "strict" that would then raise an error on unknown properties passed in the data. Take the following example that I would expect to fail with an error specifying that "thing" is an unknown property/key. my $schema = '{ "$schema" : "http://json-schema.org/draft-07/schema#", "$id" : "https://example.com/arrays.schema.json", "title" : "The Root Schema", "description" : "A representation of a person, company, organization, or place", "type" : "object", "examples" : [ { "id" : 1, "checked" : false } ], "required" : [ "checked", "id" ], "properties" : { "checked" : { "$id" : "#/properties/checked", "title" : "The Checked Schema", "description" : "An explanation about the purpose of this instance.", "type" : "boolean", "examples" : [ true, false ] }, "id" : { "$id" : "#/properties/id", "title" : "The ID of the door", "description" : "This section represents the id of the door.", "type" : "integer", "examples" : [ 1 ] } } }'; my $data = '{ "checked": false, "id": 1, "thing": "not okay" }'; my $validator = JSON::Schema->new($schema, strict => 1); my $result = $validator->validate($data); # errors
Hi LNATION, The JSON Schema specification actually allows for properties not listed in the properties array to be present in the data. [1] To have the validation fail in this case, you need to use set the "additionalProperties" property for the object to false: { "$schema" : "http://json-schema.org/draft-07/schema#", "$id" : "https://example.com/arrays.schema.json", "type" : "object", "additionalProperties": false, "properties" : { "checked" : { "type" : "boolean", }, "id" : { "type" : "integer", } } } Then you will get the following error: Show quoted text
> $: The property thing is not defined in the schema and the schema does not > allow additional properties
Also, note that specifying the required elements at the object level is not as you do, despite being conform to the JSON Schema specification, is not honoured by the JSON::Schema module, you have to add it to the property itself, as described in the ticket 95923. I hope this helps, Emmanuel Dupuis