Replaces FluentValidation and Mediator command/queries handling with custom implementations.

This commit is contained in:
BinaryAlley committed 2026-08-13 08:53:06 -07:00
1 parent 9a5c7473f9
commit 2d1c6b1ab8
297 files changed
+3619 -3045

No files matched your search

@@ -20,7 +20,7 @@ public abstract class AbstractValidator<TRequest> : IValidator<TRequest>
/// <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)
public IRuleBuilder<TRequest, TProperty> RuleFor<TProperty>(Func<TRequest, TProperty> propertySelector)
{
ValidationRule<TRequest, TProperty> rule = new(propertySelector);
_rules.Add(rule);
@@ -33,7 +33,7 @@ public abstract class AbstractValidator<TRequest> : IValidator<TRequest>
/// <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)
public IRuleBuilder<TRequest, TItem> RuleForEach<TItem>(Func<TRequest, IEnumerable<TItem>?> collectionSelector)
{
ValidationRuleForEach<TRequest, TItem> rule = new(collectionSelector);
_rules.Add(rule);
@@ -19,6 +19,13 @@ public interface IRuleBuilder<TRequest, TProperty>
/// <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 both the request instance and the property value must satisfy to be considered valid.
/// </summary>
/// <param name="predicate">A function to test the request instance together with the property value.</param>
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
IRuleBuilder<TRequest, TProperty> Must(Func<TRequest, TProperty, bool> predicate);
/// <summary>
/// Specifies a condition that must be met for the validation rule to be executed.
/// </summary>
@@ -32,4 +39,11 @@ public interface IRuleBuilder<TRequest, TProperty>
/// <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);
/// <summary>
/// Defines nested validation rules for the complex property being validated.
/// </summary>
/// <param name="configure">An action used to configure the inline validator for the property.</param>
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
IRuleBuilder<TRequest, TProperty> ChildRules(Action<AbstractValidator<TProperty>> configure);
}
@@ -0,0 +1,12 @@
#region ========================================================================= USING =====================================================================================
#endregion
namespace Lumina.Application.Common.Infrastructure.Validation;
/// <summary>
/// Concrete <see cref="AbstractValidator{TRequest}"/> used to declare validation rules inline for nested complex properties.
/// </summary>
/// <typeparam name="TRequest">The type of the request (command or query) to be validated.</typeparam>
public class InlineValidator<TRequest> : AbstractValidator<TRequest>
{
}
@@ -14,10 +14,28 @@ namespace Lumina.Application.Common.Infrastructure.Validation;
internal sealed class ValidationRule<TRequest, TProperty> : IValidationRule<TRequest>, IRuleBuilder<TRequest, TProperty>
{
private readonly Func<TRequest, TProperty> _propertySelector;
// Predicates and their errors are kept in two parallel lists (rather than a single list of tuples)
// so that Must() can append cheaply and WithError() can patch the *last* entry by index, without
// having to rebuild a composite struct each time.
private readonly List<Func<TProperty, bool>> _predicates = [];
private Error error = Error.Validation();
private readonly List<Error> _predicateErrors = [];
// instance-aware predicates get their own separate pair of lists instead of being merged into the
// lists above, because they have a different delegate signature (TRequest, TProperty) vs (TProperty).
// Keeping them separate avoids wrapping every plain predicate in an adapter, just to fit one shape.
private readonly List<Func<TRequest, TProperty, bool>> _instanceAwarePredicates = [];
private readonly List<Error> _instanceAwarePredicateErrors = [];
private Error currentError = Error.Validation();
private Func<TRequest, bool>? condition;
// Tracks which list WithError() should patch, since it doesn't know on its own whether the
// most recent Must() call went to the plain-predicate list or the instance-aware list.
private bool lastAddedPredicateIsInstanceAware;
private AbstractValidator<TProperty>? _childValidator;
/// <summary>
/// Initializes a new instance of the <see cref="ValidationRule{TRequest, TProperty}"/> class.
/// </summary>
@@ -35,6 +53,25 @@ internal sealed class ValidationRule<TRequest, TProperty> : IValidationRule<TReq
public IRuleBuilder<TRequest, TProperty> Must(Func<TProperty, bool> predicate)
{
_predicates.Add(predicate);
// Every predicate is paired with whatever error is "current" at the time it's added, so that a
// subsequent WithError() call has something concrete to overwrite via index lookup.
_predicateErrors.Add(currentError);
lastAddedPredicateIsInstanceAware = false;
return this;
}
/// <summary>
/// Adds a condition that the request instance together with the property must satisfy to be considered valid.
/// </summary>
/// <param name="predicate">A function to test the request instance together with the property value.</param>
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
public IRuleBuilder<TRequest, TProperty> Must(Func<TRequest, TProperty, bool> predicate)
{
// This overload exists for cross-field rules (e.g. "EndDate must be after StartDate") where
// the check can't be expressed from the property value alone and needs the whole request.
_instanceAwarePredicates.Add(predicate);
_instanceAwarePredicateErrors.Add(currentError);
lastAddedPredicateIsInstanceAware = true;
return this;
}
@@ -56,7 +93,35 @@ internal sealed class ValidationRule<TRequest, TProperty> : IValidationRule<TReq
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
public IRuleBuilder<TRequest, TProperty> WithError(Error error)
{
this.error = error;
// Callers might pass in an error created with the wrong factory (e.g. Error.NotFound()) by mistake.
// Rebuilding it as Error.Validation() here guarantees every error surfaced by this class is
// consistently typed as a validation error, regardless of what the caller supplied.
Error validationError = Error.Validation(description: error.Description);
currentError = validationError;
// WithError() is meant to apply to the predicate that was *just* added via Must(), not to all
// predicates registered so far. Since Must() can target either parallel-list pair, we use the
// flag set by the last Must() call to figure out which list's last slot to overwrite.
if (lastAddedPredicateIsInstanceAware && _instanceAwarePredicateErrors.Count > 0)
_instanceAwarePredicateErrors[^1] = validationError;
else if (!lastAddedPredicateIsInstanceAware && _predicateErrors.Count > 0)
_predicateErrors[^1] = validationError;
return this;
}
/// <summary>
/// Defines nested validation rules for the complex property being validated.
/// </summary>
/// <param name="configure">An action used to configure the inline validator for the property.</param>
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
public IRuleBuilder<TRequest, TProperty> ChildRules(Action<AbstractValidator<TProperty>> configure)
{
// Lets a rule delegate to a fully separate validator for a complex nested object (e.g. validating
// an Address property's own fields) instead of forcing every nested check to be flattened into
// Must() predicates on this rule.
InlineValidator<TProperty> childValidator = new();
configure(childValidator);
_childValidator = childValidator;
return this;
}
@@ -64,7 +129,7 @@ internal sealed class ValidationRule<TRequest, TProperty> : IValidationRule<TReq
/// 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>
/// <returns>The <see cref="Error"/> of every predicate that fails; empty if the rule passes.</returns>
public IEnumerable<Error> Validate(TRequest instance)
{
// evaluate the condition first, if it returns false, skip validation entirely.
@@ -73,13 +138,19 @@ internal sealed class ValidationRule<TRequest, TProperty> : IValidationRule<TReq
TProperty value = _propertySelector(instance);
foreach (Func<TProperty, bool> predicate in _predicates)
{
if (!predicate(value))
{
yield return error;
yield break;
}
}
// Deliberately does NOT stop at the first failure: every failing predicate contributes its own error,
// so a caller sees the full set of problems for // this property in one pass instead of having to fix
// and resubmit one error at a time.
for (int index = 0; index < _predicates.Count; index++)
if (!_predicates[index](value))
yield return _predicateErrors[index];
for (int index = 0; index < _instanceAwarePredicates.Count; index++)
if (!_instanceAwarePredicates[index](instance, value))
yield return _instanceAwarePredicateErrors[index];
if (_childValidator is not null && value is not null)
foreach (Error childError in _childValidator.Validate(value))
yield return childError;
}
}
@@ -15,8 +15,13 @@ internal sealed class ValidationRuleForEach<TRequest, TItem> : IValidationRule<T
{
private readonly Func<TRequest, IEnumerable<TItem>?> _collectionSelector;
private readonly List<Func<TItem, bool>> _predicates = [];
private Error error = Error.Validation();
private readonly List<Error> _predicateErrors = [];
private readonly List<Func<TRequest, TItem, bool>> _instanceAwarePredicates = [];
private readonly List<Error> _instanceAwarePredicateErrors = [];
private Error currentError = Error.Validation();
private Func<TRequest, bool>? condition;
private bool lastAddedPredicateIsInstanceAware;
private AbstractValidator<TItem>? _childValidator;
/// <summary>
/// Initializes a new instance of the <see cref="ValidationRuleForEach{TRequest, TItem}"/> class.
@@ -35,6 +40,21 @@ internal sealed class ValidationRuleForEach<TRequest, TItem> : IValidationRule<T
public IRuleBuilder<TRequest, TItem> Must(Func<TItem, bool> predicate)
{
_predicates.Add(predicate);
_predicateErrors.Add(currentError);
lastAddedPredicateIsInstanceAware = false;
return this;
}
/// <summary>
/// Adds a condition that the request instance together with each item must satisfy to be considered valid.
/// </summary>
/// <param name="predicate">A function to test the request instance together with each item value.</param>
/// <returns>The current <see cref="IRuleBuilder{TRequest, TItem}"/> instance for method chaining.</returns>
public IRuleBuilder<TRequest, TItem> Must(Func<TRequest, TItem, bool> predicate)
{
_instanceAwarePredicates.Add(predicate);
_instanceAwarePredicateErrors.Add(currentError);
lastAddedPredicateIsInstanceAware = true;
return this;
}
@@ -56,7 +76,26 @@ internal sealed class ValidationRuleForEach<TRequest, TItem> : IValidationRule<T
/// <returns>The current <see cref="IRuleBuilder{TRequest, TItem}"/> instance for method chaining.</returns>
public IRuleBuilder<TRequest, TItem> WithError(Error error)
{
this.error = error;
// validation rules always produce validation errors, regardless of the error type of the source error
Error validationError = Error.Validation(description: error.Description);
currentError = validationError;
if (lastAddedPredicateIsInstanceAware && _instanceAwarePredicateErrors.Count > 0)
_instanceAwarePredicateErrors[^1] = validationError;
else if (!lastAddedPredicateIsInstanceAware && _predicateErrors.Count > 0)
_predicateErrors[^1] = validationError;
return this;
}
/// <summary>
/// Defines nested validation rules for each item of the collection being validated.
/// </summary>
/// <param name="configure">An action used to configure the inline validator for each item.</param>
/// <returns>The current <see cref="IRuleBuilder{TRequest, TItem}"/> instance for method chaining.</returns>
public IRuleBuilder<TRequest, TItem> ChildRules(Action<AbstractValidator<TItem>> configure)
{
InlineValidator<TItem> childValidator = new();
configure(childValidator);
_childValidator = childValidator;
return this;
}
@@ -64,7 +103,7 @@ internal sealed class ValidationRuleForEach<TRequest, TItem> : IValidationRule<T
/// 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>
/// <returns>One <see cref="Error"/> per failing predicate, per item; empty if every item passes.</returns>
public IEnumerable<Error> Validate(TRequest instance)
{
// evaluate the condition first, if it returns false, skip validation entirely.
@@ -77,14 +116,17 @@ internal sealed class ValidationRuleForEach<TRequest, TItem> : IValidationRule<T
foreach (TItem item in collection)
{
foreach (Func<TItem, bool> predicate in _predicates)
{
if (!predicate(item))
{
yield return error;
break;
}
}
for (int index = 0; index < _predicates.Count; index++)
if (!_predicates[index](item))
yield return _predicateErrors[index];
for (int index = 0; index < _instanceAwarePredicates.Count; index++)
if (!_instanceAwarePredicates[index](instance, item))
yield return _instanceAwarePredicateErrors[index];
if (_childValidator is not null && item is not null)
foreach (Error childError in _childValidator.Validate(item))
yield return childError;
}
}
}