use of org.apache.synapse.commons.json.jsonprocessor.exceptions.ValidatorException in project wso2-synapse by wso2.
the class ArrayValidator method validateArray.
/**
* This method will validates an input array according to a given schema.
*
* @param input input array as a Map.
* @param schema JSON schema as an object.
* @return Validated JSON array.
* @throws ValidatorException Exception occurs in validation process.
* @throws ParserException Exception occurs in data type parsing.
*/
public static JsonArray validateArray(Map.Entry<String, JsonElement> input, JsonObject schema) throws ValidatorException, ParserException {
JsonParser parser;
int minItems = -1;
int maxItems = -1;
boolean uniqueItems = false;
boolean notAllowAdditional = false;
// parsing the properties related to arrays from the schema, if they exists.
if (schema.has(UNIQUE_ITEMS)) {
String uniqueItemsString = JsonProcessorUtils.replaceEnclosingQuotes(schema.get(UNIQUE_ITEMS).getAsString());
if (!uniqueItemsString.isEmpty()) {
uniqueItems = DataTypeConverter.convertToBoolean(uniqueItemsString);
}
}
if (schema.has(MIN_ITEMS)) {
String minItemsString = JsonProcessorUtils.replaceEnclosingQuotes(schema.get(MIN_ITEMS).getAsString());
if (!minItemsString.isEmpty()) {
minItems = DataTypeConverter.convertToInt(minItemsString);
if (minItems < 0) {
throw new ValidatorException("Invalid minItems constraint in the schema");
}
}
}
if (schema.has(MAX_ITEMS)) {
String maxItemsString = JsonProcessorUtils.replaceEnclosingQuotes(schema.get(MAX_ITEMS).getAsString());
if (!maxItemsString.isEmpty()) {
maxItems = DataTypeConverter.convertToInt(maxItemsString);
if (maxItems < 0) {
throw new ValidatorException("Invalid maxItems constraint in the schema");
}
}
}
// parsing additionalItems
// We are wrapping the additionalItems schema inside a json array schema object and calling this same method
// again
JsonObject additionalItemsSchema = null;
if (schema.has(ADDITIONAL_ITEMS)) {
JsonElement tempElement = schema.get(ADDITIONAL_ITEMS);
if (tempElement.isJsonPrimitive() && !tempElement.getAsBoolean()) {
notAllowAdditional = true;
} else if (tempElement.isJsonObject() && !tempElement.getAsJsonObject().entrySet().isEmpty()) {
StringBuffer jsonString = new StringBuffer("{\"type\": \"array\",\"items\": ");
jsonString.append(schema.get(ADDITIONAL_ITEMS).toString());
jsonString.append("}");
parser = new JsonParser();
additionalItemsSchema = parser.parse(jsonString.toString()).getAsJsonObject();
}
}
// Convert the input to an array. If possible, do the single element array correction. Ex 45 -> [45]
// else throw an error
JsonArray inputArray;
if (input.getValue().isJsonArray()) {
inputArray = input.getValue().getAsJsonArray();
} else {
inputArray = singleElementArrayCorrection(input.getValue());
}
// Structural validations
doStructuralValidations(inputArray, minItems, maxItems, uniqueItems);
// processing the items property in JSON array.
if (schema.has(ITEMS)) {
// Items must be either a valid JSON Schema or an array of valid JSON Schemas.
if (schema.get(ITEMS).isJsonArray()) {
// Items - valid JSON array.
JsonArray schemaArray = schema.get(ITEMS).getAsJsonArray();
processSchemaWithItemsArray(inputArray, schemaArray, additionalItemsSchema, notAllowAdditional);
// take all instances from json array and iteratively validate them.
} else if (schema.get(ITEMS).isJsonObject()) {
// Item is a JSON object
JsonObject schemaObject = schema.get(ITEMS).getAsJsonObject();
processSchemaWithOneItem(inputArray, schemaObject);
} else {
throw new ValidatorException("Schema for Array is invalid. " + "Should contain either JsonArray or JsonObject");
}
}
return inputArray;
}
use of org.apache.synapse.commons.json.jsonprocessor.exceptions.ValidatorException in project wso2-synapse by wso2.
the class ObjectValidator method multiTypeCheckObject.
// Try to correct a JSON object against an schema with multiple types.
private static void multiTypeCheckObject(Map.Entry<String, JsonElement> entry, ArrayList<String> types, JsonObject schema) throws ParserException, ValidatorException {
boolean validated = false;
for (String type : types) {
if (ValidatorConstants.OBJECT_KEYS.contains(type)) {
entry.setValue(ObjectValidator.validateObject(entry.getValue().getAsJsonObject(), schema));
validated = true;
}
}
if (!validated) {
for (String type : types) {
if (ValidatorConstants.ARRAY_KEYS.contains(type)) {
// Single element array correction
JsonArray array = new JsonArray();
array.add(entry.getValue().getAsJsonObject());
entry.setValue(array);
entry.setValue(ArrayValidator.validateArray(GSONDataTypeConverter.getMapFromString(entry.getValue().toString()), schema));
validated = true;
}
}
}
if (!validated) {
throw new ValidatorException("JSON object found " + entry.getValue() + " without matching " + "schema " + schema.toString());
}
}
use of org.apache.synapse.commons.json.jsonprocessor.exceptions.ValidatorException in project wso2-synapse by wso2.
the class ObjectValidator method multiTypeCheckPrimitive.
// Try to correct a JSON primitive (nominal, numeric, boolean) against an schema with multiple types.
private static void multiTypeCheckPrimitive(Map.Entry<String, JsonElement> entry, ArrayList<String> types, JsonObject schema) throws ParserException, ValidatorException {
String value = entry.getValue().getAsString();
boolean validated = false;
if ("true".equals(value) || "false".equals(value)) {
// if boolean check for boolean type if type is nominal convert to string
for (String type : types) {
if (ValidatorConstants.BOOLEAN_KEYS.contains(type)) {
entry.setValue(BooleanValidator.validateBoolean(schema, entry.getValue().getAsString()));
validated = true;
}
}
if (!validated) {
for (String type : types) {
if (ValidatorConstants.NOMINAL_KEYS.contains(type)) {
entry.setValue(StringValidator.validateNominal(schema, entry.getValue().getAsString()));
validated = true;
}
}
}
if (!validated) {
throw new ValidatorException("Boolean found " + entry.getValue() + " without matching " + "schema " + schema.toString());
}
} else if (NumberUtils.isParsable(value)) {
// if number check for numeric types if type is nominal convert to string
for (String type : types) {
if (ValidatorConstants.NUMERIC_KEYS.contains(type)) {
entry.setValue(NumericValidator.validateNumeric(schema, entry.getValue().getAsString()));
validated = true;
}
}
if (!validated) {
for (String type : types) {
if (ValidatorConstants.NOMINAL_KEYS.contains(type)) {
entry.setValue(StringValidator.validateNominal(schema, entry.getValue().getAsString()));
validated = true;
}
}
}
if (!validated) {
throw new ValidatorException("Number found " + entry.getValue() + " without matching " + "schema " + schema.toString());
}
} else {
// if string check for all types (nominal, boolean, numeric) and try to parse
for (String type : types) {
if (ValidatorConstants.NOMINAL_KEYS.contains(type)) {
entry.setValue(StringValidator.validateNominal(schema, entry.getValue().getAsString()));
validated = true;
}
}
if (!validated) {
for (String type : types) {
if (ValidatorConstants.BOOLEAN_KEYS.contains(type)) {
entry.setValue(BooleanValidator.validateBoolean(schema, entry.getValue().getAsString()));
validated = true;
}
}
}
if (!validated) {
for (String type : types) {
if (ValidatorConstants.NUMERIC_KEYS.contains(type)) {
entry.setValue(NumericValidator.validateNumeric(schema, entry.getValue().getAsString()));
validated = true;
}
}
}
if (!validated) {
throw new ValidatorException(entry.getValue() + " not matching with the schema " + schema.toString());
}
}
}
use of org.apache.synapse.commons.json.jsonprocessor.exceptions.ValidatorException in project wso2-synapse by wso2.
the class ObjectValidator method processPatternProperties.
/**
* This method will process input objects matching with "patternProperties" regular expression.
*
* @param object input JSON object.
* @param schema input schema.
* @param patternProperties arrayList which should be updated with matching keys of patternProperty.
* @throws ParserException Exception occurred in data type conversions.
* @throws ValidatorException Exception occurred in schema validations.
*/
private static void processPatternProperties(JsonObject object, JsonObject schema, ArrayList<String> patternProperties) throws ParserException, ValidatorException {
Set<String> inputObjectKeyset = object.keySet();
JsonObject patternsObject = schema.getAsJsonObject(PATTERN_PROPERTIES);
Set<Map.Entry<String, JsonElement>> patterns = patternsObject.entrySet();
for (Map.Entry<String, JsonElement> pattern : patterns) {
String regex = pattern.getKey();
JsonObject tempSchema = pattern.getValue().getAsJsonObject();
if (tempSchema.has(ValidatorConstants.TYPE_KEY)) {
String type = JsonProcessorUtils.replaceEnclosingQuotes(tempSchema.get(ValidatorConstants.TYPE_KEY).getAsString());
// get the list of keys matched the regular expression
ArrayList<String> matchingKeys = getMatchRegexAgainstStringSet(inputObjectKeyset, regex);
for (String key : matchingKeys) {
// updating patternProperties array for later use in additional properties.
if (!patternProperties.contains(key)) {
patternProperties.add(key);
}
parseAndReplaceValues(object, tempSchema, type, key);
}
} else {
throw new ValidatorException("Schema for object must have a " + "type declaration : " + tempSchema.toString());
}
}
}
use of org.apache.synapse.commons.json.jsonprocessor.exceptions.ValidatorException in project wso2-synapse by wso2.
the class ObjectValidator method validateObject.
/**
* This method will validate a given JSON input object according to
*
* @param object JSON Object
* @param schema JSON schema
* @return validated object
* @throws ParserException Exception occurred in data type conversions.
* @throws ValidatorException Exception occurred in schema validations.
*/
public static JsonObject validateObject(JsonObject object, JsonObject schema) throws ParserException, ValidatorException {
int minimumProperties = -1;
int maximumProperties = -1;
// keyword MUST be an array. Elements of this array, if any, MUST be strings, and MUST be unique.
if (schema.has(REQUIRED)) {
JsonArray requiredArray = schema.getAsJsonArray(REQUIRED);
for (JsonElement element : requiredArray) {
if (!object.has(element.getAsString())) {
throw new ValidatorException("Input object : " + object.toString() + " does not contains all the elements required in the schema : " + schema.toString());
}
}
}
if (schema.has(MIN_PROPERTIES)) {
String minPropertiesString = JsonProcessorUtils.replaceEnclosingQuotes(schema.get(MIN_PROPERTIES).getAsString());
if (!minPropertiesString.isEmpty()) {
minimumProperties = DataTypeConverter.convertToInt(minPropertiesString);
}
}
if (schema.has(MAX_PROPERTIES)) {
String maxPropertiesString = JsonProcessorUtils.replaceEnclosingQuotes(schema.get(MAX_PROPERTIES).getAsString());
if (!maxPropertiesString.isEmpty()) {
maximumProperties = DataTypeConverter.convertToInt(maxPropertiesString);
}
}
JsonObject schemaObject = null;
if (schema.has("properties")) {
schemaObject = (JsonObject) schema.get("properties");
}
Set<Map.Entry<String, JsonElement>> entryInput = object.entrySet();
// doing structural validation
doStructuralValidation(maximumProperties, minimumProperties, entryInput);
ArrayList<String> inputProperties = new ArrayList<>();
ArrayList<String> patternProperties = new ArrayList<>();
ArrayList<String> schemaProperties = new ArrayList<>();
if (schemaObject != null && !schemaObject.entrySet().isEmpty()) {
for (Map.Entry<String, JsonElement> entry : schemaObject.entrySet()) {
if (object.has(entry.getKey())) {
schemaProperties.add(entry.getKey());
}
}
}
// validate children elements according to the schema only if properties are defined for each item.
processSchemaProperties(schemaObject, entryInput, inputProperties);
// handling pattern properties
if (schema.has(PATTERN_PROPERTIES)) {
processPatternProperties(object, schema, patternProperties);
}
// handling additionalProperties
processAdditionalProperties(object, schema, inputProperties, patternProperties, schemaProperties);
return object;
}
Aggregations