use of org.apache.synapse.commons.json.jsonprocessor.exceptions.ValidatorException in project wso2-synapse by wso2.
the class NumericValidator method validateNumeric.
/**
* Take JSON schema, number as a string input and validate.
*
* @param inputObject JSON schema.
* @param value numeric value
* @return JsonPrimitive contains a number
* @throws ParserException Exception occurred in data type conversions.
* @throws ValidatorException Exception occurred in schema validations.
*/
public static JsonPrimitive validateNumeric(JsonObject inputObject, String value) throws ParserException, ValidatorException {
Double multipleOf;
if (value == null) {
throw new ValidatorException("Expected a number but found null");
}
// replacing enclosing quotes
value = JsonProcessorUtils.replaceEnclosingQuotes(value);
if (isNumeric(value)) {
String type = "";
if (inputObject.has(ValidatorConstants.TYPE_KEY)) {
if (inputObject.get(ValidatorConstants.TYPE_KEY).isJsonArray()) {
for (JsonElement typeValue : inputObject.get(ValidatorConstants.TYPE_KEY).getAsJsonArray()) {
type += JsonProcessorUtils.replaceEnclosingQuotes(typeValue.getAsString());
}
} else {
type = JsonProcessorUtils.replaceEnclosingQuotes(inputObject.get(ValidatorConstants.TYPE_KEY).getAsString());
}
}
// handling multiples of condition
Double doubleValue = DataTypeConverter.convertToDouble(value);
if (inputObject.has(MULTIPLE_OF)) {
multipleOf = DataTypeConverter.convertToDouble(JsonProcessorUtils.replaceEnclosingQuotes(inputObject.get(MULTIPLE_OF).getAsString()));
if (doubleValue % multipleOf != 0) {
throw new ValidatorException("Number " + value + " is not a multiple of " + "" + multipleOf + ". multipleOf constraint in " + inputObject.toString() + " is violated " + "by the input " + value);
}
}
// handling maximum and minimum
if (inputObject.has(MINIMUM_VALUE)) {
String minimumString = JsonProcessorUtils.replaceEnclosingQuotes(inputObject.get(MINIMUM_VALUE).getAsString());
if (!minimumString.isEmpty() && doubleValue < DataTypeConverter.convertToDouble(minimumString)) {
throw new ValidatorException("Number " + value + " is less than the " + "minimum allowed value" + ". minimumValue constraint in " + inputObject.toString() + " is violated by the input " + ": " + value);
}
}
if (inputObject.has(MAXIMUM_VALUE)) {
String maximumString = JsonProcessorUtils.replaceEnclosingQuotes(inputObject.get(MAXIMUM_VALUE).getAsString());
if (!maximumString.isEmpty() && doubleValue > DataTypeConverter.convertToDouble(maximumString)) {
throw new ValidatorException("Number " + value + " is greater than the " + "maximum allowed value. maximumValue constraint in " + inputObject.toString() + " is violated by the input " + ": " + value);
}
}
// handling exclusive maximum and minimum
if (inputObject.has(EXCLUSIVE_MINIMUM)) {
String minimumString = JsonProcessorUtils.replaceEnclosingQuotes(inputObject.get(EXCLUSIVE_MINIMUM).getAsString());
if (!minimumString.isEmpty() && doubleValue <= DataTypeConverter.convertToDouble(minimumString)) {
throw new ValidatorException("Number " + value + " is less than the " + "minimum allowed value. ExclusiveMinimum constraint in " + inputObject.toString() + " is violated by the " + "input : " + value);
}
}
if (inputObject.has(EXCLUSIVE_MAXIMUM)) {
String maximumString = JsonProcessorUtils.replaceEnclosingQuotes(inputObject.get(EXCLUSIVE_MAXIMUM).getAsString());
if (!maximumString.isEmpty() && doubleValue >= DataTypeConverter.convertToDouble(maximumString)) {
throw new ValidatorException("Number " + value + " is greater than the " + "maximum allowed value. ExclusiveMaximum constraint in " + inputObject.toString() + " is violated by the " + "input : " + value);
}
}
// Enum validations
if (inputObject.has(ValidatorConstants.ENUM)) {
JsonArray enumElements = inputObject.getAsJsonArray(ValidatorConstants.ENUM);
if (enumElements.size() > 0 && !enumElements.contains(new JsonPrimitive(doubleValue))) {
throw new ValidatorException("Number \"" + value + "\" not contains any " + "element from the enum. Input " + value + " not contains any value from the enum in " + inputObject.toString());
}
}
// Const validation
if (inputObject.has(ValidatorConstants.CONST) && !doubleValue.equals(inputObject.getAsJsonPrimitive(ValidatorConstants.CONST).getAsDouble())) {
throw new ValidatorException("Number \"" + value + "\" is not equal to the " + "const value input " + value + " not contains the const defined in " + inputObject.toString());
}
// convert to integer of give value is a float
if (type.contains(INTEGER_STRING)) {
return new JsonPrimitive(DataTypeConverter.convertToInt(value));
} else {
// this condition address both type number and empty json schemas
return new JsonPrimitive(doubleValue);
}
}
throw new ParserException("\"" + value + "\"" + " is not a number. " + "A number expected in the schema " + inputObject.toString() + " but received " + value);
}
use of org.apache.synapse.commons.json.jsonprocessor.exceptions.ValidatorException in project wso2-synapse by wso2.
the class JsonProcessor method parseJson.
/**
* This method will parse a given JSON string according to the given schema. Schema as an Object.
* Can use this method when using caching.
*
* @param inputString input JSON string.
* @param schema already parsed JSON schema.
* @return corrected JSON string.
* @throws ValidatorException Exception occurs in validation process.
* @throws ParserException Exception occurs in data type parsing.
*/
private static String parseJson(String inputString, Object schema) throws ValidatorException, ParserException {
if (StringUtils.isNotEmpty(inputString) && schema instanceof JsonObject) {
JsonElement result = null;
JsonObject schemaObject = (JsonObject) schema;
if (((JsonObject) schema).has(ValidatorConstants.TYPE_KEY)) {
String type = JsonProcessorUtils.replaceEnclosingQuotes(schemaObject.get(ValidatorConstants.TYPE_KEY).toString());
if (ValidatorConstants.BOOLEAN_KEYS.contains(type)) {
result = BooleanValidator.validateBoolean(schemaObject, inputString);
} else if (ValidatorConstants.NOMINAL_KEYS.contains(type)) {
result = StringValidator.validateNominal(schemaObject, inputString);
} else if (ValidatorConstants.NUMERIC_KEYS.contains(type)) {
result = NumericValidator.validateNumeric(schemaObject, inputString);
} else if (ValidatorConstants.ARRAY_KEYS.contains(type)) {
result = ArrayValidator.validateArray(GSONDataTypeConverter.getMapFromString(inputString), schemaObject);
} else if (ValidatorConstants.NULL_KEYS.contains(type)) {
NullValidator.validateNull(schemaObject, inputString);
result = JsonNull.INSTANCE;
} else if (ValidatorConstants.OBJECT_KEYS.contains(type)) {
JsonElement input = parser.parse(inputString);
if (input.isJsonObject()) {
result = ObjectValidator.validateObject(input.getAsJsonObject(), schemaObject);
} else {
throw new ValidatorException("Expected a JSON as input but found : " + inputString);
}
}
if (result != null) {
return result.toString();
}
return null;
} else {
throw new ValidatorException("JSON schema should contain a type declaration");
}
} else {
throw new ParserException("Input json and schema should not be null, " + "schema should be a JSON object");
}
}
use of org.apache.synapse.commons.json.jsonprocessor.exceptions.ValidatorException in project wso2-synapse by wso2.
the class JsonProcessor method parseJson.
/**
* This method parse a given JSON string according to the given schema. Both as string.
*
* @param inputString input String.
* @param inputSchema input Schema.
* @return corrected String.
* @throws ValidatorException Exception occurs in validation process.
* @throws ParserException Exception occurs in data type parsing.
*/
public static String parseJson(String inputString, String inputSchema) throws ValidatorException, ParserException {
if (StringUtils.isNotEmpty(inputString) && StringUtils.isNotEmpty(inputSchema)) {
JsonObject schemaObject;
JsonElement schema;
try {
schema = parser.parse(inputSchema);
} catch (JsonSyntaxException ex) {
throw new ValidatorException("Invalid JSON schema", ex);
}
if (schema.isJsonObject()) {
// Handling empty JSON objects - valid for all inputs
if (schema.toString().replaceAll("\\s+", "").equals("{}")) {
return inputString;
}
schemaObject = schema.getAsJsonObject();
} else if (schema.isJsonPrimitive()) {
// if schema is primitive it should be a boolean
boolean valid = schema.getAsBoolean();
if (valid) {
return inputString;
} else {
throw new ValidatorException("JSON schema is false, so all validations will fail");
}
} else {
throw new ValidatorException("JSON schema should be an object or boolean");
}
return parseJson(inputString, schemaObject);
} else {
throw new ParserException("Input json and schema should not be null");
}
}
use of org.apache.synapse.commons.json.jsonprocessor.exceptions.ValidatorException in project wso2-synapse by wso2.
the class ArrayValidator method processSchemaWithOneItem.
/**
* Validate JSON array when items is a single JSON object.
* Ex:- {"type":"array", "items":{"type": "boolean"}}
*
* @param inputArray input data as json array.
* @param schemaObject input schema as json object.
* @throws ValidatorException validation exception occurs.
* @throws ParserException parsing exception occurs.
*/
private static void processSchemaWithOneItem(JsonArray inputArray, JsonObject schemaObject) throws ValidatorException, ParserException {
if (schemaObject.has(ValidatorConstants.TYPE_KEY)) {
String type = JsonProcessorUtils.replaceEnclosingQuotes(schemaObject.get(ValidatorConstants.TYPE_KEY).toString());
int i = 0;
if (ValidatorConstants.BOOLEAN_KEYS.contains(type)) {
for (JsonElement element : inputArray) {
inputArray.set(i, BooleanValidator.validateBoolean(schemaObject, element.getAsString()));
i++;
}
} else if (ValidatorConstants.NUMERIC_KEYS.contains(type)) {
for (JsonElement element : inputArray) {
inputArray.set(i, NumericValidator.validateNumeric(schemaObject, element.getAsString()));
i++;
}
} else if (ValidatorConstants.NOMINAL_KEYS.contains(type)) {
for (JsonElement element : inputArray) {
inputArray.set(i, StringValidator.validateNominal(schemaObject, element.getAsString()));
i++;
}
} else if (ValidatorConstants.ARRAY_KEYS.contains(type)) {
for (JsonElement element : inputArray) {
inputArray.set(i, ArrayValidator.validateArray(GSONDataTypeConverter.getMapFromString(element.getAsString()), schemaObject));
i++;
}
} else if (ValidatorConstants.OBJECT_KEYS.contains(type)) {
for (JsonElement element : inputArray) {
inputArray.set(i, ObjectValidator.validateObject(element.getAsJsonObject(), schemaObject));
i++;
}
} else if (ValidatorConstants.NULL_KEYS.contains(type)) {
for (JsonElement element : inputArray) {
if (element != null) {
NullValidator.validateNull(schemaObject, element.toString());
}
inputArray.set(i, JsonNull.INSTANCE);
i++;
}
} else {
throw new ValidatorException("Schema for array must have a type declaration" + schemaObject.toString());
}
}
}
use of org.apache.synapse.commons.json.jsonprocessor.exceptions.ValidatorException in project wso2-synapse by wso2.
the class ArrayValidator method processSchemaWithItemsArray.
/**
* Validate JSON array when both items and schema are arrays.
* Ex:- {"type":"array", "items":[{"type": "boolean"},{"type": "numeric"}]}
*
* @param inputArray input data as json array.
* @param schemaArray inout schema as json array.
* @throws ValidatorException validation exception occurs.
* @throws ParserException parsing exception occurs.
*/
private static void processSchemaWithItemsArray(JsonArray inputArray, JsonArray schemaArray, JsonObject additionalItemsSchema, boolean notAllowAdditional) throws ValidatorException, ParserException {
if (notAllowAdditional && inputArray.size() > schemaArray.size()) {
throw new ValidatorException("Array : " + inputArray.toString() + " has more items than allowed in the schema");
}
int i = 0;
for (JsonElement element : schemaArray) {
JsonObject tempObj = element.getAsJsonObject();
// Checking for empty input schema Ex:- {}
if (!tempObj.entrySet().isEmpty()) {
if (tempObj.has(ValidatorConstants.TYPE_KEY)) {
String type = JsonProcessorUtils.replaceEnclosingQuotes(tempObj.get(ValidatorConstants.TYPE_KEY).toString());
if (ValidatorConstants.BOOLEAN_KEYS.contains(type)) {
inputArray.set(i, BooleanValidator.validateBoolean(tempObj, inputArray.get(i).getAsString()));
} else if (ValidatorConstants.NOMINAL_KEYS.contains(type)) {
inputArray.set(i, StringValidator.validateNominal(tempObj, inputArray.get(i).getAsString()));
} else if (ValidatorConstants.NUMERIC_KEYS.contains(type)) {
inputArray.set(i, NumericValidator.validateNumeric(tempObj, inputArray.get(i).getAsString()));
} else if (ValidatorConstants.ARRAY_KEYS.contains(type)) {
inputArray.set(i, ArrayValidator.validateArray(GSONDataTypeConverter.getMapFromString(inputArray.get(i).toString()), tempObj));
} else if (ValidatorConstants.NULL_KEYS.contains(type)) {
if (inputArray.get(i) != null) {
NullValidator.validateNull(tempObj, inputArray.get(i).toString());
}
inputArray.set(i, JsonNull.INSTANCE);
} else if (ValidatorConstants.OBJECT_KEYS.contains(type)) {
inputArray.set(i, ObjectValidator.validateObject(inputArray.get(i).getAsJsonObject(), tempObj));
}
} else {
throw new ValidatorException("Array items should contain a type " + "declaration");
}
}
i++;
}
// additional schema validating the reset of the array
if (additionalItemsSchema != null && inputArray.size() > schemaArray.size()) {
JsonArray extraArray = new JsonArray();
for (int j = i; j < inputArray.size(); j++) {
extraArray.add(inputArray.get(j));
}
extraArray = ArrayValidator.validateArray(GSONDataTypeConverter.getMapFromJsonArray(extraArray), additionalItemsSchema);
// putting back the values again after validation
for (int j = 0; j < extraArray.size(); j++) {
inputArray.set(i + j, extraArray.get(j));
}
}
}
Aggregations