Skip to content

Commit a5df748

Browse files
Fix handling of empty pattern in "pattern", "patternProperties" keywords
perldoc perlop: "If the PATTERN evaluates to the empty string, the last successfully matched regular expression is used instead." This could lead to bizarre failures if there was a successful match in a containing scope to the evaluation.
1 parent ad5cc15 commit a5df748

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

Changes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Revision history for JSON-Schema-Tiny
22

33
{{$NEXT}}
4+
- fix bad handling of empty patterns in "pattern",
5+
"patternProperties" keywords
46

57
0.020 2023-04-12 03:28:28Z
68
- update test results and exemptions for TJSA 1.019

lib/JSON/Schema/Tiny.pm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ sub _eval_keyword_pattern ($data, $schema, $state) {
520520
assert_pattern($state, $schema->{pattern});
521521

522522
return 1 if not is_type('string', $data);
523-
return 1 if $data =~ m/$schema->{pattern}/;
523+
return 1 if $data =~ m/(?:$schema->{pattern})/;
524524
return E($state, 'pattern does not match');
525525
}
526526

@@ -950,7 +950,7 @@ sub _eval_keyword_patternProperties ($data, $schema, $state) {
950950

951951
my $valid = 1;
952952
foreach my $property_pattern (sort keys $schema->{patternProperties}->%*) {
953-
foreach my $property (sort grep m/$property_pattern/, keys %$data) {
953+
foreach my $property (sort grep m/(?:$property_pattern)/, keys %$data) {
954954
if (is_type('boolean', $schema->{patternProperties}{$property_pattern})) {
955955
next if $schema->{patternProperties}{$property_pattern};
956956
$valid = E({ %$state, data_path => jsonp($state->{data_path}, $property),
@@ -979,7 +979,7 @@ sub _eval_keyword_additionalProperties ($data, $schema, $state) {
979979
foreach my $property (sort keys %$data) {
980980
next if exists $schema->{properties} and exists $schema->{properties}{$property};
981981
next if exists $schema->{patternProperties}
982-
and any { $property =~ /$_/ } keys $schema->{patternProperties}->%*;
982+
and any { $property =~ /(?:$_)/ } keys $schema->{patternProperties}->%*;
983983

984984
if (is_type('boolean', $schema->{additionalProperties})) {
985985
next if $schema->{additionalProperties};

t/pattern.t

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,30 @@ $tests->($letter, 'upgraded');
4949
utf8::downgrade($letter);
5050
$tests->($letter, 'downgraded');
5151

52+
subtest 'empty pattern' => sub {
53+
# create a "last successful match" in a containing scope
54+
my $str = "furble" =~ s/fur/meow/r;
55+
56+
cmp_deeply(
57+
evaluate('hello', { pattern => '' }),
58+
{ valid => true },
59+
'empty pattern in "pattern" will correctly match',
60+
);
61+
62+
# create a new "last successful match"
63+
$str = "furble" =~ s/fur/meow/r;
64+
65+
cmp_deeply(
66+
evaluate(
67+
{ alpha => 'hello' },
68+
{
69+
patternProperties => { '' => true },
70+
additionalProperties => false,
71+
},
72+
),
73+
{ valid => true },
74+
'empty pattern in "patternProperties" will correctly match',
75+
);
76+
};
77+
5278
done_testing;

0 commit comments

Comments
 (0)