#region ========================================================================= USING ===================================================================================== using Lumina.Application.Common.Infrastructure.Validation; using System; using System.Collections; using System.Text.RegularExpressions; #endregion namespace Lumina.Application.Common.Utilities; /// /// Validator extension methods for the to define common validation rules. /// public static class ValidatorUtilities { /// /// Defines a validation rule that ensures the integer property is greater than the specified value. /// /// The type of the request being validated. /// The rule builder instance. /// The value that the property must be greater than. /// The current instance for method chaining. public static IRuleBuilder GreaterThan(this IRuleBuilder ruleBuilder, int valueToCompare) { return ruleBuilder.Must(value => value > valueToCompare); } /// /// Defines a validation rule that ensures the nullable integer property is greater than the specified value. /// /// /// If the property is , this rule passes (use to check for missing values). /// /// The type of the request being validated. /// The rule builder instance. /// The value that the property must be greater than. /// The current instance for method chaining. public static IRuleBuilder GreaterThan(this IRuleBuilder ruleBuilder, int valueToCompare) { return ruleBuilder.Must(value => !value.HasValue || value.Value > valueToCompare); } /// /// Defines a validation rule that ensures the long property is greater than the specified value. /// /// The type of the request being validated. /// The rule builder instance. /// The value that the property must be greater than. /// The current instance for method chaining. public static IRuleBuilder GreaterThan(this IRuleBuilder ruleBuilder, long valueToCompare) { return ruleBuilder.Must(value => value > valueToCompare); } /// /// Defines a validation rule that ensures the nullable long property is greater than the specified value. /// /// /// If the property is , this rule passes (use to check for missing values). /// /// The type of the request being validated. /// The rule builder instance. /// The value that the property must be greater than. /// The current instance for method chaining. public static IRuleBuilder GreaterThan(this IRuleBuilder ruleBuilder, long valueToCompare) { return ruleBuilder.Must(value => !value.HasValue || value.Value > valueToCompare); } /// /// Defines a validation rule that ensures the decimal property is greater than the specified value. /// /// The type of the request being validated. /// The rule builder instance. /// The value that the property must be greater than. /// The current instance for method chaining. public static IRuleBuilder GreaterThan(this IRuleBuilder ruleBuilder, decimal valueToCompare) { return ruleBuilder.Must(value => value > valueToCompare); } /// /// Defines a validation rule that ensures the nullable decimal property is greater than the specified value. /// /// /// If the property is , this rule passes (use to check for missing values). /// /// The type of the request being validated. /// The rule builder instance. /// The value that the property must be greater than. /// The current instance for method chaining. public static IRuleBuilder GreaterThan(this IRuleBuilder ruleBuilder, decimal valueToCompare) { return ruleBuilder.Must(value => !value.HasValue || value.Value > valueToCompare); } /// /// Defines a validation rule that ensures the float property is greater than the specified value. /// /// The type of the request being validated. /// The rule builder instance. /// The value that the property must be greater than. /// The current instance for method chaining. public static IRuleBuilder GreaterThan(this IRuleBuilder ruleBuilder, float valueToCompare) { return ruleBuilder.Must(value => value > valueToCompare); } /// /// Defines a validation rule that ensures the nullable float property is greater than the specified value. /// /// /// If the property is , this rule passes (use to check for missing values). /// /// The type of the request being validated. /// The rule builder instance. /// The value that the property must be greater than. /// The current instance for method chaining. public static IRuleBuilder GreaterThan(this IRuleBuilder ruleBuilder, float valueToCompare) { return ruleBuilder.Must(value => !value.HasValue || value.Value > valueToCompare); } /// /// Defines a validation rule that ensures the time span property is greater than the specified value. /// /// The type of the request being validated. /// The rule builder instance. /// The value that the property must be greater than. /// The current instance for method chaining. public static IRuleBuilder GreaterThan(this IRuleBuilder ruleBuilder, TimeSpan valueToCompare) { return ruleBuilder.Must(value => value > valueToCompare); } /// /// Defines a validation rule that ensures the integer property is greater than or equal to the specified value. /// /// The type of the request being validated. /// The rule builder instance. /// The value that the property must be greater than or equal to. /// The current instance for method chaining. public static IRuleBuilder GreaterThanOrEqualTo(this IRuleBuilder ruleBuilder, int valueToCompare) { return ruleBuilder.Must(value => value >= valueToCompare); } /// /// Defines a validation rule that ensures the nullable integer property is greater than or equal to the specified value. /// /// /// If the property is , this rule passes. /// /// The type of the request being validated. /// The rule builder instance. /// The value that the property must be greater than or equal to. /// The current instance for method chaining. public static IRuleBuilder GreaterThanOrEqualTo(this IRuleBuilder ruleBuilder, int valueToCompare) { return ruleBuilder.Must(value => !value.HasValue || value.Value >= valueToCompare); } /// /// Defines a validation rule that ensures the nullable decimal property is greater than or equal to the specified value. /// /// /// If the property is , this rule passes. /// /// The type of the request being validated. /// The rule builder instance. /// The value that the property must be greater than or equal to. /// The current instance for method chaining. public static IRuleBuilder GreaterThanOrEqualTo(this IRuleBuilder ruleBuilder, decimal valueToCompare) { return ruleBuilder.Must(value => !value.HasValue || value.Value >= valueToCompare); } /// /// Defines a validation rule that ensures the property is not . /// /// /// This rule checks reference/nullable-value nullness only. Combine with /// to also reject empty values. /// /// The type of the request being validated. /// The type of the property being validated. /// The rule builder instance. /// The current instance for method chaining. public static IRuleBuilder NotNull(this IRuleBuilder ruleBuilder) { return ruleBuilder.Must(value => value is not null); } /// /// Defines a validation rule that ensures the property is not empty. /// /// /// This rule is type-aware: a is checked for white-space only content, a /// is checked against , and any is checked for having at least /// one item. If the property is , this rule fails. /// /// The type of the request being validated. /// The type of the property being validated. /// The rule builder instance. /// The current instance for method chaining. public static IRuleBuilder NotEmpty(this IRuleBuilder ruleBuilder) { return ruleBuilder.Must(value => value switch { null => false, string stringValue => stringValue.Trim().Length > 0, Guid guidValue => guidValue != Guid.Empty, IEnumerable enumerable => enumerable.GetEnumerator().MoveNext(), _ => true }); } /// /// Defines a validation rule that ensures the value is a defined member of the specified enumeration type. /// /// The type of the request being validated. /// The type of the enumeration. /// The rule builder instance. /// The current instance for method chaining. public static IRuleBuilder IsInEnum(this IRuleBuilder ruleBuilder) where TEnum : struct, Enum { return ruleBuilder.Must(value => Enum.IsDefined(value)); } /// /// Defines a validation rule that ensures a nullable enum value is either or a defined member of the specified enumeration type. /// /// The type of the request being validated. /// The type of the enumeration. /// The rule builder instance. /// The current instance for method chaining. public static IRuleBuilder IsInEnum(this IRuleBuilder ruleBuilder) where TEnum : struct, Enum { return ruleBuilder.Must(value => value.HasValue && Enum.IsDefined(value.Value)); } /// /// Defines a validation rule that ensures the string property does not exceed the specified maximum length. /// /// /// If the property is or not a string, this rule passes. /// /// The type of the request being validated. /// The type of the property being validated. /// The rule builder instance. /// The maximum allowed length of the property. /// The current instance for method chaining. public static IRuleBuilder MaximumLength(this IRuleBuilder ruleBuilder, int maximumLength) { return ruleBuilder.Must(value => value is not string stringValue || stringValue.Length <= maximumLength); } /// /// Defines a validation rule that ensures the string property is at least the specified minimum length. /// /// /// If the property is or not a string, this rule fails. /// /// The type of the request being validated. /// The type of the property being validated. /// The rule builder instance. /// The minimum allowed length of the property. /// The current instance for method chaining. public static IRuleBuilder MinimumLength(this IRuleBuilder ruleBuilder, int minimumLength) { return ruleBuilder.Must(value => value is string stringValue && stringValue.Length >= minimumLength); } /// /// Defines a validation rule that ensures the string property has exactly the specified length. /// /// /// If the property is or not a string, this rule fails. /// /// The type of the request being validated. /// The type of the property being validated. /// The rule builder instance. /// The exact allowed length of the property. /// The current instance for method chaining. public static IRuleBuilder Length(this IRuleBuilder ruleBuilder, int length) { return ruleBuilder.Must(value => value is string stringValue && stringValue.Length == length); } /// /// Defines a validation rule that ensures the string property length is within the specified inclusive range. /// /// /// If the property is or not a string, this rule fails. /// /// The type of the request being validated. /// The type of the property being validated. /// The rule builder instance. /// The minimum allowed length of the property. /// The maximum allowed length of the property. /// The current instance for method chaining. public static IRuleBuilder Length(this IRuleBuilder ruleBuilder, int minimumLength, int maximumLength) { return ruleBuilder.Must(value => value is string stringValue && stringValue.Length >= minimumLength && stringValue.Length <= maximumLength); } /// /// Defines a validation rule that ensures the string property matches the specified regular expression pattern. /// /// /// If the property is or not a string, this rule fails (use /// to skip optional properties). /// /// The type of the request being validated. /// The type of the property being validated. /// The rule builder instance. /// The regular expression pattern the property must match. /// The current instance for method chaining. public static IRuleBuilder Matches(this IRuleBuilder ruleBuilder, string regexPattern) { return ruleBuilder.Must(value => value is string stringValue && Regex.IsMatch(stringValue, regexPattern)); } /// /// Defines a validation rule that ensures the string property matches the regular expression pattern selected from the request instance. /// /// /// If the property is or not a string, this rule fails. /// /// The type of the request being validated. /// The type of the property being validated. /// The rule builder instance. /// A function that selects the regular expression pattern to match against from the request instance. /// The current instance for method chaining. public static IRuleBuilder Matches(this IRuleBuilder ruleBuilder, Func regexPatternSelector) { return ruleBuilder.Must((instance, value) => value is string stringValue && Regex.IsMatch(stringValue, regexPatternSelector(instance))); } /// /// Defines a validation rule that ensures the integer property is within the specified inclusive range. /// /// The type of the request being validated. /// The rule builder instance. /// The minimum allowed value of the property. /// The maximum allowed value of the property. /// The current instance for method chaining. public static IRuleBuilder InclusiveBetween(this IRuleBuilder ruleBuilder, int minimumValue, int maximumValue) { return ruleBuilder.Must(value => value >= minimumValue && value <= maximumValue); } /// /// Defines a validation rule that ensures the nullable integer property is within the specified inclusive range. /// /// /// If the property is , this rule passes (use to check for missing values). /// /// The type of the request being validated. /// The rule builder instance. /// The minimum allowed value of the property. /// The maximum allowed value of the property. /// The current instance for method chaining. public static IRuleBuilder InclusiveBetween(this IRuleBuilder ruleBuilder, int minimumValue, int maximumValue) { return ruleBuilder.Must(value => !value.HasValue || (value.Value >= minimumValue && value.Value <= maximumValue)); } /// /// Defines a validation rule that ensures the property is equal to another property of the request. /// /// The type of the request being validated. /// The type of the property being validated. /// The rule builder instance. /// A function that selects the property the validated property must equal. /// The current instance for method chaining. public static IRuleBuilder Equal(this IRuleBuilder ruleBuilder, Func comparisonSelector) { return ruleBuilder.Must((instance, value) => Equals(value, comparisonSelector(instance))); } }