Package com.bazaarvoice.commons.data.model.json.schema.validation

Examples of com.bazaarvoice.commons.data.model.json.schema.validation.ValidationResult


            String key = entry.getKey();
            Object value = entry.getValue();

            if (JSONSchema.SCHEMA_KEY.equals(key)) {
                if (!(value instanceof String)) {
                    results.addResult(new ValidationResult().type(ResultType.INVALID_SCHEMA).path(path).message(JSONSchema.SCHEMA_KEY + " must be a string"));
                    continue;
                }

                String schemaID = (String) value;
                if (!Strings.isNullOrEmpty(schema.getID()) && !schemaID.equals(schema.getID())) {
                    results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path).message(JSONSchema.SCHEMA_KEY + " should be " + schema.getID() + ": " + schemaID));
                }

                continue;
            }

            JSONSchemaProperty property = getPropertyForKey(key);
            JSONSchema valueSchema;
            if (property != null) {
                valueSchema = property.getValueSchema();
                seenProperties.add(property);
            } else {
                valueSchema = _additionalPropertiesSchema;
            }

            if (valueSchema != null) {
                valueSchema.validate(value, path + "." + key, results);
            } else {
                results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path + "." + key).message("Additional properties not supported: " + entry));
            }
        }

        for (final JSONSchemaProperty property : getAllProperties()) {
            if (!seenProperties.contains(property) && property.getValueSchema().isRequired()) {
                String propertyDescription;
                if (property instanceof JSONSchemaNamedProperty) {
                    propertyDescription = ((JSONSchemaNamedProperty) property).getName();
                } else if (property instanceof JSONSchemaPatternProperty) {
                    propertyDescription = ((JSONSchemaPatternProperty) property).getPattern();
                } else {
                    throw new IllegalStateException("Unknown property: " + property);
                }

                results.addResult(new ValidationResult().type(ResultType.REQUIRED_VALUE).path(path).message("Required parameter " + propertyDescription + " missing: " + obj));
            }
        }
    }
View Full Code Here


        super.validate(schema, obj, path, results);

        String str = (String) obj;

        if (_minimumLength != null && str.length() < _minimumLength) {
            results.addResult(new ValidationResult().type(ResultType.FORMAT_MISMATCH).path(path).message("Minimum length is " + _minimumLength + ": " + str));
        }

        if (_maximumLength != null && str.length() > _maximumLength) {
            results.addResult(new ValidationResult().type(ResultType.FORMAT_MISMATCH).path(path).message("Maximum length is " + _maximumLength + ": " + str));
        }

        if (_regex != null && !_regex.matcher(str).find()) {
            results.addResult(new ValidationResult().type(ResultType.FORMAT_MISMATCH).path(path).message("Text doesn't matter pattern " + getPattern() + ": " + str));
        }

        if (_format != null && _format != JSONSchemaTextFormat.CUSTOM) {
            _sFormatValidators.validate(_format, path, str, results);
        }

        if (_format == JSONSchemaTextFormat.CUSTOM) {
            results.addResult(new ValidationResult().type(ResultType.FORMAT_NOT_VALIDATED).path(path).message("Custom format " + _customFormat + " not validated: " + str));
        }
    }
View Full Code Here

        if (map != null) {
            // must be an object, so let's find the "right" schema
            Object schemaValue = map.get(JSONSchema.SCHEMA_KEY);
            String schemaID = schemaValue != null ? schemaValue.toString() : null;
            if (schemaID == null) {
                results.addResult(new ValidationResult().type(ResultType.MISSING_SCHEMA).path(path).message("No " + JSONSchema.SCHEMA_KEY + " to disambiguate union schema"));
                return;
            }

            JSONSchema matchingSchema = getSchema(schemaID);
            if (matchingSchema == null) {
                results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path).message("Union schema not found: " + schemaID));
                return;
            }

            matchingSchema.validate(obj, buildPath(path, matchingSchema, 0), results);
            return;
View Full Code Here

        Number number = (Number) obj;
        if (_minimum != null) {
            int result = NumberComparator.INSTANCE.compare(number, _minimum);
            if (isMinimumExclusive()) {
                if (!(result > 0)) {
                    results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path).message("Value must be > " + _minimum + ": " + number));
                }
            } else {
                if (!(result >= 0)) {
                    results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path).message("Value must be >= " + _minimum + ": " + number));
                }
            }
        }

        if (_maximum != null) {
            int result = NumberComparator.INSTANCE.compare(number, _maximum);
            if (isMaximumExclusive()) {
                if (!(result < 0)) {
                    results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path).message("Value must be < " + _maximum + ": " + number));
                }
            } else {
                if (!(result <= 0)) {
                    results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path).message("Value must be <= " + _maximum + ": " + number));
                }
            }
        }

        if (_divisibleBy != null) {
            if (_divisibleBy instanceof Integer && number instanceof Integer) {
                int divInt = _divisibleBy.intValue();
                int valueInt = number.intValue();
                if (valueInt % divInt != 0) {
                    results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path).message("Value must be divisible by " + _divisibleBy + ": " + number));
                }
            } else {
                double divDouble = _divisibleBy.doubleValue();
                double valueDouble = number.doubleValue();
                double result = valueDouble / divDouble;
                if (Math.floor(result) != result) {
                    results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path).message("Value must be divisible by " + _divisibleBy + ": " + number));
                }
            }
        }
    }
View Full Code Here

        if (isTuple()) {
            int tupleSize = _items.size();
            int collSize = _items.size();
            if (tupleSize > collSize) {
                results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path).message("Tuple is not the correct size " + tupleSize + ": " + coll));
            }

            Iterator<JSONSchema> tupleIter = _items.iterator();
            Iterator collIter = coll.iterator();
            int index = 0;
            while (tupleIter.hasNext() && collIter.hasNext()) {
                JSONSchema tupleSchema = tupleIter.next();
                Object tupleObj = collIter.next();
                tupleSchema.validate(tupleObj, path + "[" + index + "]", results);

                index++;
            }

            if (_additionalItemsSchema != null) {
                while (collIter.hasNext()) {
                    Object additionalObj = collIter.next();
                    _additionalItemsSchema.validate(additionalObj, path + "[" + index + "]", results);

                    index++;
                }
            } else if (collIter.hasNext()) {
                results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path).message("Tuple does not allow additional items:" + coll));
            }
        } else {
            int index = 0;
            JSONSchema itemSchema = getItem();
            for (final Object collObj : coll) {
View Full Code Here

    public void validate(JSONSchema schema, Object obj, String path, ValidationResults results) {
        @SuppressWarnings ("unchecked")
        V value = (V) obj;

        if (!_enumValues.isEmpty() && !_enumValues.contains(value)) {
            results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path).message("Value not in enumerated values: " + value));
        }
    }
View Full Code Here

    }

    protected abstract boolean validate(String str);

    protected void addFormatMismatchResult(String path, String str, ValidationResults results) {
        results.addResult(new ValidationResult().type(ResultType.FORMAT_MISMATCH).path(path).message("Text not in " + _formatName + " format: " + str));
    }
View Full Code Here

    }

    @Override
    public ValidationResult fromJSONObject(JSONObject jsonObject)
            throws JSONException {
        return new ValidationResult().
                type(convertToEnum(ResultType.class, jsonObject.optString(getTypeField(), null))).
                path(jsonObject.optString(getPathField(), null)).
                message(jsonObject.optString(getMessageField(), null));
    }
View Full Code Here

                }
            };
        }

        if (_minimumItems != null && coll.size() < _minimumItems) {
            results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path).message("Array does not have enough items, < " + _minimumItems + ": " + coll));
        }

        if (_maximumItems != null && coll.size() > _maximumItems) {
            results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path).message("Array has too many items, > " + _minimumItems + ": " + coll));
        }

        boolean hasDuplicates = false;
        Set<Object> set = Sets.newHashSet();
        if (isTuple()) {
            int tupleSize = _items.size();
            int collSize = _items.size();
            if (tupleSize > collSize) {
                results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path).message("Tuple is not the correct size, " + tupleSize + ": " + coll));
            }

            Iterator<JSONSchema> tupleIter = _items.iterator();
            Iterator collIter = coll.iterator();
            int index = 0;
            while (tupleIter.hasNext() && collIter.hasNext()) {
                JSONSchema tupleSchema = tupleIter.next();
                Object tupleObj = collIter.next();
                hasDuplicates = hasDuplicates || !set.add(tupleObj);
                tupleSchema.validate(tupleObj, path + "[" + index + "]", results);

                index++;
            }

            if (_additionalItemsSchema != null) {
                while (collIter.hasNext()) {
                    Object additionalObj = collIter.next();
                    hasDuplicates = hasDuplicates || !set.add(additionalObj);
                    _additionalItemsSchema.validate(additionalObj, path + "[" + index + "]", results);

                    index++;
                }
            } else if (collIter.hasNext()) {
                results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path).message("Tuple does not allow additional items: " + coll));
            }
        } else {
            int index = 0;
            JSONSchema itemSchema = getItem();
            for (final Object collObj : coll) {
                hasDuplicates = hasDuplicates || !set.add(collObj);
                itemSchema.validate(collObj, path + "[" + index + "]", results);

                index++;
            }
        }

        if (isSet() && hasDuplicates) {
            results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path).message("Set has duplicate items: " + coll));
        }
    }
View Full Code Here

    public void validate(JSONSchema schema, Object obj, String path, ValidationResults results) {
        @SuppressWarnings ("unchecked")
        V value = (V) obj;

        if (!_enumValues.isEmpty() && !_enumValues.contains(value)) {
            results.addResult(new ValidationResult().type(ResultType.CONSTRAINT_VIOLATION).path(path).message("Value not in enumerated values: " + value));
        }
    }
View Full Code Here

TOP

Related Classes of com.bazaarvoice.commons.data.model.json.schema.validation.ValidationResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.