|
| 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 | +public class DependentRequired extends BaseJsonValidator implements JsonValidator { |
| 26 | + private static final Logger logger = LoggerFactory.getLogger(DependentRequired.class); |
| 27 | + private final Map<String, List<String>> propertyDependencies = new HashMap<String, List<String>>(); |
| 28 | + |
| 29 | + public DependentRequired(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) { |
| 30 | + |
| 31 | + super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.DEPENDENT_REQUIRED, validationContext); |
| 32 | + |
| 33 | + for (Iterator<String> it = schemaNode.fieldNames(); it.hasNext(); ) { |
| 34 | + String pname = it.next(); |
| 35 | + JsonNode pvalue = schemaNode.get(pname); |
| 36 | + if (pvalue.isArray()) { |
| 37 | + List<String> dependencies = propertyDependencies.computeIfAbsent(pname, k -> new ArrayList<>()); |
| 38 | + |
| 39 | + for (int i = 0; i < pvalue.size(); i++) { |
| 40 | + dependencies.add(pvalue.get(i).asText()); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + parseErrorCode(getValidatorType().getErrorCodeKey()); |
| 46 | + } |
| 47 | + |
| 48 | + public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) { |
| 49 | + debug(logger, node, rootNode, at); |
| 50 | + |
| 51 | + Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>(); |
| 52 | + |
| 53 | + for (Iterator<String> it = node.fieldNames(); it.hasNext(); ) { |
| 54 | + String pname = it.next(); |
| 55 | + List<String> dependencies = propertyDependencies.get(pname); |
| 56 | + if (dependencies != null && !dependencies.isEmpty()) { |
| 57 | + for (String field : dependencies) { |
| 58 | + if (node.get(field) == null) { |
| 59 | + errors.add(buildValidationMessage(at, propertyDependencies.toString())); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return Collections.unmodifiableSet(errors); |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments