jhthorsen / json-validator

:cop: Validate data against a JSON schema
https://metacpan.org/release/JSON-Validator
56 stars 58 forks source link

Validator does not throw error if there are extra properties #216

Closed abhsha-amd closed 4 years ago

abhsha-amd commented 4 years ago

my $working_schema = { type => "object", required => ["firstName", "lastName"], definitions => { special_id_type => { type => "string", pattern => "^([0-9]+)-([0-9]+)" } }, properties => { firstName => {type => "string"}, lastName => {type => "string"}, age => {type => "integer", minimum => 0, description => "Age in years"}, id => { type => "array", items => { type=> "string" , "\$ref" => "#/definitions/special_id_type" }} } };

Now if my JSON contains id2 instead of id, it doesn't throw any error.

my $jv2 = JSON::Validator->new; my @errors2 = $jv2->validate({firstName => "Jan Henning", lastName => "Thorsen", age => 42, id2 => [["12", "123"]]});

This doesn't throw error.

Expected behavior

All the properties mentioned in the JSON should be checked against the schema, and if any property found outside of schema, an error should be thrown.

jhthorsen commented 4 years ago

This depends a bit on the schema you are using, but I think what you want is to add "additionalProperties":false.

abhsha-amd commented 4 years ago

Thanks, @jhthorsen