|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 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 | +package com.networknt.schema; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.databind.JsonNode; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | + |
| 22 | +import java.util.*; |
| 23 | + |
| 24 | +public class PropertyNamesValidator extends BaseJsonValidator implements JsonValidator { |
| 25 | + private static final Logger logger = LoggerFactory.getLogger(PropertyNamesValidator.class); |
| 26 | + private Map<String, JsonSchema> schemas; |
| 27 | + private boolean schemaValue = false; |
| 28 | + public PropertyNamesValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) { |
| 29 | + super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.PROPERTYNAMES, validationContext); |
| 30 | + if(schemaNode.isBoolean()) { |
| 31 | + schemaValue = schemaNode.booleanValue(); |
| 32 | + } else { |
| 33 | + schemas = new HashMap<String, JsonSchema>(); |
| 34 | + for (Iterator<String> it = schemaNode.fieldNames(); it.hasNext(); ) { |
| 35 | + String pname = it.next(); |
| 36 | + schemas.put(pname, new JsonSchema(validationContext, schemaPath + "/" + pname, parentSchema.getCurrentUri(), schemaNode.get(pname), parentSchema) |
| 37 | + .initialize()); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) { |
| 43 | + debug(logger, node, rootNode, at); |
| 44 | + |
| 45 | + Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>(); |
| 46 | + if(schemas != null) { |
| 47 | + for (Map.Entry<String, JsonSchema> entry : schemas.entrySet()) { |
| 48 | + JsonNode propertyNode = node.get(entry.getKey()); |
| 49 | + // check propertyNames |
| 50 | + if (!node.isObject()) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + for (Iterator<String> it = node.fieldNames(); it.hasNext(); ) { |
| 54 | + String pname = it.next(); |
| 55 | + if("maxLength".equals(entry.getKey()) && pname.length() > entry.getValue().getSchemaNode().intValue()) { |
| 56 | + errors.add(buildValidationMessage(at + "." + pname, "maxLength")); |
| 57 | + } |
| 58 | + if("minLength".equals(entry.getKey()) && pname.length() < entry.getValue().getSchemaNode().intValue()) { |
| 59 | + errors.add(buildValidationMessage(at + "." + pname, "minLength")); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } else { |
| 64 | + if(!schemaValue && node.isObject() && !node.isEmpty()) { |
| 65 | + errors.add(buildValidationMessage(at + "." + node, "false")); |
| 66 | + } |
| 67 | + } |
| 68 | + return Collections.unmodifiableSet(errors); |
| 69 | + } |
| 70 | +} |
0 commit comments