-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dependentRequired and dependentSchemas validators (#479)
- Loading branch information
Showing
8 changed files
with
420 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (c) 2016 Network New Technologies Inc. | ||
* | ||
* 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 com.fasterxml.jackson.databind.JsonNode; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.*; | ||
|
||
public class DependentRequired extends BaseJsonValidator implements JsonValidator { | ||
private static final Logger logger = LoggerFactory.getLogger(DependentRequired.class); | ||
private final Map<String, List<String>> propertyDependencies = new HashMap<String, List<String>>(); | ||
|
||
public DependentRequired(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) { | ||
|
||
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.DEPENDENT_REQUIRED, validationContext); | ||
|
||
for (Iterator<String> it = schemaNode.fieldNames(); it.hasNext(); ) { | ||
String pname = it.next(); | ||
JsonNode pvalue = schemaNode.get(pname); | ||
if (pvalue.isArray()) { | ||
List<String> dependencies = propertyDependencies.computeIfAbsent(pname, k -> new ArrayList<>()); | ||
|
||
for (int i = 0; i < pvalue.size(); i++) { | ||
dependencies.add(pvalue.get(i).asText()); | ||
} | ||
} | ||
} | ||
|
||
parseErrorCode(getValidatorType().getErrorCodeKey()); | ||
} | ||
|
||
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) { | ||
debug(logger, node, rootNode, at); | ||
|
||
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>(); | ||
|
||
for (Iterator<String> it = node.fieldNames(); it.hasNext(); ) { | ||
String pname = it.next(); | ||
List<String> dependencies = propertyDependencies.get(pname); | ||
if (dependencies != null && !dependencies.isEmpty()) { | ||
for (String field : dependencies) { | ||
if (node.get(field) == null) { | ||
errors.add(buildValidationMessage(at, propertyDependencies.toString())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return Collections.unmodifiableSet(errors); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (c) 2016 Network New Technologies Inc. | ||
* | ||
* 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 com.fasterxml.jackson.databind.JsonNode; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.*; | ||
|
||
public class DependentSchemas extends BaseJsonValidator implements JsonValidator { | ||
private static final Logger logger = LoggerFactory.getLogger(DependentSchemas.class); | ||
private final Map<String, JsonSchema> schemaDependencies = new HashMap<String, JsonSchema>(); | ||
|
||
public DependentSchemas(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) { | ||
|
||
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.DEPENDENT_SCHEMAS, validationContext); | ||
|
||
for (Iterator<String> it = schemaNode.fieldNames(); it.hasNext(); ) { | ||
String pname = it.next(); | ||
JsonNode pvalue = schemaNode.get(pname); | ||
if (pvalue.isObject() || pvalue.isBoolean()) { | ||
schemaDependencies.put(pname, new JsonSchema(validationContext, pname, parentSchema.getCurrentUri(), pvalue, parentSchema)); | ||
} | ||
} | ||
|
||
parseErrorCode(getValidatorType().getErrorCodeKey()); | ||
} | ||
|
||
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) { | ||
debug(logger, node, rootNode, at); | ||
|
||
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>(); | ||
|
||
for (Iterator<String> it = node.fieldNames(); it.hasNext(); ) { | ||
String pname = it.next(); | ||
JsonSchema schema = schemaDependencies.get(pname); | ||
if (schema != null) { | ||
errors.addAll(schema.validate(node, rootNode, at)); | ||
} | ||
} | ||
|
||
return Collections.unmodifiableSet(errors); | ||
} | ||
|
||
@Override | ||
public void preloadJsonSchema() { | ||
preloadJsonSchemas(schemaDependencies.values()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
[ | ||
{ | ||
"description": "single dependency", | ||
"schema": {"dependentRequired": {"bar": ["foo"]}}, | ||
"tests": [ | ||
{ | ||
"description": "neither", | ||
"data": {}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "nondependant", | ||
"data": {"foo": 1}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "with dependency", | ||
"data": {"foo": 1, "bar": 2}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "missing dependency", | ||
"data": {"bar": 2}, | ||
"valid": false | ||
}, | ||
{ | ||
"description": "ignores arrays", | ||
"data": ["bar"], | ||
"valid": true | ||
}, | ||
{ | ||
"description": "ignores strings", | ||
"data": "foobar", | ||
"valid": true | ||
}, | ||
{ | ||
"description": "ignores other non-objects", | ||
"data": 12, | ||
"valid": true | ||
} | ||
] | ||
}, | ||
{ | ||
"description": "empty dependents", | ||
"schema": {"dependentRequired": {"bar": []}}, | ||
"tests": [ | ||
{ | ||
"description": "empty object", | ||
"data": {}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "object with one property", | ||
"data": {"bar": 2}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "non-object is valid", | ||
"data": 1, | ||
"valid": true | ||
} | ||
] | ||
}, | ||
{ | ||
"description": "multiple dependents required", | ||
"schema": {"dependentRequired": {"quux": ["foo", "bar"]}}, | ||
"tests": [ | ||
{ | ||
"description": "neither", | ||
"data": {}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "nondependants", | ||
"data": {"foo": 1, "bar": 2}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "with dependencies", | ||
"data": {"foo": 1, "bar": 2, "quux": 3}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "missing dependency", | ||
"data": {"foo": 1, "quux": 2}, | ||
"valid": false | ||
}, | ||
{ | ||
"description": "missing other dependency", | ||
"data": {"bar": 1, "quux": 2}, | ||
"valid": false | ||
}, | ||
{ | ||
"description": "missing both dependencies", | ||
"data": {"quux": 1}, | ||
"valid": false | ||
} | ||
] | ||
}, | ||
{ | ||
"description": "dependencies with escaped characters", | ||
"schema": { | ||
"dependentRequired": { | ||
"foo\nbar": ["foo\rbar"], | ||
"foo\"bar": ["foo'bar"] | ||
} | ||
}, | ||
"tests": [ | ||
{ | ||
"description": "CRLF", | ||
"data": { | ||
"foo\nbar": 1, | ||
"foo\rbar": 2 | ||
}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "quoted quotes", | ||
"data": { | ||
"foo'bar": 1, | ||
"foo\"bar": 2 | ||
}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "CRLF missing dependent", | ||
"data": { | ||
"foo\nbar": 1, | ||
"foo": 2 | ||
}, | ||
"valid": false | ||
}, | ||
{ | ||
"description": "quoted quotes missing dependent", | ||
"data": { | ||
"foo\"bar": 2 | ||
}, | ||
"valid": false | ||
} | ||
] | ||
} | ||
] |
Oops, something went wrong.