Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fixed validation for path parameters and query parameters #113

Merged
merged 3 commits into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 92 additions & 75 deletions src/main/java/com/networknt/schema/EnumValidator.java
Original file line number Diff line number Diff line change
@@ -1,75 +1,92 @@
/*
* 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.Collections;
import java.util.LinkedHashSet;
import java.util.HashSet;
import java.util.Set;

public class EnumValidator extends BaseJsonValidator implements JsonValidator {
private static final Logger logger = LoggerFactory.getLogger(EnumValidator.class);

private final Set<JsonNode> nodes;
private final String error;

public EnumValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ENUM, validationContext);

if (schemaNode != null && schemaNode.isArray()) {
nodes = new HashSet<JsonNode>();
StringBuilder sb = new StringBuilder();

sb.append('[');
String separator = "";

for (JsonNode n : schemaNode) {
nodes.add(n);

sb.append(separator);
sb.append(n.asText());
separator = ", ";
}

sb.append(']');

error = sb.toString();
} else {
nodes = Collections.emptySet();
error = "[none]";
}

parseErrorCode(getValidatorType().getErrorCodeKey());
}

public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
debug(logger, node, rootNode, at);

Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();

if (!nodes.contains(node)) {
errors.add(buildValidationMessage(at, error));
}

return Collections.unmodifiableSet(errors);
}

}
/*
* 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.Collections;
import java.util.LinkedHashSet;
import java.util.HashSet;
import java.util.Set;

public class EnumValidator extends BaseJsonValidator implements JsonValidator {
private static final Logger logger = LoggerFactory.getLogger(EnumValidator.class);

private final Set<JsonNode> nodes;
private final String error;

public EnumValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ENUM, validationContext);

if (schemaNode != null && schemaNode.isArray()) {
nodes = new HashSet<JsonNode>();
StringBuilder sb = new StringBuilder();

sb.append('[');
String separator = "";

for (JsonNode n : schemaNode) {
nodes.add(n);

sb.append(separator);
sb.append(n.asText());
separator = ", ";
}

sb.append(']');

error = sb.toString();
} else {
nodes = Collections.emptySet();
error = "[none]";
}

parseErrorCode(getValidatorType().getErrorCodeKey());
}

public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
debug(logger, node, rootNode, at);

Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();

if (!nodes.contains(node) && !(config.isTypeLoose() && isTypeLooseContainsInEnum(node))) {
errors.add(buildValidationMessage(at, error));
}

return Collections.unmodifiableSet(errors);
}

/**
* Check whether enum contains the value of the JsonNode if the typeLoose is enabled.
* @param node JsonNode to check
*/
private boolean isTypeLooseContainsInEnum(JsonNode node) {
if (TypeFactory.getValueNodeType(node) == JsonType.STRING) {
String nodeText = node.textValue();
for (JsonNode n : nodes) {
String value = n.asText();
if (value != null && value.equals(nodeText)) {
return true;
}
}
}
return false;
}

}
140 changes: 70 additions & 70 deletions src/main/java/com/networknt/schema/MaximumValidator.java
Original file line number Diff line number Diff line change
@@ -1,70 +1,70 @@
/*
* 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.Collections;
import java.util.Set;
public class MaximumValidator extends BaseJsonValidator implements JsonValidator {
private static final Logger logger = LoggerFactory.getLogger(MaximumValidator.class);
private static final String PROPERTY_EXCLUSIVE_MAXIMUM = "exclusiveMaximum";
private double maximum;
private boolean excludeEqual = false;
public MaximumValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.MAXIMUM, validationContext);
if (schemaNode.isNumber()) {
maximum = schemaNode.doubleValue();
} else {
throw new JsonSchemaException("maximum value is not a number");
}
JsonNode exclusiveMaximumNode = getParentSchema().getSchemaNode().get(PROPERTY_EXCLUSIVE_MAXIMUM);
if (exclusiveMaximumNode != null && exclusiveMaximumNode.isBoolean()) {
excludeEqual = exclusiveMaximumNode.booleanValue();
}
parseErrorCode(getValidatorType().getErrorCodeKey());
}
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
debug(logger, node, rootNode, at);
if (!node.isNumber()) {
// maximum only applies to numbers
return Collections.emptySet();
}
String fieldType = this.getNodeFieldType();
double value = node.doubleValue();
if (greaterThan(value, maximum) || (excludeEqual && equals(value, maximum))) {
if (JsonType.INTEGER.toString().equals(fieldType)) {
return Collections.singleton(buildValidationMessage(at, "" + (int)maximum));
}
return Collections.singleton(buildValidationMessage(at, "" + maximum));
}
return Collections.emptySet();
}
}
/*
* 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.Collections;
import java.util.Set;

public class MaximumValidator extends BaseJsonValidator implements JsonValidator {
private static final Logger logger = LoggerFactory.getLogger(MaximumValidator.class);
private static final String PROPERTY_EXCLUSIVE_MAXIMUM = "exclusiveMaximum";

private double maximum;
private boolean excludeEqual = false;

public MaximumValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {

super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.MAXIMUM, validationContext);
if (schemaNode.isNumber()) {
maximum = schemaNode.doubleValue();
} else {
throw new JsonSchemaException("maximum value is not a number");
}

JsonNode exclusiveMaximumNode = getParentSchema().getSchemaNode().get(PROPERTY_EXCLUSIVE_MAXIMUM);
if (exclusiveMaximumNode != null && exclusiveMaximumNode.isBoolean()) {
excludeEqual = exclusiveMaximumNode.booleanValue();
}

parseErrorCode(getValidatorType().getErrorCodeKey());
}

public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
debug(logger, node, rootNode, at);

if (!TypeValidator.isNumber(node, config.isTypeLoose())) {
// maximum only applies to numbers
return Collections.emptySet();
}

String fieldType = this.getNodeFieldType();

double value = node.asDouble();
if (greaterThan(value, maximum) || (excludeEqual && equals(value, maximum))) {
if (JsonType.INTEGER.toString().equals(fieldType)) {
return Collections.singleton(buildValidationMessage(at, "" + (int)maximum));
}
return Collections.singleton(buildValidationMessage(at, "" + maximum));
}
return Collections.emptySet();
}

}
Loading