jhthorsen / json-validator

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

file:// schema url behaves differently to data:// #172

Closed kiwiroy closed 4 years ago

kiwiroy commented 5 years ago

Steps to reproduce the behavior

$schema = path(__FILE__)->sibling('schema.yaml');
$validator = JSON::Validator->new;
$validator->schema(sprintf 'file://%s', $schema);

where schema.yaml contains this snippet

example:
  firstName: "Foo"
  lastName: "Bar"
  age: 6
  id: 1234

See gist for more info.

Expected behavior

No error for the file:// url. Using data:// and the same schema does not result in the error.

Actual behavior

Can't locate object method "scheme" via package "Mojo::File" at /.../lib/Mojo/URL.pm line 134.

This appears to be solved by master...kiwiroy:file-schema-id

Using draft-7 also sidesteps the issue.

jhthorsen commented 4 years ago

The gist makes my head hurt. There's just too much information there. Can you make https://github.com/mojolicious/json-validator/blob/master/t/load-file.t fail instead?

jhthorsen commented 4 years ago

Closing this, since the issue seems to have gone stale.

mschout commented 4 years ago

yes, this is the same issue as jhthorsen/mojolicious-plugin-openapi#173 That is, the Mojolicious plugin is setting its own version attribute which puts JSON::Validator into a mode where it thinks id is a reference that it needs to resolve.

karenetheridge commented 4 years ago

I attempted to fix this in https://github.com/mojolicious/json-validator/pull/152. The problem is that the '$schema' key is not being parsed out of the document after it has been read, which is what tells us what spec version to use.

The easy solution is to instantiate the JSON::Validator object with the desired version attribute (or set it yourself later): my $jv = JSON::Validator->new->version(7)->schema(...) -- it's important to do this before the schema is resolved, as it's not possible to go back and request a reparsing of the document with a different spec version's semantics (or erase what it thinks it learned about the document when it misparsed it the first time).