skip to Main Content

I Have to validate the max length of an int or long with FluentValidation

I want to use something like this

int PortCode = 4444;
RuleFor(x => x.PortCode).NotEmpty().MaximumLength(10);

but FluentValidation does not support it

what is the solution;

2

Answers


  1. Chosen as BEST ANSWER

    please use this custom validation

    using FluentValidation;

    public static class ValidationHelper
    {
        public static IRuleBuilderOptions<T, short> MaximumLength<T>(this IRuleBuilder<T, short> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs((double)n)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
        public static IRuleBuilderOptions<T, short?> MaximumLength<T>(this IRuleBuilder<T, short?> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs((double)n.Value)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
        public static IRuleBuilderOptions<T, int> MaximumLength<T>(this IRuleBuilder<T, int> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs((double)n)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
        public static IRuleBuilderOptions<T, int?> MaximumLength<T>(this IRuleBuilder<T, int?> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs((double)n.Value)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
        public static IRuleBuilderOptions<T, long> MaximumLength<T>(this IRuleBuilder<T, long> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs((double)n)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
        public static IRuleBuilderOptions<T, long?> MaximumLength<T>(this IRuleBuilder<T, long?> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs((double)n.Value)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
        public static IRuleBuilderOptions<T, decimal> MaximumLength<T>(this IRuleBuilder<T, decimal> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs((double)n)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
        public static IRuleBuilderOptions<T, decimal?> MaximumLength<T>(this IRuleBuilder<T, decimal?> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs((double)n.Value)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
        public static IRuleBuilderOptions<T, double> MaximumLength<T>(this IRuleBuilder<T, double> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs(n)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
        public static IRuleBuilderOptions<T, double?> MaximumLength<T>(this IRuleBuilder<T, double?> rule, int maximumLength) => rule.Must(n => Math.Log10(Math.Abs(n.Value)) <= maximumLength).WithMessage("The length of '{PropertyName}' must be '" + maximumLength + "' Digit or fewer.");
    }
    

  2. you don’t need custom validation

    you can do this instead

    RuleFor(a => a.PortCode).NotEmpty().Must(w => w.ToString().Length < 10);

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search