Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for external class validators #67

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/MiniValidation/IValidatable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;

namespace MiniValidation;

/// <summary>
/// Provides a way to add a validator for a type outside the class.
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IValidatable<in T>
{
/// <summary>
/// Determines whether the specified object is valid.
/// </summary>
/// <param name="instance">The object instance to validate.</param>
/// <param name="validationContext">The validation context.</param>
/// <returns>A collection that holds failed-validation information.</returns>
Task<IEnumerable<ValidationResult>> ValidateAsync(T instance, ValidationContext validationContext);
}
52 changes: 48 additions & 4 deletions src/MiniValidation/MiniValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public static class MiniValidator
/// </remarks>
/// <param name="targetType">The <see cref="Type"/>.</param>
/// <param name="recurse"><c>true</c> to recursively check descendant types; if <c>false</c> only simple values directly on the target type are checked.</param>
/// <param name="serviceProvider">The service provider to use when checking for validators.</param>
/// <returns><c>true</c> if <paramref name="targetType"/> has anything to validate, <c>false</c> if not.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="targetType"/> is <c>null</c>.</exception>
public static bool RequiresValidation(Type targetType, bool recurse = true)
public static bool RequiresValidation(Type targetType, bool recurse = true, IServiceProvider? serviceProvider = null)
{
if (targetType is null)
{
Expand All @@ -44,7 +45,8 @@ public static bool RequiresValidation(Type targetType, bool recurse = true)
return typeof(IValidatableObject).IsAssignableFrom(targetType)
|| typeof(IAsyncValidatableObject).IsAssignableFrom(targetType)
|| (recurse && typeof(IEnumerable).IsAssignableFrom(targetType))
|| _typeDetailsCache.Get(targetType).Properties.Any(p => p.HasValidationAttributes || recurse);
|| _typeDetailsCache.Get(targetType).Properties.Any(p => p.HasValidationAttributes || recurse)
|| serviceProvider?.GetService(typeof(IValidatable<>).MakeGenericType(targetType)) != null;
}

/// <summary>
Expand Down Expand Up @@ -163,7 +165,7 @@ private static bool TryValidateImpl<TTarget>(TTarget target, IServiceProvider? s
throw new ArgumentNullException(nameof(target));
}

if (!RequiresValidation(target.GetType(), recurse))
if (!RequiresValidation(target.GetType(), recurse, serviceProvider))
{
errors = _emptyErrors;

Expand Down Expand Up @@ -306,7 +308,7 @@ private static bool TryValidateImpl<TTarget>(TTarget target, IServiceProvider? s

IDictionary<string, string[]>? errors;

if (!RequiresValidation(target.GetType(), recurse))
if (!RequiresValidation(target.GetType(), recurse, serviceProvider))
{
errors = _emptyErrors;

Expand Down Expand Up @@ -415,6 +417,7 @@ private static async Task<bool> TryValidateImpl(
(property.Recurse
|| typeof(IValidatableObject).IsAssignableFrom(propertyValueType)
|| typeof(IAsyncValidatableObject).IsAssignableFrom(propertyValueType)
|| serviceProvider?.GetService(typeof(IValidatable<>).MakeGenericType(propertyValueType!)) != null
|| properties.Any(p => p.Recurse)))
{
propertiesToRecurse!.Add(property, propertyValue);
Expand Down Expand Up @@ -532,6 +535,47 @@ private static async Task<bool> TryValidateImpl(
}
}

if (isValid)
{
var validators = (IEnumerable?)serviceProvider?.GetService(typeof(IEnumerable<>).MakeGenericType(typeof(IValidatable<>).MakeGenericType(targetType)));
if (validators != null)
{
foreach (var validator in validators)
{
if (!isValid)
continue;

var validatorMethod = validator.GetType().GetMethod(nameof(IValidatable<object>.ValidateAsync));
if (validatorMethod is null)
{
throw new InvalidOperationException(
$"The type {validators.GetType().Name} does not implement the required method 'Task<IEnumerable<ValidationResult>> ValidateAsync(object, ValidationContext)'.");
}

var validateTask = (Task<IEnumerable<ValidationResult>>?)validatorMethod.Invoke(validator,
new[] { target, validationContext });
if (validateTask is null)
{
throw new InvalidOperationException(
$"The type {validators.GetType().Name} does not implement the required method 'Task<IEnumerable<ValidationResult>> ValidateAsync(object, ValidationContext)'.");
}

// Reset validation context
validationContext.MemberName = null;
validationContext.DisplayName = validationContext.ObjectType.Name;

ThrowIfAsyncNotAllowed(validateTask.IsCompleted, allowAsync);

var validatableResults = await validateTask.ConfigureAwait(false);
if (validatableResults is not null)
{
ProcessValidationResults(validatableResults, workingErrors, prefix);
isValid = workingErrors.Count == 0 && isValid;
}
}
}
}

// Update state of target in tracking dictionary
validatedObjects[target] = isValid;

Expand Down
41 changes: 41 additions & 0 deletions tests/MiniValidation.UnitTests/TestTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,47 @@ public async Task<IEnumerable<ValidationResult>> ValidateAsync(ValidationContext
}
}

class TestClassLevel
{
public int TwentyOrMore { get; set; } = 20;
}

class TestClassLevelValidator : IValidatable<TestClassLevel>
{
public async Task<IEnumerable<ValidationResult>> ValidateAsync(TestClassLevel instance, ValidationContext validationContext)
{
await Task.Yield();

List<ValidationResult>? errors = null;

if (instance.TwentyOrMore < 20)
{
errors ??= new List<ValidationResult>();
errors.Add(new ValidationResult($"The field {validationContext.DisplayName} must have a value greater than 20.", new[] { nameof(TestClassLevel.TwentyOrMore) }));
}

return errors ?? Enumerable.Empty<ValidationResult>();
}
}

class ExtraTestClassLevelValidator : IValidatable<TestClassLevel>
{
public async Task<IEnumerable<ValidationResult>> ValidateAsync(TestClassLevel instance, ValidationContext validationContext)
{
await Task.Yield();

List<ValidationResult>? errors = null;

if (instance.TwentyOrMore > 20)
{
errors ??= new List<ValidationResult>();
errors.Add(new ValidationResult($"The field {validationContext.DisplayName} must have a value less than 20.", new[] { nameof(TestClassLevel.TwentyOrMore) }));
}

return errors ?? Enumerable.Empty<ValidationResult>();
}
}

class TestClassLevelAsyncValidatableOnlyTypeWithServiceProvider : IAsyncValidatableObject
{
public async Task<IEnumerable<ValidationResult>> ValidateAsync(ValidationContext validationContext)
Expand Down
57 changes: 57 additions & 0 deletions tests/MiniValidation.UnitTests/TryValidate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,63 @@ public async Task TryValidateAsync_With_ServiceProvider()
Assert.Equal(nameof(IServiceProvider), errors.Keys.First());
}

[Fact]
public void TryValidate_With_Validator()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<IValidatable<TestClassLevel>, TestClassLevelValidator>();
var serviceProvider = serviceCollection.BuildServiceProvider();

var thingToValidate = new TestClassLevel
{
TwentyOrMore = 12
};

Assert.Throws<InvalidOperationException>(() =>
{
var isValid = MiniValidator.TryValidate(thingToValidate, serviceProvider, out var errors);
});
}

[Fact]
public async Task TryValidateAsync_With_Validator()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<IValidatable<TestClassLevel>, TestClassLevelValidator>();
var serviceProvider = serviceCollection.BuildServiceProvider();

var thingToValidate = new TestClassLevel
{
TwentyOrMore = 12
};

var (isValid, errors) = await MiniValidator.TryValidateAsync(thingToValidate, serviceProvider);

Assert.False(isValid);
Assert.Single(errors);
Assert.Equal(nameof(TestValidatableType.TwentyOrMore), errors.Keys.First());
}

[Fact]
public async Task TryValidateAsync_With_Multiple_Validators()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<IValidatable<TestClassLevel>, TestClassLevelValidator>();
serviceCollection.AddSingleton<IValidatable<TestClassLevel>, ExtraTestClassLevelValidator>();
var serviceProvider = serviceCollection.BuildServiceProvider();

var thingToValidate = new TestClassLevel
{
TwentyOrMore = 22
};

var (isValid, errors) = await MiniValidator.TryValidateAsync(thingToValidate, serviceProvider);

Assert.False(isValid);
Assert.Single(errors);
Assert.Equal(nameof(TestValidatableType.TwentyOrMore), errors.Keys.First());
}

[Fact]
public void TryValidate_Enumerable_With_ServiceProvider()
{
Expand Down