mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 15:11:10 +00:00
Defines base implementation for mediatr and fluentvalidation with samples
This commit is contained in:
1 parent
76db0c1597
commit
9a5c7473f9
33 files changed
+1123
-131
No files matched your search
@@ -0,0 +1,57 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Infrastructure.Validation;
|
||||
|
||||
/// <summary>
|
||||
/// Base class for creating validators for commands and queries.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request (command or query) to be validated.</typeparam>
|
||||
public abstract class AbstractValidator<TRequest> : IValidator<TRequest>
|
||||
{
|
||||
private readonly List<IValidationRule<TRequest>> _rules = [];
|
||||
|
||||
/// <summary>
|
||||
/// Defines a validation rule for a specific property on the request being validated.
|
||||
/// </summary>
|
||||
/// <typeparam name="TProperty">The type of the property being validated.</typeparam>
|
||||
/// <param name="propertySelector">A function that selects the property to validate from the request instance.</param>
|
||||
/// <returns>An <see cref="IRuleBuilder{TRequest, TProperty}"/> instance to configure the rule fluently.</returns>
|
||||
protected IRuleBuilder<TRequest, TProperty> RuleFor<TProperty>(Func<TRequest, TProperty> propertySelector)
|
||||
{
|
||||
ValidationRule<TRequest, TProperty> rule = new(propertySelector);
|
||||
_rules.Add(rule);
|
||||
return rule;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines a validation rule that is evaluated against each item within a collection property on the request being validated.
|
||||
/// </summary>
|
||||
/// <typeparam name="TItem">The type of the items in the collection being validated.</typeparam>
|
||||
/// <param name="collectionSelector">A function that selects the collection to validate from the request instance.</param>
|
||||
/// <returns>An <see cref="IRuleBuilder{TRequest, TItem}"/> instance to configure the per-item rule fluently.</returns>
|
||||
protected IRuleBuilder<TRequest, TItem> RuleForEach<TItem>(Func<TRequest, IEnumerable<TItem>?> collectionSelector)
|
||||
{
|
||||
ValidationRuleForEach<TRequest, TItem> rule = new(collectionSelector);
|
||||
_rules.Add(rule);
|
||||
return rule;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates the specified request instance against all defined rules.
|
||||
/// </summary>
|
||||
/// <param name="instance">The request instance to validate.</param>
|
||||
/// <returns>A list of validation errors. If the instance is valid, the list will be empty.</returns>
|
||||
public List<Error> Validate(TRequest instance)
|
||||
{
|
||||
List<Error> errors = [];
|
||||
|
||||
foreach (IValidationRule<TRequest> rule in _rules)
|
||||
errors.AddRange(rule.Validate(instance));
|
||||
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Infrastructure.Validation;
|
||||
|
||||
/// <summary>
|
||||
/// Provides a fluent API for building validation rules for commands and queries.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request (command or query) being validated.</typeparam>
|
||||
/// <typeparam name="TProperty">The type of the specific property on the request that the rule applies to.</typeparam>
|
||||
public interface IRuleBuilder<TRequest, TProperty>
|
||||
{
|
||||
/// <summary>
|
||||
/// Specifies a condition that the property must satisfy to be considered valid.
|
||||
/// </summary>
|
||||
/// <param name="predicate">A function to test the property value.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
IRuleBuilder<TRequest, TProperty> Must(Func<TProperty, bool> predicate);
|
||||
|
||||
/// <summary>
|
||||
/// Specifies a condition that must be met for the validation rule to be executed.
|
||||
/// </summary>
|
||||
/// <param name="condition">A function evaluating the request instance.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
IRuleBuilder<TRequest, TProperty> When(Func<TRequest, bool> condition);
|
||||
|
||||
/// <summary>
|
||||
/// Specifies a custom <see cref="Error"/> object to be returned when the validation fails.
|
||||
/// </summary>
|
||||
/// <param name="error">The specific error to return upon failure.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
IRuleBuilder<TRequest, TProperty> WithError(Error error);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using System.Collections.Generic;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Infrastructure.Validation;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for triggering commands and queries validator evaluation.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request (command or query) being evaluated.</typeparam>
|
||||
internal interface IValidationRule<in TRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Evaluates the validation rule against the specified request instance.
|
||||
/// </summary>
|
||||
/// <param name="instance">The request instance to validate.</param>
|
||||
/// <returns>The <see cref="Error"/>s produced by this rule; empty if the rule passes.</returns>
|
||||
IEnumerable<Error> Validate(TRequest instance);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using System.Collections.Generic;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Infrastructure.Validation;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for validating requests of commands and queries.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request (command or query) to be validated.</typeparam>
|
||||
public interface IValidator<in TRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Validates the specified request instance against the defined rules.
|
||||
/// </summary>
|
||||
/// <param name="instance">The request instance to validate.</param>
|
||||
/// <returns>A list of validation errors. If the instance is valid, the list will be empty.</returns>
|
||||
List<Error> Validate(TRequest instance);
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Infrastructure.Validation;
|
||||
|
||||
/// <summary>
|
||||
/// Defines a validation rule for commands and queries that evaluates a specific property of a request.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request (command or query) being validated.</typeparam>
|
||||
/// <typeparam name="TProperty">The type of the specific property on the request that the rule applies to.</typeparam>
|
||||
internal sealed class ValidationRule<TRequest, TProperty> : IValidationRule<TRequest>, IRuleBuilder<TRequest, TProperty>
|
||||
{
|
||||
private readonly Func<TRequest, TProperty> _propertySelector;
|
||||
private readonly List<Func<TProperty, bool>> _predicates = [];
|
||||
private Error error = Error.Validation();
|
||||
private Func<TRequest, bool>? condition;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ValidationRule{TRequest, TProperty}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="propertySelector">A function that extracts the property value from the request instance.</param>
|
||||
public ValidationRule(Func<TRequest, TProperty> propertySelector)
|
||||
{
|
||||
_propertySelector = propertySelector;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a condition that the property must satisfy to be considered valid.
|
||||
/// </summary>
|
||||
/// <param name="predicate">A function to test the property value.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
public IRuleBuilder<TRequest, TProperty> Must(Func<TProperty, bool> predicate)
|
||||
{
|
||||
_predicates.Add(predicate);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies a condition that must be met for the validation rule to be executed.
|
||||
/// </summary>
|
||||
/// <param name="condition">A function evaluating the request instance.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
public IRuleBuilder<TRequest, TProperty> When(Func<TRequest, bool> condition)
|
||||
{
|
||||
this.condition = condition;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies a custom <see cref="Error"/> object to be returned when the validation fails.
|
||||
/// </summary>
|
||||
/// <param name="error">The specific error to return upon failure.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
public IRuleBuilder<TRequest, TProperty> WithError(Error error)
|
||||
{
|
||||
this.error = error;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Evaluates all configured predicates against the specified request instance.
|
||||
/// </summary>
|
||||
/// <param name="instance">The request instance to validate.</param>
|
||||
/// <returns>The <see cref="Error"/> if any predicate fails; empty if the rule passes.</returns>
|
||||
public IEnumerable<Error> Validate(TRequest instance)
|
||||
{
|
||||
// evaluate the condition first, if it returns false, skip validation entirely.
|
||||
if (condition is not null && !condition(instance))
|
||||
yield break;
|
||||
|
||||
TProperty value = _propertySelector(instance);
|
||||
|
||||
foreach (Func<TProperty, bool> predicate in _predicates)
|
||||
{
|
||||
if (!predicate(value))
|
||||
{
|
||||
yield return error;
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Infrastructure.Validation;
|
||||
|
||||
/// <summary>
|
||||
/// Defines a validation rule for commands and queries that evaluates each item within a collection property of a request.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request (command or query) being validated.</typeparam>
|
||||
/// <typeparam name="TItem">The type of the items in the collection that the rule applies to.</typeparam>
|
||||
internal sealed class ValidationRuleForEach<TRequest, TItem> : IValidationRule<TRequest>, IRuleBuilder<TRequest, TItem>
|
||||
{
|
||||
private readonly Func<TRequest, IEnumerable<TItem>?> _collectionSelector;
|
||||
private readonly List<Func<TItem, bool>> _predicates = [];
|
||||
private Error error = Error.Validation();
|
||||
private Func<TRequest, bool>? condition;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ValidationRuleForEach{TRequest, TItem}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="collectionSelector">A function that extracts the collection to iterate from the request instance.</param>
|
||||
public ValidationRuleForEach(Func<TRequest, IEnumerable<TItem>?> collectionSelector)
|
||||
{
|
||||
_collectionSelector = collectionSelector;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a condition that each item in the collection must satisfy to be considered valid.
|
||||
/// </summary>
|
||||
/// <param name="predicate">A function to test each item value.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TItem}"/> instance for method chaining.</returns>
|
||||
public IRuleBuilder<TRequest, TItem> Must(Func<TItem, bool> predicate)
|
||||
{
|
||||
_predicates.Add(predicate);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies a condition that must be met for the validation rule to be executed.
|
||||
/// </summary>
|
||||
/// <param name="condition">A function evaluating the request instance.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TItem}"/> instance for method chaining.</returns>
|
||||
public IRuleBuilder<TRequest, TItem> When(Func<TRequest, bool> condition)
|
||||
{
|
||||
this.condition = condition;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies a custom <see cref="Error"/> object to be returned when the validation fails.
|
||||
/// </summary>
|
||||
/// <param name="error">The specific error to return upon failure.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TItem}"/> instance for method chaining.</returns>
|
||||
public IRuleBuilder<TRequest, TItem> WithError(Error error)
|
||||
{
|
||||
this.error = error;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Evaluates all configured predicates against each item in the collection.
|
||||
/// </summary>
|
||||
/// <param name="instance">The request instance to validate.</param>
|
||||
/// <returns>One <see cref="Error"/> per item that fails a predicate; empty if every item passes.</returns>
|
||||
public IEnumerable<Error> Validate(TRequest instance)
|
||||
{
|
||||
// evaluate the condition first, if it returns false, skip validation entirely.
|
||||
if (condition is not null && !condition(instance))
|
||||
yield break;
|
||||
|
||||
IEnumerable<TItem>? collection = _collectionSelector(instance);
|
||||
if (collection is null)
|
||||
yield break;
|
||||
|
||||
foreach (TItem item in collection)
|
||||
{
|
||||
foreach (Func<TItem, bool> predicate in _predicates)
|
||||
{
|
||||
if (!predicate(item))
|
||||
{
|
||||
yield return error;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user