protected internal virtual void PerformValidation(string path, object value, List <ValidationResult> results) { if (value == null) { // Check for required values if (IsRequired) { results.Add( new ValidationResult( path, ValidationResultType.Error, "VALUE_IS_NULL", "value cannot be null", "NOT NULL", null ) ); } } else { value = ObjectReader.GetValue(value); // Check validation rules if (Rules != null) { foreach (var rule in Rules) { rule.Validate(path, this, value, results); } } } }
/// <summary> /// Validates a given value to match specified type. The type can be defined as a /// Schema, type, a type name or TypeCode When type is a Schema, it executes /// validation recursively against that Schema. /// </summary> /// <param name="path">a dot notation path to the value.</param> /// <param name="type">a type to match the value type</param> /// <param name="value">a value to be validated.</param> /// <param name="results">a list with validation results to add new results.</param> protected void PerformTypeValidation(string path, object type, object value, List <ValidationResult> results) { // If type it not defined then skip if (type == null) { return; } // Perform validation against schema var schema = type as Schema; if (schema != null) { schema.PerformValidation(path, value, results); return; } // If value is null then skip value = ObjectReader.GetValue(value); if (value == null) { return; } // Match types if (TypeMatcher.MatchType(type, value.GetType(), value)) { return; } // Match enum if (TypeMatcher.MatchEnum(type, value)) { return; } var name = path ?? "value"; var valueType = value.GetType(); // Generate type mismatch error results.Add( new ValidationResult( path, ValidationResultType.Error, "TYPE_MISMATCH", name + " type must be " + type + " but found " + valueType.Name, type, valueType.Name ) ); }
/// <summary> /// Validates a given value against the schema and configured validation rules. /// </summary> /// <param name="path">a dot notation path to the value.</param> /// <param name="value">a value to be validated.</param> /// <param name="results">a list with validation results to add new results.</param> protected internal override void PerformValidation(string path, object value, List <ValidationResult> results) { var name = path ?? "value"; value = ObjectReader.GetValue(value); base.PerformValidation(path, value, results); if (value == null) { return; } var list = value as IEnumerable; if (list != null) { var index = 0; foreach (var element in list) { var elementPath = string.IsNullOrWhiteSpace(path) ? index.ToString() : path + "." + index; PerformTypeValidation(elementPath, ValueType, element, results); index++; } } else { results.Add( new ValidationResult( path, ValidationResultType.Error, "VALUE_ISNOT_ARRAY", name + " type must be List or Array", typeof(IEnumerable), value.GetType() ) ); } }
/// <summary> /// Validates a given value against the schema and configured validation rules. /// </summary> /// <param name="path">a dot notation path to the value.</param> /// <param name="value">a value to be validated.</param> /// <param name="results">a list with validation results to add new results.</param> protected internal override void PerformValidation(string path, object value, List <ValidationResult> results) { var name = path ?? "value"; value = ObjectReader.GetValue(value); base.PerformValidation(path, value, results); if (value == null) { return; } var map = value as IDictionary; if (map != null) { foreach (var key in map.Keys) { var elementPath = string.IsNullOrWhiteSpace(path) ? key.ToString() : path + "." + key; PerformTypeValidation(elementPath, KeyType, key, results); PerformTypeValidation(elementPath, ValueType, map[key], results); } } else { results.Add( new ValidationResult( path, ValidationResultType.Error, "VALUE_ISNOT_MAP", name + " type must to be Map (Dictionary)", typeof(IDictionary), value.GetType() ) ); } }