karenetheridge / JSON-Schema-Modern

Validate data against a schema using a JSON Schema
https://metacpan.org/release/JSON-Schema-Modern/
Other
10 stars 1 forks source link

exception "ambigous type" in schema validation #75

Closed miniHive closed 1 year ago

miniHive commented 1 year ago

Experimenting with the validator, I run into a exception "ambigous type":

Data: { "a": 0, "b": 0, "c": 0 }

Schema:

{
  "type": "object",
  "properties":  {
        "a": {"type": "integer"} ,
        "b": false
  },
  "unevaluatedProperties" : false
}

Output (flag "basic"):

{
  "errors": [
    {
      "absoluteKeywordLocation": "https://formalize.edu/onlya_no_b.json#/properties",
      "error": "EXCEPTION: ambiguous type for \"\" at /usr/local/lib/perl5/site_perl/5.34.1/JSON/Schema/Modern.pm line 463.\n",
      "instanceLocation": "",
      "keywordLocation": "/properties"
    }
  ],
  "valid": false
}
karenetheridge commented 1 year ago

Cannot reproduce:

$ json-schema-eval
enter data instance, followed by ^D:
{ "a": 0, "b": 0, "c": 0 }
^D
enter schema, followed by ^D:
{
  "type": "object",
  "properties":  {
        "a": {"type": "integer"} ,
        "b": false
  },
  "unevaluatedProperties" : false
}
^D
{
  "errors" : [
    {
      "error" : "property not permitted",
      "instanceLocation" : "/b",
      "keywordLocation" : "/properties/b"
    },
    {
      "error" : "not all properties are valid",
      "instanceLocation" : "",
      "keywordLocation" : "/properties"
    },
    {
      "error" : "additional property not permitted",
      "instanceLocation" : "/c",
      "keywordLocation" : "/unevaluatedProperties"
    },
    {
      "error" : "not all additional properties are valid",
      "instanceLocation" : "",
      "keywordLocation" : "/unevaluatedProperties"
    }
  ],
  "valid" : false
}

Can you provide the actual data you are passing, not json-encoded? Also see https://metacpan.org/pod/JSON::Schema::Modern#Types.

miniHive commented 1 year ago

We wrote a code wrapper to be able to read the data from file: https://github.com/sdbs-uni-p/ke_validator/blob/main/runScript.pl

Did we maybe garble the input in doing so?

karenetheridge commented 1 year ago

I can reproduce your issue with your test script.

I replaced your use of JSON::Parse in your test script with:

use Cpanel::JSON::XS;
use Path::Tiny;

...
my $instance_data = decode_json(path($instance_path)->slurp_raw);
my $schema_data = decode_json(path($schema_path)->slurp_raw);
...

..and the problem goes away. I would suggest not using JSON::Parse, as it is clearly not decoding the json data correctly. You might want to report to the maintainers of JSON::Parse that they are inflating integers as PVNV, rather than IV. Cpanel::JSON::XS (or the wrapper module, JSON::MaybeXS) is the most robust and reliable JSON decoder in perl.

miniHive commented 1 year ago

Thank you so much, it now works wonderfully.