#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 a specific property of an instance. /// /// The type of the instance being validated. /// The type of the specific property on the instance that the rule applies to. internal sealed class ValidationRule : IValidationRule, IRuleBuilder { private readonly Func _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> _predicates = []; private readonly List _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> _instanceAwarePredicates = []; private readonly List _instanceAwarePredicateErrors = []; private Error currentError = Error.Validation(); private Func? 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 isLastAddedPredicateInstanceAware; private AbstractValidator? _childValidator; /// /// Initializes a new instance of the class. /// /// A function that extracts the property value from the request instance. public ValidationRule(Func propertySelector) { _propertySelector = propertySelector; } /// /// Adds a condition that the property must satisfy to be considered valid. /// /// A function to test the property value. /// The current instance for method chaining. public IRuleBuilder Must(Func 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); isLastAddedPredicateInstanceAware = false; return this; } /// /// Adds a condition that the request instance together with the property must satisfy to be considered valid. /// /// A function to test the request instance together with the property value. /// The current instance for method chaining. public IRuleBuilder Must(Func 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); isLastAddedPredicateInstanceAware = 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) { // 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 (isLastAddedPredicateInstanceAware && _instanceAwarePredicateErrors.Count > 0) _instanceAwarePredicateErrors[^1] = validationError; else if (!isLastAddedPredicateInstanceAware && _predicateErrors.Count > 0) _predicateErrors[^1] = validationError; return this; } /// /// Defines nested validation rules for the complex property being validated. /// /// An action used to configure the inline validator for the property. /// The current instance for method chaining. public IRuleBuilder ChildRules(Action> 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 childValidator = new(); configure(childValidator); _childValidator = childValidator; return this; } /// /// Evaluates all configured predicates against the specified request instance. /// /// The request instance to validate. /// The of every predicate that fails; empty if the rule 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; TProperty value = _propertySelector(instance); // 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; } }