-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary of changes: - Fix the test resources introduced by #783 by moving the `regex` fields, such that the test framework does not skip them with a "Not a valid test case" message. - Revert the changes introduced by #815, as those are simply incorrect. - Extend the test coverage introduced by #815 by (a) updating the test regexes to match their intended semantics and (b) include a few negative test cases. - Partially revert the change introduced by #783: the use of `Matcher#find()` is correct, but the `hasStartAnchor` and `hasEndAnchor` logic introduces more bugs than the issue it aims to solve. - Extend the test coverage introduced by #783, by introducing regexes that are not covered by the `hasStartAnchor`/`hasEndAnchor` logic. - Update the Joni regular expression integration such that it passes more of the test cases. - Disable the "trailing newline" test cases, as these are currently not handled correctly by either regex implementation.
- Loading branch information
1 parent
9ed6dc2
commit 0924dfe
Showing
5 changed files
with
99 additions
and
40 deletions.
There are no files selected for viewing
21 changes: 2 additions & 19 deletions
21
src/main/java/com/networknt/schema/regex/JDKRegularExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,16 @@ | ||
package com.networknt.schema.regex; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
class JDKRegularExpression implements RegularExpression { | ||
private final Pattern pattern; | ||
private final boolean hasStartAnchor; | ||
private final boolean hasEndAnchor; | ||
|
||
JDKRegularExpression(String regex) { | ||
// The patterns in JSON Schema are not implicitly anchored so we must | ||
// use Matcher.find(). However, this method does not honor the end | ||
// anchor when immediately preceded by a quantifier (e.g., ?, *, +). | ||
// To make this work in all cases, we wrap the pattern in a group. | ||
this.hasStartAnchor = '^' == regex.charAt(0); | ||
this.hasEndAnchor = '$' == regex.charAt(regex.length() - 1); | ||
String pattern = regex; | ||
if (this.hasEndAnchor) { | ||
pattern = pattern.substring(this.hasStartAnchor ? 1 : 0, pattern.length() - 1); | ||
pattern = '(' + pattern + ")$"; | ||
if (this.hasStartAnchor) pattern = '^' + pattern; | ||
} | ||
this.pattern = Pattern.compile(pattern); | ||
this.pattern = Pattern.compile(regex); | ||
} | ||
|
||
@Override | ||
public boolean matches(String value) { | ||
Matcher matcher = this.pattern.matcher(value); | ||
return matcher.find() && (!this.hasStartAnchor || 0 == matcher.start()) && (!this.hasEndAnchor || matcher.end() == value.length()); | ||
return this.pattern.matcher(value).find(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters