Fluent Validation - check string after trimming

I have a game with http://fluentvalidation.codeplex.com/ to test some domain models.

I have a typical scenario where I want to check a string, for example ...

RuleFor(x => x.MyString).NotNull().NotEmpty().Length(2, 20).WithMessage("Please provide a string with a minium of 2 characters.");

... everything works fine and dandy, until I will create a unit test, which indicates that the MyString property must have a length of 2-20 characters not including spaces.

So that myObject.myString = "A" + new String(' ', 10);should be done with an error.

I can make it all work with .Must(IsValidString)and write all the logic in ...

    private bool IsValidString(string myString)
    {
       if(String.IsNullOrEmpty(myString))
           return false;

       // Then work on myString.Trim()'ed value. 
    }

... but then I lose all the wonderful self-confidence !!

Obviously, I can make my unit test pass using this method and everyone will be happy in my small world, but did I miss the trick?

Many thanks.

+4
2

dll FluentValidation dll http://ilspy.net/, , TrimmedLengthValidator...

public static class DefaultValidatorExtensions
    {
        public static IRuleBuilderOptions<T, string> TrimmedLength<T>(this IRuleBuilder<T, string> ruleBuilder, int min, int max)
        {
            return ruleBuilder.SetValidator(new TrimmedLengthValidator(min, max));
        }
    }

public class TrimmedLengthValidator : PropertyValidator, ILengthValidator, IPropertyValidator
    {
        public int Min { get; private set; }
        public int Max { get; private set; }

        public TrimmedLengthValidator(int min, int max)
            : this(min, max, () => Messages.length_error)
        { }

        public TrimmedLengthValidator(int min, int max, Expression<Func<string>> errorMessageResourceSelector)
            : base(errorMessageResourceSelector)
        {
            this.Max = max;
            this.Min = min;

            if (max != -1 && max < min)
                throw new ArgumentOutOfRangeException("max", "Max should be larger than min.");
        }

        protected override bool IsValid(PropertyValidatorContext context)
        {
            if (context.PropertyValue == null)
                return true;

            int length = context.PropertyValue.ToString().Trim().Length;

            if (length < this.Min || (length > this.Max && this.Max != -1))
            {
                context.MessageFormatter.AppendArgument("MinLength", this.Min).AppendArgument("MaxLength", this.Max).AppendArgument("TotalLength", length);
                return false;
            }
            return true;
        }
    }

... , :

RuleFor(x => x.myString).NotEmpty().TrimmedLength(2, 20).WithMessage("Please provide a string with a minium of 2 characters.");

!

+4

, - ? - , ( ),

RuleFor(x => x.MyString)
    .NotNull()
    .NotEmpty()
    .Length(2, 20)
        .WithMessage("Please provide a string with a minium of 2 characters.")
    .Must(myString => myString == Regex.Replace( myString, @"s", "" ))
+1

Source: https://habr.com/ru/post/1526748/


All Articles