diff -Nru libjson-validator-perl-2.07+dfsg/Changes libjson-validator-perl-2.08+dfsg/Changes --- libjson-validator-perl-2.07+dfsg/Changes 2018-04-18 07:54:21.000000000 +0000 +++ libjson-validator-perl-2.08+dfsg/Changes 2018-06-03 11:19:22.000000000 +0000 @@ -1,5 +1,11 @@ Revision history for perl distribution JSON-Validator +2.08 2018-06-03T19:19:22+0800 + - Fix validating oneOf correctly #103 + - Fix validating "id" property #111 + - Add support for $id keyword in draft7 #114 + - Bundle JSON Schema draft-06 and draft-07 + 2.07 2018-04-18T09:54:21+0200 - Fix joi->object->strict() Contributor: Pierre-Aymeric Masse diff -Nru libjson-validator-perl-2.07+dfsg/debian/changelog libjson-validator-perl-2.08+dfsg/debian/changelog --- libjson-validator-perl-2.07+dfsg/debian/changelog 2018-05-06 17:12:21.000000000 +0000 +++ libjson-validator-perl-2.08+dfsg/debian/changelog 2018-06-04 06:07:59.000000000 +0000 @@ -1,3 +1,13 @@ +libjson-validator-perl (2.08+dfsg-1) unstable; urgency=medium + + * Team upload. + * New upstream release. + * Bump debhelper compatibility level to 11. + * Skip t/id-keyword-draft4.t and t/id-keyword-draft7.t tests. + * Update Recommends dependencies. + + -- Andrius Merkys Mon, 04 Jun 2018 02:07:59 -0400 + libjson-validator-perl (2.07+dfsg-1) unstable; urgency=medium * Team upload. diff -Nru libjson-validator-perl-2.07+dfsg/debian/compat libjson-validator-perl-2.08+dfsg/debian/compat --- libjson-validator-perl-2.07+dfsg/debian/compat 2018-05-06 17:12:21.000000000 +0000 +++ libjson-validator-perl-2.08+dfsg/debian/compat 2018-06-04 06:07:59.000000000 +0000 @@ -1 +1 @@ -10 +11 diff -Nru libjson-validator-perl-2.07+dfsg/debian/control libjson-validator-perl-2.08+dfsg/debian/control --- libjson-validator-perl-2.07+dfsg/debian/control 2018-05-06 17:12:21.000000000 +0000 +++ libjson-validator-perl-2.08+dfsg/debian/control 2018-06-04 06:07:59.000000000 +0000 @@ -4,7 +4,7 @@ Section: perl Testsuite: autopkgtest-pkg-perl Priority: optional -Build-Depends: debhelper (>= 10) +Build-Depends: debhelper (>= 11) Build-Depends-Indep: libboolean-perl, libmojolicious-perl (>= 7.28), libtest-warnings-perl, @@ -21,6 +21,8 @@ ${perl:Depends}, libmojolicious-perl (>= 7.28) Recommends: libcpanel-json-xs-perl (>= 3.0200), + libdata-validate-domain-perl, + libdata-validate-ip-perl, libhash-multivalue-perl, libyaml-libyaml-perl (>= 0.67) Description: module to validate data against a JSON schema diff -Nru libjson-validator-perl-2.07+dfsg/debian/tests/pkg-perl/smoke-skip libjson-validator-perl-2.08+dfsg/debian/tests/pkg-perl/smoke-skip --- libjson-validator-perl-2.07+dfsg/debian/tests/pkg-perl/smoke-skip 2018-05-06 17:12:21.000000000 +0000 +++ libjson-validator-perl-2.08+dfsg/debian/tests/pkg-perl/smoke-skip 2018-06-04 06:07:59.000000000 +0000 @@ -7,5 +7,7 @@ t/load-json.t t/recursion.t t/validate-schema.t +t/id-keyword-draft4.t +t/id-keyword-draft7.t # empty file t/spec/invalid-ref.t diff -Nru libjson-validator-perl-2.07+dfsg/lib/JSON/Validator/OpenAPI.pm libjson-validator-perl-2.08+dfsg/lib/JSON/Validator/OpenAPI.pm --- libjson-validator-perl-2.07+dfsg/lib/JSON/Validator/OpenAPI.pm 2018-03-10 15:56:13.000000000 +0000 +++ libjson-validator-perl-2.08+dfsg/lib/JSON/Validator/OpenAPI.pm 2018-06-02 09:36:22.000000000 +0000 @@ -4,7 +4,7 @@ use Mojo::Util; use Scalar::Util (); -use constant DEBUG => $ENV{JSON_VALIDATOR_DEBUG} || 0; +use constant DEBUG => $ENV{JSON_VALIDATOR_DEBUG} || 0; use constant IV_SIZE => eval 'require Config;$Config::Config{ivsize}'; use constant SPECIFICATION_URL => 'http://swagger.io/v2/schema.json'; @@ -12,6 +12,7 @@ my %COLLECTION_RE = (pipes => qr{\|}, csv => qr{,}, ssv => qr{\s}, tsv => qr{\t}); +has version => 2; has _json_validator => sub { state $v = JSON::Validator->new; }; sub load_and_validate_schema { @@ -145,7 +146,7 @@ my $in = $p->{in}; my $schema = { properties => {$name => $p->{'x-json-schema'} || $p->{schema} || $p}, - required => [$p->{required} ? ($name) : ()] + required => [$p->{required} ? ($name) : ()] }; if ($in eq 'body') { @@ -355,6 +356,10 @@ =back +=head2 version + +See L. + =head1 METHODS L inherits all attributes from L. diff -Nru libjson-validator-perl-2.07+dfsg/lib/JSON/Validator.pm libjson-validator-perl-2.08+dfsg/lib/JSON/Validator.pm --- libjson-validator-perl-2.07+dfsg/lib/JSON/Validator.pm 2018-04-18 07:54:21.000000000 +0000 +++ libjson-validator-perl-2.08+dfsg/lib/JSON/Validator.pm 2018-06-03 11:19:22.000000000 +0000 @@ -26,7 +26,7 @@ use constant VALIDATE_IP => eval 'require Data::Validate::IP;1'; our $ERR; # ugly hack to improve validation errors -our $VERSION = '2.07'; +our $VERSION = '2.08'; our @EXPORT_OK = qw(joi validate_json); my $BUNDLED_CACHE_DIR = path(path(__FILE__)->dirname, qw(Validator cache)); @@ -39,7 +39,8 @@ sub S { Mojo::Util::md5_sum(Data::Dumper->new([@_])->Sortkeys(1)->Useqq(1)->Dump) } has cache_paths => sub { [split(/:/, $ENV{JSON_VALIDATOR_CACHE_PATH} || ''), $BUNDLED_CACHE_DIR] }; -has formats => sub { shift->_build_formats }; +has formats => sub { shift->_build_formats }; +has version => 4; has ua => sub { require Mojo::UserAgent; @@ -171,8 +172,10 @@ sub load_and_validate_schema { my ($self, $spec, $args) = @_; + my $schema = $args->{schema} || SPECIFICATION_URL; + $self->version($1) if !$self->{version} and $schema =~ /draft-0+(\w+)/; $spec = $self->_resolve($spec); - my @errors = $self->new(%$self)->schema($args->{schema} || SPECIFICATION_URL)->validate($spec); + my @errors = $self->new(%$self)->schema($schema)->validate($spec); confess join "\n", "Invalid JSON specification $spec:", map {"- $_"} @errors if @errors; $self->{schema} = Mojo::JSON::Pointer->new($spec); $self; @@ -207,16 +210,19 @@ sub _build_formats { return { - 'date-time' => \&_is_date_time, - 'email' => \&_is_email, - 'hostname' => VALIDATE_HOSTNAME ? \&Data::Validate::Domain::is_domain : \&_is_domain, - 'ipv4' => VALIDATE_IP ? \&Data::Validate::IP::is_ipv4 : \&_is_ipv4, - 'ipv6' => VALIDATE_IP ? \&Data::Validate::IP::is_ipv6 : \&_is_ipv6, - 'regex' => \&_is_regex, - 'uri' => \&_is_uri, + 'date-time' => \&_is_date_time, + 'email' => \&_is_email, + 'hostname' => VALIDATE_HOSTNAME ? \&Data::Validate::Domain::is_domain : \&_is_domain, + 'ipv4' => VALIDATE_IP ? \&Data::Validate::IP::is_ipv4 : \&_is_ipv4, + 'ipv6' => VALIDATE_IP ? \&Data::Validate::IP::is_ipv6 : \&_is_ipv6, + 'regex' => \&_is_regex, + 'uri' => \&_is_uri, + 'uri-reference' => \&_is_uri_reference, }; } +sub _id_key { $_[0]->version < 7 ? 'id' : '$id' } + sub _load_schema { my ($self, $url) = @_; @@ -293,7 +299,10 @@ confess "GET $url == $err" if DEBUG and $err; die "[JSON::Validator] GET $url == $err" if $err; - if ($cache_path and $cache_path ne $BUNDLED_CACHE_DIR and -w $cache_path) { + if ( $cache_path + and ($cache_path ne $BUNDLED_CACHE_DIR or $ENV{JSON_VALIDATOR_CACHE_ANYWAYS}) + and -w $cache_path) + { $cache_file = path $cache_path, $cache_file; warn "[JSON::Validator] Caching $url to $cache_file\n" unless $ENV{HARNESS_ACTIVE}; $cache_file->spurt($tx->res->body); @@ -351,13 +360,14 @@ # resolve all the $ref's that we find inside JSON Schema specification. sub _resolve { my ($self, $schema) = @_; + my $id_key = $self->_id_key; my ($id, $resolved, @refs); local $self->{level} = $self->{level} || 0; delete $_[0]->{schemas}{''} unless $self->{level}; if (ref $schema eq 'HASH') { - $id = $schema->{id} // ''; + $id = $schema->{$id_key} // ''; return $resolved if $resolved = $self->{schemas}{$id}; } elsif ($resolved = $self->{schemas}{$schema // ''}) { @@ -365,11 +375,11 @@ } else { ($schema, $id) = $self->_load_schema($schema); - $id = $schema->{id} if $schema->{id}; + $id = $schema->{$id_key} if $schema->{$id_key}; } unless ($self->{level}) { - my $rid = $schema->{id} // $id; + my $rid = $schema->{$id_key} // $id; if ($rid) { confess "Root schema cannot have a fragment in the 'id'. ($rid)" if $rid =~ /\#./; confess "Root schema cannot have a relative 'id'. ($rid)" @@ -394,8 +404,8 @@ elsif (UNIVERSAL::isa($topic, 'HASH')) { push @refs, [$topic, $base] and next if $topic->{'$ref'} and !ref $topic->{'$ref'}; - if ($topic->{id} and !ref $topic->{id}) { - my $fqn = Mojo::URL->new($topic->{id}); + if ($topic->{$id_key} and !ref $topic->{$id_key}) { + my $fqn = Mojo::URL->new($topic->{$id_key}); $fqn = $fqn->to_abs($base) unless $fqn->is_abs; $self->_register_schema($topic, $fqn->to_string); } @@ -441,25 +451,22 @@ tie %$topic, 'JSON::Validator::Ref', $other, $topic->{'$ref'}, $fqn; } -# This code is from Data::Dumper::format_refaddr() -sub _seen { - my $self = shift; - my $key = join ':', shift, map { pack 'J', refaddr $_ } @_; - return $self->{seen}{$key}++; -} - sub _validate { my ($self, $data, $path, $schema) = @_; - my ($type, @errors); + my ($seen_addr, $type, @errors); $schema = $self->_ref_to_schema($schema) if $schema->{'$ref'}; + $seen_addr = refaddr $schema; + $seen_addr .= ':' . (ref $data ? refaddr $data : "s:$data") if defined $data; # Avoid recursion - if (ref $data and !_is_blessed_boolean($data) and $self->_seen($schema, $data)) { + if ($self->{seen}{$seen_addr}) { $self->_report_schema($path || '/', 'seen', $schema) if REPORT; - return; + return @{$self->{seen}{$seen_addr}}; } + $self->{seen}{$seen_addr} = \@errors; + # Make sure we validate plain data and not a perl object $data = $data->TO_JSON if blessed $data and UNIVERSAL::can($data, 'TO_JSON'); $type = $schema->{type} || _guess_schema_type($schema, $data); @@ -766,16 +773,9 @@ $additional = {} unless UNIVERSAL::isa($additional, 'HASH'); $rules{$_} ||= [$additional] for @dkeys; } - else { - # Special case used internally when validating schemas: This module adds "id" - # on the top level which might conflict with very strict schemas, so we have to - # remove it again unless there's a rule. - local $rules{id} = 1 if !$path and exists $data->{id}; - - if (my @k = grep { !$rules{$_} } @dkeys) { - local $" = ', '; - return E $path, "Properties not allowed: @k."; - } + elsif (my @k = grep { !$rules{$_} } @dkeys) { + local $" = ', '; + return E $path, "Properties not allowed: @k."; } for my $k (sort keys %required) { @@ -786,18 +786,13 @@ for my $k (sort keys %rules) { for my $r (@{$rules{$k}}) { - if (!exists $data->{$k} and (UNIVERSAL::isa($r, 'HASH') and exists $r->{default})) { - - #$data->{$k} = $r->{default}; # TODO: This seems to fail when using oneOf and friends - } - elsif (exists $data->{$k}) { - my @e = $self->_validate($data->{$k}, _path($path, $k), $r); - push @errors, @e; - push @errors, $self->_validate_type_enum($data->{$k}, _path($path, $k), $r) - if $r->{enum} and !@e; - push @errors, $self->_validate_type_const($data->{$k}, _path($path, $k), $r) - if $r->{const} and !@e; - } + next unless exists $data->{$k}; + my @e = $self->_validate($data->{$k}, _path($path, $k), $r); + push @errors, @e; + push @errors, $self->_validate_type_enum($data->{$k}, _path($path, $k), $r) + if $r->{enum} and !@e; + push @errors, $self->_validate_type_const($data->{$k}, _path($path, $k), $r) + if $r->{const} and !@e; } } @@ -975,6 +970,17 @@ return 1; } +sub _is_uri_reference { + return unless defined $_[0]; + return unless $_[0] =~ m!^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?!; + + my ($scheme, $auth_host, $path, $query, $fragment) = map { $_ // '' } ($2, $4, $5, $7, $9); + return _invalid('Path cannot not start with //.') if $path =~ m!^//!; + return 1 if length $path; + return _is_uri($_[0]); + return 1; +} + sub _merge_errors { join ' ', map { my $e = $_; @@ -1011,7 +1017,7 @@ =head1 VERSION -2.07 +2.08 =head1 SYNOPSIS @@ -1096,11 +1102,13 @@ =over 2 -=item * JSON schema, draft 4 +=item * JSON schema, draft 4, 6, 7 Web page: L -C<$ref>: L +C<$ref>: L, +L, +L. =item * JSON schema for JSONPatch files @@ -1277,6 +1285,16 @@ L set to 3. (These settings are EXPERIMENTAL and might change without a warning) +=head2 version + + $int = $self->version; + $self = $self->version(7); + +Used to set the JSON Schema version to use. Will be set automatically when +using L, unless already set. + +Note that this attribute is EXPERIMENTAL and might change without a warning. + =head1 METHODS =head2 bundle diff -Nru libjson-validator-perl-2.07+dfsg/MANIFEST libjson-validator-perl-2.08+dfsg/MANIFEST --- libjson-validator-perl-2.07+dfsg/MANIFEST 2018-04-18 07:54:21.000000000 +0000 +++ libjson-validator-perl-2.08+dfsg/MANIFEST 2018-06-03 11:19:24.000000000 +0000 @@ -5,7 +5,9 @@ cpanfile lib/JSON/Validator.pm lib/JSON/Validator/cache/36d1bd12eeed51e86c8695bd8876a9df +lib/JSON/Validator/cache/3d35aac549d951f4cf9182ff47bff0b4 lib/JSON/Validator/cache/49c95b866e40f788892a7fb3c816b0e8 +lib/JSON/Validator/cache/4a31fe43be9e23ca9eb8d9e9faba8892 lib/JSON/Validator/cache/630949337805585c8e52deea27d11419 lib/JSON/Validator/cache/a0f5b4b4e75ea17fc09e88ec0343d148 lib/JSON/Validator/cache/ea34d47d4e060a1c3b12d2287aff89a7 @@ -62,8 +64,10 @@ t/draft4-tests/uniqueItems.json t/get.t t/Helper.pm -t/id-keyword.t +t/id-keyword-draft4.t +t/id-keyword-draft7.t t/invalid-ref.t +t/issue-103-one-of.t t/issue-22-duplicate-error-messages.t t/issue-42-cache-control.t t/issue-59-oneof-blessed-booleans.t diff -Nru libjson-validator-perl-2.07+dfsg/META.json libjson-validator-perl-2.08+dfsg/META.json --- libjson-validator-perl-2.07+dfsg/META.json 2018-04-18 07:54:21.000000000 +0000 +++ libjson-validator-perl-2.08+dfsg/META.json 2018-06-03 11:19:24.000000000 +0000 @@ -51,6 +51,6 @@ "web" : "https://github.com/jhthorsen/json-validator" } }, - "version" : "2.07", + "version" : "2.08", "x_serialization_backend" : "JSON::PP version 2.27400_02" } diff -Nru libjson-validator-perl-2.07+dfsg/META.yml libjson-validator-perl-2.08+dfsg/META.yml --- libjson-validator-perl-2.07+dfsg/META.yml 2018-04-18 07:54:21.000000000 +0000 +++ libjson-validator-perl-2.08+dfsg/META.yml 2018-06-03 11:19:24.000000000 +0000 @@ -23,5 +23,5 @@ bugtracker: https://github.com/jhthorsen/json-validator/issues homepage: https://github.com/jhthorsen/json-validator repository: https://github.com/jhthorsen/json-validator.git -version: '2.07' +version: '2.08' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' diff -Nru libjson-validator-perl-2.07+dfsg/t/bundle.t libjson-validator-perl-2.08+dfsg/t/bundle.t --- libjson-validator-perl-2.07+dfsg/t/bundle.t 2018-04-08 17:28:21.000000000 +0000 +++ libjson-validator-perl-2.08+dfsg/t/bundle.t 2018-06-02 09:36:22.000000000 +0000 @@ -7,6 +7,14 @@ my $validator = JSON::Validator->new; my $bundled; +# Bundle files +{ + local $ENV{JSON_VALIDATOR_CACHE_ANYWAYS} = 1; + $validator->_load_schema_from_url("http://json-schema.org/draft-04/schema"); + $validator->_load_schema_from_url("http://json-schema.org/draft-06/schema"); + $validator->_load_schema_from_url("http://json-schema.org/draft-07/schema"); +} + # Run multiple times to make sure _reset() works for my $n (1 .. 3) { note "[$n] replace=1"; diff -Nru libjson-validator-perl-2.07+dfsg/t/id-keyword-draft4.t libjson-validator-perl-2.08+dfsg/t/id-keyword-draft4.t --- libjson-validator-perl-2.07+dfsg/t/id-keyword-draft4.t 1970-01-01 00:00:00.000000000 +0000 +++ libjson-validator-perl-2.08+dfsg/t/id-keyword-draft4.t 2018-06-02 09:36:22.000000000 +0000 @@ -0,0 +1,82 @@ +use Mojo::Base -strict; +use Mojo::JSON 'encode_json'; +use Test::Mojo; +use Test::More; +use JSON::Validator; + +my ($base_url, $jv, $t, @e); + +use Mojolicious::Lite; +get '/invalid-fragment' => 'invalid-fragment'; +get '/invalid-relative' => 'invalid-relative'; +get '/relative-to-the-root' => 'relative-to-the-root'; + +$t = Test::Mojo->new; +$jv = JSON::Validator->new(ua => $t->ua); +$t->get_ok('/relative-to-the-root.json')->status_is(200); + +$base_url = $t->tx->req->url->to_abs->path('/'); +like $base_url, qr{^http}, 'got base_url to web server'; +is $jv->version, 4, 'default version'; +is $jv->_id_key, 'id', 'default id_key'; + +delete $jv->{version}; +eval { $jv->load_and_validate_schema("${base_url}relative-to-the-root.json") }; +ok !$@, "${base_url}relative-to-the-root.json" or diag $@; +is $jv->{version}, 4, 'detected version from draft-04'; + +my $schema = $jv->schema; +is $schema->get('/id'), 'http://example.com/relative-to-the-root.json', 'get /id'; +is $schema->get('/definitions/B/id'), 'b.json', 'id /definitions/B/id'; +is $schema->get('/definitions/B/definitions/X/id'), '#bx', 'id /definitions/B/definitions/X/id'; +is $schema->get('/definitions/B/definitions/Y/id'), 't/inner.json', + 'id /definitions/B/definitions/Y/id'; +is $schema->get('/definitions/C/definitions/X/id'), + 'urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f', 'id /definitions/C/definitions/X/id'; +is $schema->get('/definitions/C/definitions/Y/id'), '#cy', 'id /definitions/C/definitions/Y/id'; + +my $ref = $schema->get('/definitions/R1'); +ok $ref->{$_}, "got $_" for qw($ref %%fqn %%schema); +is encode_json($ref), '{"$ref":"b.json#bx"}', 'ref encode_json'; +$ref = tied %$ref; +is $ref->ref, 'b.json#bx', 'ref ref'; +is $ref->fqn, 'http://example.com/b.json#bx', 'ref fqn'; +ok $ref->schema->{definitions}{Y}, 'ref schema'; + +eval { $jv->load_and_validate_schema("${base_url}invalid-fragment.json") }; +like $@, qr{cannot have a fragment}, 'Root id cannot have a fragment' or diag $@; + +eval { $jv->load_and_validate_schema("${base_url}invalid-relative.json") }; +like $@, qr{cannot have a relative}, 'Root id cannot be relative' or diag $@; + +done_testing; + +__DATA__ +@@ invalid-fragment.json.ep +{"id": "http://example.com/invalid-fragment.json#cannot_be_here"} +@@ invalid-relative.json.ep +{"id": "whatever"} +@@ relative-to-the-root.json.ep +{ + "id": "http://example.com/relative-to-the-root.json", + "definitions": { + "A": { "id": "#a" }, + "B": { + "id": "b.json", + "definitions": { + "X": { "id": "#bx" }, + "Y": { "id": "t/inner.json" } + } + }, + "C": { + "id": "c.json", + "definitions": { + "X": { "id": "urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f" }, + "Y": { "id": "#cy" } + } + }, + "R1": { "$ref": "b.json#bx" }, + "R2": { "$ref": "#a" }, + "R3": { "$ref": "urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f" } + } +} diff -Nru libjson-validator-perl-2.07+dfsg/t/id-keyword-draft7.t libjson-validator-perl-2.08+dfsg/t/id-keyword-draft7.t --- libjson-validator-perl-2.07+dfsg/t/id-keyword-draft7.t 1970-01-01 00:00:00.000000000 +0000 +++ libjson-validator-perl-2.08+dfsg/t/id-keyword-draft7.t 2018-06-02 09:36:22.000000000 +0000 @@ -0,0 +1,46 @@ +use Mojo::Base -strict; +use Mojo::JSON 'encode_json'; +use Test::Mojo; +use Test::More; +use JSON::Validator; + +my ($base_url, $jv, $t, @e); + +use Mojolicious::Lite; +get '/person' => 'person'; +get '/invalid-relative' => 'invalid-relative'; + +$t = Test::Mojo->new; +$jv = JSON::Validator->new(ua => $t->ua); + +eval { + $t->get_ok('/person.json')->status_is(200); + $base_url = $t->tx->req->url->to_abs->path('/'); + $jv->load_and_validate_schema("${base_url}person.json", + {schema => 'http://json-schema.org/draft-07/schema'}); +}; +ok !$@, "${base_url}schema.json" or diag $@; + +is $jv->version, 7, 'detected version from draft-07'; +is $jv->_id_key, '$id', 'detected id_key from draft-07'; + +eval { $jv->load_and_validate_schema("${base_url}invalid-relative.json") }; +like $@, qr{cannot have a relative}, 'Root id cannot be relative' or diag $@; + +done_testing; + +__DATA__ +@@ invalid-relative.json.ep +{"$id": "whatever"} +@@ person.json.ep +{ + "$id": "http://example.com/person.json", + "definitions": { + "Person": { + "type": "object", + "properties": { + "firstName": { "type": "string" } + } + } + } +} diff -Nru libjson-validator-perl-2.07+dfsg/t/id-keyword.t libjson-validator-perl-2.08+dfsg/t/id-keyword.t --- libjson-validator-perl-2.07+dfsg/t/id-keyword.t 2018-03-10 13:35:33.000000000 +0000 +++ libjson-validator-perl-2.08+dfsg/t/id-keyword.t 1970-01-01 00:00:00.000000000 +0000 @@ -1,78 +0,0 @@ -use Mojo::Base -strict; -use Mojo::JSON 'encode_json'; -use Test::Mojo; -use Test::More; -use JSON::Validator; - -my ($base_url, $jv, $t, @e); - -use Mojolicious::Lite; -get '/invalid-fragment' => 'invalid-fragment'; -get '/invalid-relative' => 'invalid-relative'; -get '/relative-to-the-root' => 'relative-to-the-root'; - -$t = Test::Mojo->new; -$jv = JSON::Validator->new(ua => $t->ua); -$t->get_ok('/relative-to-the-root.json')->status_is(200); - -$base_url = $t->tx->req->url->to_abs->path('/'); -like $base_url, qr{^http}, 'got base_url to web server'; - -eval { $jv->load_and_validate_schema("${base_url}relative-to-the-root.json") }; -ok !$@, "${base_url}relative-to-the-root.json" or diag $@; - -my $schema = $jv->schema; -is $schema->get('/id'), 'http://example.com/relative-to-the-root.json', 'get /id'; -is $schema->get('/definitions/B/id'), 'b.json', 'id /definitions/B/id'; -is $schema->get('/definitions/B/definitions/X/id'), '#bx', 'id /definitions/B/definitions/X/id'; -is $schema->get('/definitions/B/definitions/Y/id'), 't/inner.json', - 'id /definitions/B/definitions/Y/id'; -is $schema->get('/definitions/C/definitions/X/id'), - 'urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f', 'id /definitions/C/definitions/X/id'; -is $schema->get('/definitions/C/definitions/Y/id'), '#cy', 'id /definitions/C/definitions/Y/id'; - -my $ref = $schema->get('/definitions/R1'); -ok $ref->{$_}, "got $_" for qw($ref %%fqn %%schema); -is encode_json($ref), '{"$ref":"b.json#bx"}', 'ref encode_json'; -$ref = tied %$ref; -is $ref->ref, 'b.json#bx', 'ref ref'; -is $ref->fqn, 'http://example.com/b.json#bx', 'ref fqn'; -ok $ref->schema->{definitions}{Y}, 'ref schema'; - -eval { $jv->load_and_validate_schema("${base_url}invalid-fragment.json") }; -like $@, qr{cannot have a fragment}, 'Root id cannot have a fragment' or diag $@; - -eval { $jv->load_and_validate_schema("${base_url}invalid-relative.json") }; -like $@, qr{cannot have a relative}, 'Root id cannot be relative' or diag $@; - -done_testing; - -__DATA__ -@@ invalid-fragment.json.ep -{"id": "http://example.com/invalid-fragment.json#cannot_be_here"} -@@ invalid-relative.json.ep -{"id": "whatever"} -@@ relative-to-the-root.json.ep -{ - "id": "http://example.com/relative-to-the-root.json", - "definitions": { - "A": { "id": "#a" }, - "B": { - "id": "b.json", - "definitions": { - "X": { "id": "#bx" }, - "Y": { "id": "t/inner.json" } - } - }, - "C": { - "id": "c.json", - "definitions": { - "X": { "id": "urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f" }, - "Y": { "id": "#cy" } - } - }, - "R1": { "$ref": "b.json#bx" }, - "R2": { "$ref": "#a" }, - "R3": { "$ref": "urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f" } - } -} diff -Nru libjson-validator-perl-2.07+dfsg/t/issue-103-one-of.t libjson-validator-perl-2.08+dfsg/t/issue-103-one-of.t --- libjson-validator-perl-2.07+dfsg/t/issue-103-one-of.t 1970-01-01 00:00:00.000000000 +0000 +++ libjson-validator-perl-2.08+dfsg/t/issue-103-one-of.t 2018-06-03 11:13:51.000000000 +0000 @@ -0,0 +1,69 @@ +use lib '.'; +use t::Helper; +use Test::More; + +my $validator = JSON::Validator->new->schema('data://main/example.json'); + +my @errors + = $validator->validate({who_id => 'WHO', expire => '2018-01-01', amount => 1000, desc => 'foo'}); + +like "@errors", qr{oneOf failed}, "missing sym is not detected (@errors)"; +note map {"\$errors[] = $_\n"} @errors; + +done_testing; + +__DATA__ +@@ example.json +{ + "oneOf": [ + {"$ref": "#/definitions/template_1"}, + {"$ref": "#/definitions/bar_header"} + ], + "definitions": { + "hwho":{ + "required": [ "who_id" ], + "properties": { + "who_id": { "type": "string" }, + "sub_who_id": { "type": "string" } + } + }, + "header": { + "required": [ "sym", "expire" ], + "properties": { + "sym": { "type": "string" }, + "expire": { "type": "string" } + } + }, + "foo_header": { + "allOf": [ + { "$ref": "#/definitions/header" }, + { + "required": [ "amount", "desc" ], + "properties": { + "amount": { "type": "integer" }, + "desc": { "enum": [ "foo" ] } + } + } + ] + }, + "template_1": { + "allOf": [ + { "$ref": "#/definitions/foo_header" }, + { "$ref": "#/definitions/hwho" }, + { "required": [ "template" ], "properties": { "template": { "type": "string" } } } + ] + }, + "bar_header" : { + "allOf": [ + { "$ref": "#/definitions/header" }, + { + "required": [ "amount", "desc" ], + "properties": { + "amount": { "type": "integer" }, + "desc": { "enum": [ "foo" ] } + } + } + ] + } + } +} diff -Nru libjson-validator-perl-2.07+dfsg/t/validate-id.t libjson-validator-perl-2.08+dfsg/t/validate-id.t --- libjson-validator-perl-2.07+dfsg/t/validate-id.t 2016-05-31 11:35:31.000000000 +0000 +++ libjson-validator-perl-2.08+dfsg/t/validate-id.t 2018-06-03 11:15:36.000000000 +0000 @@ -1,9 +1,10 @@ -use Mojo::Base -strict; -use Test::More; -use JSON::Validator; +use lib '.'; +use t::Helper; -my $validator = JSON::Validator->new; -my @errors = $validator->validate({id => 1}, {type => 'object'}); -is "@errors", "", "object"; +validate_ok {id => 1}, {type => 'object'}; + +validate_ok {id => 1, message => 'cannot exclude "id" #111'}, + {type => 'object', additionalProperties => 0, properties => {message => {type => "string"}}}, + E('/', 'Properties not allowed: id.'); done_testing;