Example #1
0
        private void ValidateProperties(JToken token, string propertyName, string propertyPath, List <ValidationError> errors)
        {
            var obj = token as JObject;

            foreach (var propertyInfo in _schema.Properties)
            {
                var newPropertyPath = propertyName != null ? propertyName + "." + propertyInfo.Key : propertyInfo.Key;

                var property = obj != null?obj.Property(propertyInfo.Key) : null;

                if (property != null)
                {
                    var propertyValidator = new JsonSchemaValidator(propertyInfo.Value);
                    var propertyErrors    = propertyValidator.Validate(property.Value, propertyInfo.Key, newPropertyPath);
                    errors.AddRange(propertyErrors);
                }
                else if (propertyInfo.Value.IsRequired)
                {
                    errors.Add(new ValidationError(ValidationErrorKind.PropertyRequired, propertyInfo.Key, newPropertyPath));
                }
            }

            if (obj != null)
            {
                var properties = obj.Properties().ToList();

                ValidateMaxProperties(properties, propertyName, propertyPath, errors);
                ValidateMinProperties(properties, propertyName, propertyPath, errors);

                var additionalProperties = properties.Where(p => !_schema.Properties.ContainsKey(p.Name)).ToList();

                ValidatePatternProperties(additionalProperties, errors);
                ValidateAdditionalProperties(additionalProperties, propertyName, propertyPath, errors);
            }
        }
        private ChildSchemaValidationError TryCreateChildSchemaError(JsonSchemaValidator validator, JsonSchema4 schema, JToken token, ValidationErrorKind errorKind, string property, string path)
        {
            var errors = validator.Validate(token, null, null);
            if (errors.Count == 0)
                return null;

            var errorDictionary = new Dictionary<JsonSchema4, ICollection<ValidationError>>();
            errorDictionary.Add(schema, errors);

            return new ChildSchemaValidationError(errorKind, property, path, errorDictionary);
        }
Example #3
0
        private ChildSchemaValidationError TryCreateChildSchemaError(JsonSchemaValidator validator, JsonSchema4 schema, JToken token, ValidationErrorKind errorKind, string property, string path)
        {
            var errors = validator.Validate(token, null, null);

            if (errors.Count == 0)
            {
                return(null);
            }

            var errorDictionary = new Dictionary <JsonSchema4, ICollection <ValidationError> >();

            errorDictionary.Add(schema, errors);

            return(new ChildSchemaValidationError(errorKind, property, path, errorDictionary));
        }
Example #4
0
        /// <summary>Validates the given JSON token against this schema. </summary>
        /// <param name="token">The token to validate. </param>
        /// <returns>The collection of validation errors. </returns>
        public ICollection <ValidationError> Validate(JToken token)
        {
            var validator = new JsonSchemaValidator(ActualSchema);

            return(validator.Validate(token, null, token.Path));
        }
Example #5
0
        /// <summary>Validates the given JSON data against this schema.</summary>
        /// <param name="jsonData">The JSON data to validate. </param>
        /// <returns>The collection of validation errors. </returns>
        public ICollection <ValidationError> Validate(string jsonData)
        {
            var validator = new JsonSchemaValidator();

            return(validator.Validate(jsonData, ActualSchema));
        }
 private ChildSchemaValidationError TryCreateChildSchemaError(JsonSchema4 schema, JToken token, ValidationErrorKind errorKind, string property, string path)
 {
     var validator = new JsonSchemaValidator(schema);
     return TryCreateChildSchemaError(validator, schema, token, errorKind, property, path);
 }
        private void ValidateProperties(JToken token, string propertyName, string propertyPath, List<ValidationError> errors)
        {
            var obj = token as JObject;
            foreach (var propertyInfo in _schema.Properties)
            {
                var newPropertyPath = propertyName != null ? propertyName + "." + propertyInfo.Key : propertyInfo.Key;

                var property = obj != null ? obj.Property(propertyInfo.Key) : null;
                if (property != null)
                {
                    var propertyValidator = new JsonSchemaValidator(propertyInfo.Value);
                    var propertyErrors = propertyValidator.Validate(property.Value, propertyInfo.Key, newPropertyPath);
                    errors.AddRange(propertyErrors);
                }
                else if (propertyInfo.Value.IsRequired)
                    errors.Add(new ValidationError(ValidationErrorKind.PropertyRequired, propertyInfo.Key, newPropertyPath));
            }

            if (obj != null)
            {
                var properties = obj.Properties().ToList(); 

                ValidateMaxProperties(properties, propertyName, propertyPath, errors);
                ValidateMinProperties(properties, propertyName, propertyPath, errors);

                var additionalProperties = properties.Where(p => !_schema.Properties.ContainsKey(p.Name)).ToList();

                ValidatePatternProperties(additionalProperties, errors);
                ValidateAdditionalProperties(additionalProperties, propertyName, propertyPath, errors);
            }
        }
Example #8
0
        private ChildSchemaValidationError TryCreateChildSchemaError(JsonSchema4 schema, JToken token, ValidationErrorKind errorKind, string property, string path)
        {
            var validator = new JsonSchemaValidator(schema);

            return(TryCreateChildSchemaError(validator, schema, token, errorKind, property, path));
        }
Example #9
0
        /// <summary>Validates the given JSON token against this schema.</summary>
        /// <param name="token">The token to validate. </param>
        /// <param name="customValidators">Custom validators to validate the token.</param>
        /// <returns>The collection of validation errors. </returns>
        public ICollection <ValidationError> Validate(JToken token, params IFormatValidator[] customValidators)
        {
            var validator = new JsonSchemaValidator(customValidators);

            return(validator.Validate(token, ActualSchema));
        }
Example #10
0
        /// <summary>Validates the given JSON data against this schema.</summary>
        /// <param name="jsonData">The JSON data to validate. </param>
        /// <param name="customValidators">Custom validators to validate the JSON.</param>
        /// <exception cref="JsonReaderException">Could not deserialize the JSON data.</exception>
        /// <returns>The collection of validation errors. </returns>
        public ICollection <ValidationError> Validate(string jsonData, params IFormatValidator[] customValidators)
        {
            var validator = new JsonSchemaValidator(customValidators);

            return(validator.Validate(jsonData, ActualSchema));
        }
Example #11
0
 /// <summary>Validates the given JSON token against this schema.</summary>
 /// <param name="token">The token to validate. </param>
 /// <returns>The collection of validation errors. </returns>
 public ICollection<ValidationError> Validate(JToken token)
 {
     var validator = new JsonSchemaValidator();
     return validator.Validate(token, ActualSchema);
 }
Example #12
0
        /// <summary>Validates the given JSON token against this schema. </summary>
        /// <param name="token">The token to validate. </param>
        /// <returns>The collection of validation errors. </returns>
        public ICollection <ValidationError> Validate(JToken token)
        {
            var validator = new JsonSchemaValidator(this);

            return(validator.Validate(token, null, null));
        }