Skip to content

Commit 5cc14f9

Browse files
authored
Merge pull request #252 from Asamsig/feature/226_contains
Issue-226 ContainsValidator
2 parents a03428e + b658913 commit 5cc14f9

File tree

5 files changed

+74
-4
lines changed

5 files changed

+74
-4
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2016 Network New Technologies Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.networknt.schema;
18+
19+
import com.fasterxml.jackson.databind.JsonNode;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
import java.util.Collections;
24+
import java.util.Set;
25+
26+
public class ContainsValidator extends BaseJsonValidator implements JsonValidator {
27+
private static final Logger logger = LoggerFactory.getLogger(ContainsValidator.class);
28+
29+
private JsonSchema schema;
30+
31+
public ContainsValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
32+
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.CONTAINS, validationContext);
33+
if (schemaNode.isObject() || schemaNode.isBoolean()) {
34+
schema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema);
35+
}
36+
37+
parseErrorCode(getValidatorType().getErrorCodeKey());
38+
}
39+
40+
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
41+
debug(logger, node, rootNode, at);
42+
43+
44+
if (!node.isArray()) {
45+
// ignores non-arrays
46+
return Collections.emptySet();
47+
}
48+
49+
if (node.isEmpty()) {
50+
// Array was empty
51+
return buildErrorMessageSet(at);
52+
} else if (node.isArray()) {
53+
int i = 0;
54+
for (JsonNode n : node) {
55+
if (schema.validate(n, rootNode, at + "[" + i + "]").isEmpty()) {
56+
//Short circuit on first success
57+
return Collections.emptySet();
58+
}
59+
i++;
60+
}
61+
// None of the elements in the array satisfies the schema
62+
return buildErrorMessageSet(at);
63+
}
64+
65+
return Collections.emptySet();
66+
}
67+
68+
private Set<ValidationMessage> buildErrorMessageSet(String at) {
69+
return Collections.singleton(buildValidationMessage(at, schema.getSchemaNode().toString()));
70+
}
71+
72+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
7171
EXCLUSIVE_MINIMUM("exclusiveMinimum", "1039", new MessageFormat("{0}: must have a exclusive minimum value of {1}"), ExclusiveMinimumValidator.class, 14),
7272
TRUE("true", "1040", null, TrueValidator.class, 14),
7373
FALSE("false", "1041", new MessageFormat("Boolean schema false is not valid"), FalseValidator.class, 14),
74-
CONST("const", "1042", new MessageFormat("{0}: must be a constant value {1}"), ConstValidator.class, 14);
74+
CONST("const", "1042", new MessageFormat("{0}: must be a constant value {1}"), ConstValidator.class, 14),
75+
CONTAINS("contains", "1043", new MessageFormat("{0}: does not contain an element that passes these validations: {1}"), ContainsValidator.class, 14);
7576

7677
private static Map<String, ValidatorTypeCode> constants = new HashMap<String, ValidatorTypeCode>();
7778
private static SpecVersion specVersion = new SpecVersion();

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ public void testConstValidator() throws Exception {
262262
}
263263

264264
@Test
265-
@Ignore
266265
public void testContainsValidator() throws Exception {
267266
runTestFile("draft2019-09/contains.json");
268267
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ public void testConstValidator() throws Exception {
160160
}
161161

162162
@Test
163-
@Ignore
164163
public void testContainsValidator() throws Exception {
165164
runTestFile("draft6/contains.json");
166165
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ public void testConstValidator() throws Exception {
256256
}
257257

258258
@Test
259-
@Ignore
260259
public void testContainsValidator() throws Exception {
261260
runTestFile("draft7/contains.json");
262261
}

0 commit comments

Comments
 (0)