jhthorsen / json-validator

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

additionalProperties set to false isn't working #71

Closed foobargeez closed 7 years ago

foobargeez commented 7 years ago

Schema:

{
   "required" : [
      "link"
   ],
   "type" : "object",
   "additionalProperties" : false,
   "properties" : {
      "link" : {
         "format" : "uri"
      }
   }
}

input:

{
  "link": "http://a",
  "haha": "hehe"
}

JSON::Validator doesn't throw errors (incorrect) while validator at http://www.jsonschemavalidator.net/ throws errors (correct).

Thanks in advance!

jhthorsen commented 7 years ago

That is weird. There's even a test for it: https://github.com/jhthorsen/json-validator/blob/master/t/jv-object.t

foobargeez commented 7 years ago

The test is failing for both true and false for additionalProperties -- the test seems to be broken.

foobargeez commented 7 years ago

I dug this a bit ... IIUC, t/Helper.pm has an issue. If validate() returns no errors, Test::More::is_deeply is comparing undef with an array of hashes. I added this line and it seems to evaluate validate_ok correctly now and is behaving as expected. Please review if it makes sense.

   my @errors = validator()->schema($schema)->validate($data);
+  @errors = scalar @errors ? @errors : ({});
   Test::More::is_deeply([map { $_->TO_JSON } sort { $a->path cmp $b->path } @errors],
foobargeez commented 7 years ago

Also, regarding the original issue -- additionalProperties is not working when set to false or true -- per my understanding of code, the module expects this property to be a 0 or 1 and not boolean. I was setting it to \0 or \1 but the module isn't recognizing it. If you're OK, I am happy to submit a patch that recognizes boolean value for this property and not numeric. Let me know.

foobargeez commented 7 years ago

For now, I set the value of additionalProperties in my JSON schema to numeric 0 and I got this working, although, I would ideally prefer to use boolean, so I don't have to diverge the JSON configurations across code bases and programming languages. Let me know your thoughts.

jhthorsen commented 7 years ago

I don't get this at all. 3a3a470d66c093d3a24b9eb4c1fbf3f70cc0e721 shows that your input data is not valid.

Please write a test that shows what you expect.

jhthorsen commented 7 years ago

There seems to be some confusion around $JSON::PP::false and \0.

Closing this issue based on the discussion on https://github.com/jhthorsen/json-validator/commit/3a3a470d66c093d3a24b9eb4c1fbf3f70cc0e721