Skip to content

Commit

Permalink
Validate allOf, oneOf and anyOf contains array (#1039)
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-tay authored May 14, 2024
1 parent 0fc43f0 commit 9777fd3
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/networknt/schema/AllOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public class AllOfValidator extends BaseJsonValidator {

public AllOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.ALL_OF, validationContext);
if (!schemaNode.isArray()) {
JsonType nodeType = TypeFactory.getValueNodeType(schemaNode, this.validationContext.getConfig());
throw new JsonSchemaException(message().instanceNode(schemaNode)
.instanceLocation(schemaLocation.getFragment())
.messageKey("type")
.arguments(nodeType.toString(), "array")
.build());
}
int size = schemaNode.size();
for (int i = 0; i < size; i++) {
this.schemas.add(validationContext.newSchema(schemaLocation.append(i), evaluationPath.append(i),
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/networknt/schema/AnyOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public class AnyOfValidator extends BaseJsonValidator {

public AnyOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.ANY_OF, validationContext);
if (!schemaNode.isArray()) {
JsonType nodeType = TypeFactory.getValueNodeType(schemaNode, this.validationContext.getConfig());
throw new JsonSchemaException(message().instanceNode(schemaNode)
.instanceLocation(schemaLocation.getFragment())
.messageKey("type")
.arguments(nodeType.toString(), "array")
.build());
}
int size = schemaNode.size();
for (int i = 0; i < size; i++) {
this.schemas.add(validationContext.newSchema(schemaLocation.append(i), evaluationPath.append(i),
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/networknt/schema/OneOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public class OneOfValidator extends BaseJsonValidator {

public OneOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.ONE_OF, validationContext);
if (!schemaNode.isArray()) {
JsonType nodeType = TypeFactory.getValueNodeType(schemaNode, this.validationContext.getConfig());
throw new JsonSchemaException(message().instanceNode(schemaNode)
.instanceLocation(schemaLocation.getFragment())
.messageKey("type")
.arguments(nodeType.toString(), "array")
.build());
}
int size = schemaNode.size();
for (int i = 0; i < size; i++) {
JsonNode childNode = schemaNode.get(i);
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/com/networknt/schema/AllOfValidatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.networknt.schema;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

import com.networknt.schema.SpecVersion.VersionFlag;

public class AllOfValidatorTest {
@Test
void invalidTypeShouldThrowJsonSchemaException() {
String schemaData = "{\r\n"
+ " \"$defs\": {\r\n"
+ " \"User\": true\r\n"
+ " },\r\n"
+ " \"allOf\": {\r\n"
+ " \"$ref\": \"#/defs/User\"\r\n"
+ " }\r\n"
+ "}";
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012);
JsonSchemaException ex = assertThrows(JsonSchemaException.class, () -> factory.getSchema(schemaData));
assertEquals("type", ex.getValidationMessage().getMessageKey());
}
}
40 changes: 40 additions & 0 deletions src/test/java/com/networknt/schema/AnyOfValidatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.networknt.schema;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

import com.networknt.schema.SpecVersion.VersionFlag;

public class AnyOfValidatorTest {
@Test
void invalidTypeShouldThrowJsonSchemaException() {
String schemaData = "{\r\n"
+ " \"$defs\": {\r\n"
+ " \"User\": true\r\n"
+ " },\r\n"
+ " \"anyOf\": {\r\n"
+ " \"$ref\": \"#/defs/User\"\r\n"
+ " }\r\n"
+ "}";
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012);
JsonSchemaException ex = assertThrows(JsonSchemaException.class, () -> factory.getSchema(schemaData));
assertEquals("type", ex.getValidationMessage().getMessageKey());
}
}
16 changes: 16 additions & 0 deletions src/test/java/com/networknt/schema/OneOfValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.networknt.schema;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -123,4 +124,19 @@ void oneOfZero() {
assertEquals("$.test", assertions.get(3).getInstanceLocation().toString());
assertEquals("$.oneOf[2].additionalProperties.type", assertions.get(3).getEvaluationPath().toString());
}

@Test
void invalidTypeShouldThrowJsonSchemaException() {
String schemaData = "{\r\n"
+ " \"$defs\": {\r\n"
+ " \"User\": true\r\n"
+ " },\r\n"
+ " \"oneOf\": {\r\n"
+ " \"$ref\": \"#/defs/User\"\r\n"
+ " }\r\n"
+ "}";
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012);
JsonSchemaException ex = assertThrows(JsonSchemaException.class, () -> factory.getSchema(schemaData));
assertEquals("type", ex.getValidationMessage().getMessageKey());
}
}

0 comments on commit 9777fd3

Please sign in to comment.