#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.Primitives;
using System;
using System.Collections.Generic;
#endregion
namespace Lumina.Presentation.Web.Common.Validation;
///
/// Defines a validation rule that evaluates each item within a collection property of an instance.
///
/// The type of the instance being validated.
/// The type of the items in the collection that the rule applies to.
internal sealed class ValidationRuleForEach : IValidationRule, IRuleBuilder
{
private readonly Func?> _collectionSelector;
private readonly List> _predicates = [];
private readonly List _predicateErrors = [];
private readonly List> _instanceAwarePredicates = [];
private readonly List _instanceAwarePredicateErrors = [];
private Error currentError = Error.Validation();
private Func? condition;
private bool lastAddedPredicateIsInstanceAware;
private AbstractValidator? _childValidator;
///
/// Initializes a new instance of the class.
///
/// A function that extracts the collection to iterate from the request instance.
public ValidationRuleForEach(Func?> collectionSelector)
{
_collectionSelector = collectionSelector;
}
///
/// Adds a condition that each item in the collection must satisfy to be considered valid.
///
/// A function to test each item value.
/// The current instance for method chaining.
public IRuleBuilder Must(Func predicate)
{
_predicates.Add(predicate);
_predicateErrors.Add(currentError);
lastAddedPredicateIsInstanceAware = false;
return this;
}
///
/// Adds a condition that the request instance together with each item must satisfy to be considered valid.
///
/// A function to test the request instance together with each item value.
/// The current instance for method chaining.
public IRuleBuilder Must(Func predicate)
{
_instanceAwarePredicates.Add(predicate);
_instanceAwarePredicateErrors.Add(currentError);
lastAddedPredicateIsInstanceAware = true;
return this;
}
///
/// Specifies a condition that must be met for the validation rule to be executed.
///
/// A function evaluating the request instance.
/// The current instance for method chaining.
public IRuleBuilder When(Func condition)
{
this.condition = condition;
return this;
}
///
/// Specifies a custom object to be returned when the validation fails.
///
/// The specific error to return upon failure.
/// The current instance for method chaining.
public IRuleBuilder WithError(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;
}
///
/// Defines nested validation rules for each item of the collection being validated.
///
/// An action used to configure the inline validator for each item.
/// The current instance for method chaining.
public IRuleBuilder ChildRules(Action> configure)
{
InlineValidator childValidator = new();
configure(childValidator);
_childValidator = childValidator;
return this;
}
///
/// Evaluates all configured predicates against each item in the collection.
///
/// The request instance to validate.
/// One per failing predicate, per item; empty if every item passes.
public IEnumerable Validate(TRequest instance)
{
// evaluate the condition first, if it returns false, skip validation entirely.
if (condition is not null && !condition(instance))
yield break;
IEnumerable? collection = _collectionSelector(instance);
if (collection is null)
yield break;
foreach (TItem item in collection)
{
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;
}
}
}