Skip to content

Commit 4fcfc6a

Browse files
fduttonFaron Dutton
andauthored
Supports fail-fast when a pattern does not match. (#795)
Resolves #792 Co-authored-by: Faron Dutton <[email protected]>
1 parent a4ce435 commit 4fcfc6a

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,21 @@ public PatternValidator(String schemaPath, JsonNode schemaNode, JsonSchema paren
3535

3636
this.pattern = Optional.ofNullable(schemaNode).filter(JsonNode::isTextual).map(JsonNode::textValue).orElse(null);
3737
try {
38-
this.compiledPattern = RegularExpression.compile(pattern, validationContext);
38+
this.compiledPattern = RegularExpression.compile(this.pattern, validationContext);
3939
} catch (RuntimeException e) {
4040
e.setStackTrace(new StackTraceElement[0]);
41-
logger.error("Failed to compile pattern '{}': {}", pattern, e.getMessage());
41+
logger.error("Failed to compile pattern '{}': {}", this.pattern, e.getMessage());
4242
throw e;
4343
}
4444
this.validationContext = validationContext;
4545
parseErrorCode(getValidatorType().getErrorCodeKey());
4646
}
4747

4848
private boolean matches(String value) {
49-
return compiledPattern.matches(value);
49+
return this.compiledPattern.matches(value);
5050
}
5151

52+
@Override
5253
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
5354
debug(logger, node, rootNode, at);
5455

@@ -59,10 +60,13 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
5960

6061
try {
6162
if (!matches(node.asText())) {
62-
return Collections.singleton(buildValidationMessage(at, pattern));
63+
return Collections.singleton(buildValidationMessage(at, this.pattern));
6364
}
65+
} catch (JsonSchemaException e) {
66+
throw e;
6467
} catch (RuntimeException e) {
65-
logger.error("Failed to apply pattern '{}' at {}: {}", pattern, at, e.getMessage());
68+
logger.error("Failed to apply pattern '{}' at {}: {}", this.pattern, at, e.getMessage());
69+
throw e;
6670
}
6771

6872
return Collections.emptySet();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.networknt.schema;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.JsonNode;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
public class Issue792 {
11+
12+
@Test
13+
void test() throws JsonProcessingException {
14+
JsonSchemaFactory schemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
15+
16+
String schemaDef =
17+
"{\n" +
18+
" \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n" +
19+
" \"$id\": \"http://some-id/\",\n" +
20+
" \"title\": \"title\",\n" +
21+
" \"type\": \"object\",\n" +
22+
" \"properties\": {\n" +
23+
" \"field\": {\n" +
24+
" \"type\": \"string\",\n" +
25+
" \"pattern\": \"^[A-Z]{2}$\"\n" +
26+
" }\n" +
27+
" }\n" +
28+
"}";
29+
30+
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
31+
config.setTypeLoose(false);
32+
config.setFailFast(true);
33+
34+
JsonSchema jsonSchema = schemaFactory.getSchema(schemaDef, config);
35+
JsonNode jsonNode = new ObjectMapper().readTree("{\"field\": \"pattern-violation\"}");
36+
37+
//this works with 1.0.81, but not with 1.0.82+
38+
assertThrows(JsonSchemaException.class, () -> jsonSchema.validate(jsonNode));
39+
}
40+
}

0 commit comments

Comments
 (0)