#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.Primitives;
using System;
using System.Collections.Generic;
#endregion
namespace Lumina.Presentation.Web.Common.Validation;
///
/// Base class for creating validators for a specific type.
///
/// The type of the instance to be validated.
public abstract class AbstractValidator : IValidator
{
private readonly List> _rules = [];
///
/// Defines a validation rule for a specific property on the request being validated.
///
/// The type of the property being validated.
/// A function that selects the property to validate from the request instance.
/// An instance to configure the rule fluently.
public IRuleBuilder RuleFor(Func propertySelector)
{
ValidationRule rule = new(propertySelector);
_rules.Add(rule);
return rule;
}
///
/// Defines a validation rule that is evaluated against each item within a collection property on the request being validated.
///
/// The type of the items in the collection being validated.
/// A function that selects the collection to validate from the request instance.
/// An instance to configure the per-item rule fluently.
public IRuleBuilder RuleForEach(Func?> collectionSelector)
{
ValidationRuleForEach rule = new(collectionSelector);
_rules.Add(rule);
return rule;
}
///
/// Validates the specified request instance against all defined rules.
///
/// The request instance to validate.
/// A list of validation errors. If the instance is valid, the list will be empty.
public List Validate(TRequest instance)
{
List errors = [];
foreach (IValidationRule rule in _rules)
errors.AddRange(rule.Validate(instance));
return errors;
}
}