36

Originally when writing validation logic for strings I settled on using NotEmpty for any string that was required. When using .NotEmpty().Length(min, max) this will cause two errors to be returned instead of just one when an empty string is passed in.

How can the redundant errors be prevented?

Brett Allen
  • 5,297
  • 5
  • 32
  • 62

2 Answers2

60

.Length(min, max) will not return an error if the string is null, but will return an error when the string is empty and min is greater than 0. There are two ways to implement a required string with a minimum length greater than 0.

The typical way to stop on the first error is to use Cascade method:

    RuleFor(o => o.PropertyName)
        .Cascade(CascadeMode.StopOnFirstFailure)
        .NotEmpty() // Will return an error if null or empty
        .Length(2, 10) // Will only return an error if length == 1 or > than 10

However for strings, it is easier to read the following:

    RuleFor(o => o.PropertyName)
        .NotNull()
        .Length(2, 10) // Will not return an error on null

String validation scenarios using NotNull, NotEmpty and Length:

Optional with a max length:

    RuleFor(o => o.PropertyName).Length(0, max);


Optional with a min and max length:

    RuleFor(o => o.PropertyName).Length(min, max);


Required but can have a zero length:

    RuleFor(o => o.PropertyName).NotNull()


Required and must have a non-zero length:

    RuleFor(o => o.PropertyName).NotEmpty();


Required and has a max length:

    RuleFor(o => o.PropertyName).NotNull().Length(0, max);


Required and has a min and max length:

    RuleFor(o => o.PropertyName).NotNull().Length(min, max);
Brett Allen
  • 5,297
  • 5
  • 32
  • 62
  • 8
    It seems VERY strange to me that `RuleFor(o => o.PropertyName).Length(min, max)` will not return an error if string is `null`. Why is that? Is there any logic behind this behaviour? Because I can't see any... I think the mechanism that validates the string should receive a `NullReferenceException` or something like that, because it tests lenght of an object that doesn't exist. Then why doesn't it throw any errors? – Salivan Nov 07 '15 at 21:43
  • 6
    Hi, As author said: every rule is doing one single job. NotNull for checking null and Length() for checking length of string. Hope that helps. Szczesc Boze. – Leszek P Apr 29 '16 at 08:20
  • 2
    @Salivan The use case can be directly tied to a HTTP Patch request where a property that is not included in properties are not updated. For String and Nullable it is extremely easy to validate that an included property is valid, but properties not included (JSON) are ignored (they would be mapped as null). Extremely easy to update on those properties using entity framework that are not null. – Erik Philips Apr 05 '21 at 19:32
5

Another way to prevent additional errors from occurring would be to set the cascade mode.

RuleFor(x => x.PropName)
    .Cascade(CascadeMode.StopOnFirstFailure)
    .NotEmpty()
    .Length(min, max);
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
bschreck
  • 183
  • 1
  • 9