Skip to content

Commit 6ffc3ee

Browse files
clean up handling of unevaluated* in terse format
1 parent dff0573 commit 6ffc3ee

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

Changes

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Revision history for JSON-Schema-Draft201909
22

33
{{$NEXT}}
4+
- further improvements to the "terse" output format
45

56
0.018 2020-12-07 18:22:07Z
67
- now can correctly evaluate schemas containing unevaluatedItems,

lib/JSON/Schema/Draft201909/Result.pm

+22-15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use MooX::HandlesVia;
1717
use JSON::Schema::Draft201909::Annotation;
1818
use JSON::Schema::Draft201909::Error;
1919
use JSON::PP ();
20+
use List::Util 1.50 'head';
2021
use namespace::clean;
2122

2223
use overload
@@ -70,35 +71,41 @@ sub format {
7071
}
7172
if ($style eq 'terse') {
7273
# we can also drop errors for unevaluatedItems, unevaluatedProperties
73-
# when there is another (non-discarded) error at the same instance location (indicating that
74-
# "unevaluated" is actually "unsuccessfully evaluated").
75-
my %instance_locations;
74+
# when there is another (non-discarded) error at the same instance location or parent keyword
75+
# location (indicating that "unevaluated" is actually "unsuccessfully evaluated").
76+
my (%instance_locations, %keyword_locations);
7677

7778
my @errors = grep {
7879
my ($keyword, $error) = ($_->keyword, $_->error);
7980

8081
my $keep = 0+!!(
8182
not $keyword
82-
or ($keyword =~ /^unevaluated(?:Items|Properties)$/
83-
and $error =~ /"$keyword" keyword present, but/)
8483
or (
85-
not grep $keyword eq $_, qw(allOf anyOf if then else dependentSchemas propertyNames)
84+
not grep $keyword eq $_, qw(allOf anyOf if then else dependentSchemas contains propertyNames)
8685
and ($keyword ne 'oneOf' or $error ne 'no subschemas are valid')
87-
and ($keyword ne 'items' or $error eq 'item not permitted')
86+
and ($keyword ne 'items' # list form of items (prefixItems)
87+
or $error eq 'item not permitted' and $_->keyword_location =~ m{/[0-9]+$})
8888
and ($keyword ne 'additionalItems' or $error eq 'additional item not permitted')
89-
and ($keyword ne 'unevaluatedItems'
90-
or ($error eq 'additional item not permitted' and not $instance_locations{$_->instance_location}{$keyword}))
9189
and (not grep $keyword eq $_, qw(properties patternProperties)
9290
or $error eq 'property not permitted')
9391
and ($keyword ne 'additionalProperties' or $error eq 'additional property not permitted'))
94-
and ($keyword ne 'unevaluatedProperties'
95-
or ($error eq 'additional property not permitted' and not $instance_locations{$_->instance_location}{$keyword}))
9692
);
9793

98-
++$instance_locations{$_->instance_location}->{unevaluatedItems}
99-
if $keep and $keyword and grep $keyword eq $_, qw(items additionalItems unevaluatedItems);
100-
++$instance_locations{$_->instance_location}->{unevaluatedProperties}
101-
if $keep and $keyword and grep $keyword eq $_, qw(properties additionalProperties patternProperties unevaluatedProperties);
94+
if ($keep and $keyword and $keyword =~ /^unevaluated(?:Items|Properties)$/
95+
and $error !~ /"$keyword" keyword present, but/) {
96+
my $parent_keyword_location = join('/', head(-1, split('/', $_->keyword_location)));
97+
my $parent_instance_location = join('/', head(-1, split('/', $_->instance_location)));
98+
99+
$keep = (
100+
(($keyword eq 'unevaluatedProperties' and $error eq 'additional property not permitted')
101+
or ($keyword eq 'unevaluatedItems' and $error eq 'additional item not permitted'))
102+
and not $instance_locations{$_->instance_location}
103+
and not grep m/^$parent_keyword_location/, keys %keyword_locations
104+
);
105+
}
106+
107+
++$instance_locations{$_->instance_location} if $keep;
108+
++$keyword_locations{$_->keyword_location} if $keep;
102109

103110
$keep;
104111
}

t/output_format.t

+3-11
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,9 @@ cmp_deeply(
305305
},
306306
# - "summary" error from /additionalProperties is omitted
307307
# - /properties/alpha/unevaluatedProperties error at /alpha is removed because it is also
308-
# covered at /properties/alpha
309-
{
310-
instanceLocation => '/beta',
311-
keywordLocation => '/unevaluatedProperties',
312-
error => 'additional property not permitted',
313-
},
314-
{
315-
instanceLocation => '/gamma',
316-
keywordLocation => '/unevaluatedProperties',
317-
error => 'additional property not permitted',
318-
},
308+
# covered at /properties/alpha
309+
# - /unevaluatedProperties errors at /beta, /gamma are removed because they are also covered
310+
# at /properties/{alpha,beta}
319311
# - "summary" error from /unevaluatedProperties is omitted
320312
{
321313
instanceLocation => '/zulu',

0 commit comments

Comments
 (0)