jhthorsen/json-validator

Validator does not throw error if there are extra properties

Closed this issue · 2 comments

  • JSON::Validator version: 3.15
  • Perl version: 5.24.0
  • Operating system: RHEL7
    If there is a typo in one of the elements, then JSON schema will ignore it totally and it also doesn't check if the defined elements belong to one of the properties defined in the schema. This can have dangerous consequences. For example, I have a schema of DB filters, I process the JSON to create a DB query after validating it against the schema. If one of the fields is typoed, then that field is simply left out and I have no idea of that. Example,

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.

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

Thanks, @jhthorsen