Skip to content

Commit 9777fd3

Browse files
authored
Validate allOf, oneOf and anyOf contains array (#1039)
1 parent 0fc43f0 commit 9777fd3

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public class AllOfValidator extends BaseJsonValidator {
3535

3636
public AllOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3737
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.ALL_OF, validationContext);
38+
if (!schemaNode.isArray()) {
39+
JsonType nodeType = TypeFactory.getValueNodeType(schemaNode, this.validationContext.getConfig());
40+
throw new JsonSchemaException(message().instanceNode(schemaNode)
41+
.instanceLocation(schemaLocation.getFragment())
42+
.messageKey("type")
43+
.arguments(nodeType.toString(), "array")
44+
.build());
45+
}
3846
int size = schemaNode.size();
3947
for (int i = 0; i < size; i++) {
4048
this.schemas.add(validationContext.newSchema(schemaLocation.append(i), evaluationPath.append(i),

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ public class AnyOfValidator extends BaseJsonValidator {
3737

3838
public AnyOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3939
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.ANY_OF, validationContext);
40+
if (!schemaNode.isArray()) {
41+
JsonType nodeType = TypeFactory.getValueNodeType(schemaNode, this.validationContext.getConfig());
42+
throw new JsonSchemaException(message().instanceNode(schemaNode)
43+
.instanceLocation(schemaLocation.getFragment())
44+
.messageKey("type")
45+
.arguments(nodeType.toString(), "array")
46+
.build());
47+
}
4048
int size = schemaNode.size();
4149
for (int i = 0; i < size; i++) {
4250
this.schemas.add(validationContext.newSchema(schemaLocation.append(i), evaluationPath.append(i),

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ public class OneOfValidator extends BaseJsonValidator {
3636

3737
public OneOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3838
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.ONE_OF, validationContext);
39+
if (!schemaNode.isArray()) {
40+
JsonType nodeType = TypeFactory.getValueNodeType(schemaNode, this.validationContext.getConfig());
41+
throw new JsonSchemaException(message().instanceNode(schemaNode)
42+
.instanceLocation(schemaLocation.getFragment())
43+
.messageKey("type")
44+
.arguments(nodeType.toString(), "array")
45+
.build());
46+
}
3947
int size = schemaNode.size();
4048
for (int i = 0; i < size; i++) {
4149
JsonNode childNode = schemaNode.get(i);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2024 the original author or authors.
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+
package com.networknt.schema;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertThrows;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import com.networknt.schema.SpecVersion.VersionFlag;
24+
25+
public class AllOfValidatorTest {
26+
@Test
27+
void invalidTypeShouldThrowJsonSchemaException() {
28+
String schemaData = "{\r\n"
29+
+ " \"$defs\": {\r\n"
30+
+ " \"User\": true\r\n"
31+
+ " },\r\n"
32+
+ " \"allOf\": {\r\n"
33+
+ " \"$ref\": \"#/defs/User\"\r\n"
34+
+ " }\r\n"
35+
+ "}";
36+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012);
37+
JsonSchemaException ex = assertThrows(JsonSchemaException.class, () -> factory.getSchema(schemaData));
38+
assertEquals("type", ex.getValidationMessage().getMessageKey());
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2024 the original author or authors.
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+
package com.networknt.schema;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertThrows;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import com.networknt.schema.SpecVersion.VersionFlag;
24+
25+
public class AnyOfValidatorTest {
26+
@Test
27+
void invalidTypeShouldThrowJsonSchemaException() {
28+
String schemaData = "{\r\n"
29+
+ " \"$defs\": {\r\n"
30+
+ " \"User\": true\r\n"
31+
+ " },\r\n"
32+
+ " \"anyOf\": {\r\n"
33+
+ " \"$ref\": \"#/defs/User\"\r\n"
34+
+ " }\r\n"
35+
+ "}";
36+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012);
37+
JsonSchemaException ex = assertThrows(JsonSchemaException.class, () -> factory.getSchema(schemaData));
38+
assertEquals("type", ex.getValidationMessage().getMessageKey());
39+
}
40+
}

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

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

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
2021

2122
import java.util.List;
2223
import java.util.Set;
@@ -123,4 +124,19 @@ void oneOfZero() {
123124
assertEquals("$.test", assertions.get(3).getInstanceLocation().toString());
124125
assertEquals("$.oneOf[2].additionalProperties.type", assertions.get(3).getEvaluationPath().toString());
125126
}
127+
128+
@Test
129+
void invalidTypeShouldThrowJsonSchemaException() {
130+
String schemaData = "{\r\n"
131+
+ " \"$defs\": {\r\n"
132+
+ " \"User\": true\r\n"
133+
+ " },\r\n"
134+
+ " \"oneOf\": {\r\n"
135+
+ " \"$ref\": \"#/defs/User\"\r\n"
136+
+ " }\r\n"
137+
+ "}";
138+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012);
139+
JsonSchemaException ex = assertThrows(JsonSchemaException.class, () -> factory.getSchema(schemaData));
140+
assertEquals("type", ex.getValidationMessage().getMessageKey());
141+
}
126142
}

0 commit comments

Comments
 (0)