Skip to content

Commit 636a346

Browse files
fduttonfdutton
andauthored
Adds support for validating regular expressions (#706)
Co-authored-by: fdutton <fdutton@noreply>
1 parent 1f3ae93 commit 636a346

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

src/main/java/com/networknt/schema/JsonMetaSchema.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.networknt.schema;
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
20+
import com.networknt.schema.format.RegexFormat;
2021
import com.networknt.schema.format.TimeFormat;
2122
import com.networknt.schema.utils.StringUtils;
2223
import org.slf4j.Logger;
@@ -91,6 +92,7 @@ static PatternFormat pattern(String name, String regex) {
9192
COMMON_BUILTIN_FORMATS.add(pattern("phone", "^\\+(?:[0-9] ?){6,14}[0-9]$"));
9293
COMMON_BUILTIN_FORMATS.add(pattern("utc-millisec", "^[0-9]+(\\.?[0-9]+)?$"));
9394
COMMON_BUILTIN_FORMATS.add(pattern("style", "\\s*(.+?):\\s*([^;]+);?"));
95+
COMMON_BUILTIN_FORMATS.add(new RegexFormat());
9496
}
9597

9698
public static class Builder {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* https://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package com.networknt.schema.format;
15+
16+
import com.networknt.schema.AbstractFormat;
17+
18+
import java.util.regex.Pattern;
19+
import java.util.regex.PatternSyntaxException;
20+
21+
/**
22+
* Validates that a value is a valid regex.
23+
*/
24+
public class RegexFormat extends AbstractFormat {
25+
26+
public RegexFormat() {
27+
super("regex", "must be a valid regex");
28+
}
29+
30+
@Override
31+
public boolean matches(String value) {
32+
if (null == value) return true;
33+
try {
34+
Pattern.compile(value);
35+
return true;
36+
37+
} catch (PatternSyntaxException e) {
38+
return false;
39+
}
40+
}
41+
42+
}

src/test/java/com/networknt/schema/JsonSchemaTestSuiteTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,9 @@ private void disableV202012Tests() {
8787
disabled.add(Paths.get("src/test/suite/tests/draft2020-12/optional/format/iri-reference.json"));
8888
disabled.add(Paths.get("src/test/suite/tests/draft2020-12/optional/format/iri.json"));
8989
disabled.add(Paths.get("src/test/suite/tests/draft2020-12/optional/format/json-pointer.json"));
90-
disabled.add(Paths.get("src/test/suite/tests/draft2020-12/optional/format/regex.json"));
9190
disabled.add(Paths.get("src/test/suite/tests/draft2020-12/optional/format/relative-json-pointer.json"));
9291
disabled.add(Paths.get("src/test/suite/tests/draft2020-12/optional/format/uri-reference.json"));
9392
disabled.add(Paths.get("src/test/suite/tests/draft2020-12/optional/format/uri-template.json"));
94-
disabled.add(Paths.get("src/test/suite/tests/draft2020-12/optional/format/uri.json"));
9593
disabled.add(Paths.get("src/test/suite/tests/draft2020-12/ref.json"));
9694
disabled.add(Paths.get("src/test/suite/tests/draft2020-12/refRemote.json"));
9795
disabled.add(Paths.get("src/test/suite/tests/draft2020-12/type.json"));
@@ -114,7 +112,6 @@ private void disableV201909Tests() {
114112
disabled.add(Paths.get("src/test/suite/tests/draft2019-09/optional/format/iri-reference.json"));
115113
disabled.add(Paths.get("src/test/suite/tests/draft2019-09/optional/format/iri.json"));
116114
disabled.add(Paths.get("src/test/suite/tests/draft2019-09/optional/format/json-pointer.json"));
117-
disabled.add(Paths.get("src/test/suite/tests/draft2019-09/optional/format/regex.json"));
118115
disabled.add(Paths.get("src/test/suite/tests/draft2019-09/optional/format/relative-json-pointer.json"));
119116
disabled.add(Paths.get("src/test/suite/tests/draft2019-09/optional/format/uri-reference.json"));
120117
disabled.add(Paths.get("src/test/suite/tests/draft2019-09/optional/format/uri-template.json"));
@@ -141,11 +138,9 @@ private void disableV7Tests() {
141138
disabled.add(Paths.get("src/test/suite/tests/draft7/optional/format/iri-reference.json"));
142139
disabled.add(Paths.get("src/test/suite/tests/draft7/optional/format/iri.json"));
143140
disabled.add(Paths.get("src/test/suite/tests/draft7/optional/format/json-pointer.json"));
144-
disabled.add(Paths.get("src/test/suite/tests/draft7/optional/format/regex.json"));
145141
disabled.add(Paths.get("src/test/suite/tests/draft7/optional/format/relative-json-pointer.json"));
146142
disabled.add(Paths.get("src/test/suite/tests/draft7/optional/format/uri-reference.json"));
147143
disabled.add(Paths.get("src/test/suite/tests/draft7/optional/format/uri-template.json"));
148-
disabled.add(Paths.get("src/test/suite/tests/draft7/optional/format/uri.json"));
149144
disabled.add(Paths.get("src/test/suite/tests/draft7/ref.json"));
150145
disabled.add(Paths.get("src/test/suite/tests/draft7/refRemote.json"));
151146
disabled.add(Paths.get("src/test/suite/tests/draft7/type.json"));

0 commit comments

Comments
 (0)