io.swagger.inflector.validators.NumericValidator Maven / Gradle / Ivy
package io.swagger.inflector.validators;
import io.swagger.models.parameters.AbstractSerializableParameter;
import io.swagger.models.parameters.Parameter;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class NumericValidator implements Validator {
public void validate(Object o, Parameter parameter, Iterator chain) throws ValidationException {
if(o != null && parameter instanceof AbstractSerializableParameter) {
AbstractSerializableParameter> ap = (AbstractSerializableParameter>) parameter;
if(ap.getEnum() != null && ap.getEnum().size() > 0) {
List> values = ap.getEnum();
Set allowable = new LinkedHashSet();
for(Object obj : values) {
allowable.add(obj.toString());
}
if(!allowable.contains(o.toString())) {
throw new ValidationException()
.message(new ValidationMessage()
.code(ValidationError.UNACCEPTABLE_VALUE)
.message(parameter.getIn() + " parameter `" + parameter.getName() + " value `" + o + "` is not in the allowable values `" + allowable + "`"));
}
};
if(ap.getMaximum() != null) {
double max = ap.getMaximum().doubleValue();
Double value;
try {
value = Double.parseDouble(o.toString());
}
catch (NumberFormatException e) {
throw new ValidationException()
.message(new ValidationMessage()
.code(ValidationError.INVALID_FORMAT)
.message(parameter.getIn() + " parameter `" + parameter.getName() + " is not a compatible number"));
}
if(ap.isExclusiveMaximum() != null && ap.isExclusiveMaximum()) {
if(value >= max) {
throw new ValidationException()
.message(new ValidationMessage()
.code(ValidationError.VALUE_OVER_MAXIMUM)
.message(parameter.getIn() + " parameter `" + parameter.getName() + " value `" + o + "` is greater than maximum allowed value `" + max + "`"));
}
}
else {
if(value > max) {
throw new ValidationException()
.message(new ValidationMessage()
.code(ValidationError.VALUE_OVER_MAXIMUM)
.message(parameter.getIn() + " parameter `" + parameter.getName() + " value `" + o + "` is greater or equal to maximum allowed value `" + max + "`"));
}
}
}
if(ap.getMinimum() != null) {
double min = ap.getMinimum().doubleValue();
Double value;
try {
value = Double.parseDouble(o.toString());
}
catch (NumberFormatException e) {
throw new ValidationException()
.message(new ValidationMessage()
.code(ValidationError.INVALID_FORMAT)
.message(parameter.getIn() + " parameter `" + parameter.getName() + " is not a compatible number"));
}
if(ap.isExclusiveMinimum() != null && ap.isExclusiveMinimum()) {
if(value <= min) {
throw new ValidationException()
.message(new ValidationMessage()
.code(ValidationError.VALUE_UNDER_MINIMUM)
.message(parameter.getIn() + " parameter `" + parameter.getName() + " value `" + o + "` is less than minimum allowed value `" + min + "`"));
}
}
else {
if(value < min) {
throw new ValidationException()
.message(new ValidationMessage()
.code(ValidationError.VALUE_UNDER_MINIMUM)
.message(parameter.getIn() + " parameter `" + parameter.getName() + " value `" + o + "` is less or equal to the minimum allowed value `" + min + "`"));
}
}
}
}
if(chain.hasNext()) {
chain.next().validate(o, parameter, chain);
return;
}
return;
}
}