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.*; 24 25 /** 26 * {@link JsonValidator} for dependentRequired. 27 */ 28 public class DependentRequired extends BaseJsonValidator implements JsonValidator { 29 private static final Logger logger = LoggerFactory.getLogger(DependentRequired.class); 30 private final Map<String, List<String>> propertyDependencies = new HashMap<String, List<String>>(); 31 DependentRequired(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext)32 public DependentRequired(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) { 33 34 super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.DEPENDENT_REQUIRED, validationContext); 35 36 for (Iterator<String> it = schemaNode.fieldNames(); it.hasNext(); ) { 37 String pname = it.next(); 38 JsonNode pvalue = schemaNode.get(pname); 39 if (pvalue.isArray()) { 40 List<String> dependencies = propertyDependencies.computeIfAbsent(pname, k -> new ArrayList<>()); 41 42 for (int i = 0; i < pvalue.size(); i++) { 43 dependencies.add(pvalue.get(i).asText()); 44 } 45 } 46 } 47 } 48 validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation)49 public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation) { 50 debug(logger, node, rootNode, instanceLocation); 51 52 Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>(); 53 54 for (Iterator<String> it = node.fieldNames(); it.hasNext(); ) { 55 String pname = it.next(); 56 List<String> dependencies = propertyDependencies.get(pname); 57 if (dependencies != null && !dependencies.isEmpty()) { 58 for (String field : dependencies) { 59 if (node.get(field) == null) { 60 errors.add(message().instanceNode(node).property(pname).instanceLocation(instanceLocation) 61 .locale(executionContext.getExecutionConfig().getLocale()) 62 .failFast(executionContext.isFailFast()).arguments(field, pname) 63 .build()); 64 } 65 } 66 } 67 } 68 69 return Collections.unmodifiableSet(errors); 70 } 71 72 } 73