Skip to content

Commit

Permalink
Use Validator.TryValidateValue
Browse files Browse the repository at this point in the history
Fixes #9
  • Loading branch information
DamianEdwards committed Sep 14, 2021
1 parent 0766071 commit 28225bd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
14 changes: 7 additions & 7 deletions src/MinimalValidation/MinimalValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ private static bool TryValidateImpl(

foreach (var property in typeProperties)
{
if (property.HasValidationAttribute)
if (property.HasValidationAttributes)
{
var validationContext = new ValidationContext(target) { MemberName = property.PropertyInfo.Name };
var validationContext = new ValidationContext(target) { MemberName = property.Name };
var validationResults = new List<ValidationResult>();
var propertyValue = property.PropertyInfo.GetValue(target);
var propertyIsValid = Validator.TryValidateProperty(propertyValue, validationContext, validationResults);
var propertyValue = property.GetValue(target);
var propertyIsValid = Validator.TryValidateValue(propertyValue, validationContext, validationResults, property.ValidationAttributes);
if (!propertyIsValid)
{
ProcessValidationResults(property.PropertyInfo.Name, validationResults, errors, prefix);
ProcessValidationResults(property.Name, validationResults, errors, prefix);
isValid = false;
}
}
Expand Down Expand Up @@ -126,12 +126,12 @@ private static bool TryValidateImpl(

if (property.IsEnumerable)
{
var thePrefix = $"{prefix}{property.PropertyInfo.Name}";
var thePrefix = $"{prefix}{property.Name}";
isValid = TryValidateEnumerable(propertyValue, recurse, errors, validatedObjects, thePrefix, currentDepth);
}
else
{
var thePrefix = $"{prefix}{property.PropertyInfo.Name}.";
var thePrefix = $"{prefix}{property.Name}.";
isValid = TryValidateImpl(propertyValue, recurse, errors, validatedObjects, thePrefix, currentDepth + 1);
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/MinimalValidation/TypeDetailsCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ private void Visit(Type type, HashSet<Type> visited)
continue;
}

var hasValidationOnProperty = property.GetCustomAttributes().OfType<ValidationAttribute>().Any();
var validationAttributes = property.GetCustomAttributes().OfType<ValidationAttribute>();
var hasValidationOnProperty = validationAttributes.Any();
var hasSkipRecursionOnProperty = property.GetCustomAttributes().OfType<SkipRecursionAttribute>().Any();
var enumerableType = GetEnumerableType(property.PropertyType);
if (enumerableType != null)
Expand All @@ -65,7 +66,7 @@ private void Visit(Type type, HashSet<Type> visited)
if (type == property.PropertyType && !hasSkipRecursionOnProperty)
{
propertiesToValidate ??= new List<PropertyDetails>();
propertiesToValidate.Add(new (property, hasValidationOnProperty, true, enumerableType));
propertiesToValidate.Add(new (property.Name, property, validationAttributes.ToArray(), true, enumerableType));
hasPropertiesOfOwnType = true;
continue;
}
Expand All @@ -80,13 +81,14 @@ private void Visit(Type type, HashSet<Type> visited)
if (recurse || hasValidationOnProperty)
{
propertiesToValidate ??= new List<PropertyDetails>();
propertiesToValidate.Add(new(property, hasValidationOnProperty, recurse, enumerableTypeHasProperties ? enumerableType : null));
propertiesToValidate.Add(new(property.Name, property, validationAttributes.ToArray(), recurse, enumerableTypeHasProperties ? enumerableType : null));
hasValidatableProperties = true;
}
}

if (hasPropertiesOfOwnType && propertiesToValidate != null)
{
// Remove properties of same type if there's nothing to validate on them
for (int i = propertiesToValidate.Count - 1; i >= 0; i--)
{
var property = propertiesToValidate[i];
Expand Down Expand Up @@ -124,10 +126,11 @@ private void Visit(Type type, HashSet<Type> visited)
}
}

internal record PropertyDetails(PropertyInfo PropertyInfo, bool HasValidationAttribute, bool Recurse, Type? EnumerableType)
internal record PropertyDetails(string Name, PropertyInfo PropertyInfo, ValidationAttribute[] ValidationAttributes, bool Recurse, Type? EnumerableType)
{
// TODO: Replace this with cached property getter (aka FastPropertyGetter)
public object? GetValue(object target) => PropertyInfo.GetValue(target);
public bool IsEnumerable => EnumerableType != null;
public bool HasValidationAttributes => ValidationAttributes.Length > 0;
}
}

0 comments on commit 28225bd

Please sign in to comment.