Customize rules

StopOnFirstError()

Use this rule to stop all left validation on the chain when the current rule is violated. Normally, we should call StopOnFirstError() at the beginning of the chain before other rules.
Example:
RuleFor(person => person.Address)
        .StopOnFirstError()
        .NotNull()
        .NotEmpty();
On the example above, if the Address is null, the NotEmpty() rull will not be executed and there is just one error message return which is from NotNull() validation rule.

When()

Use this rule to make the previous validation rule to be validated only if the condition specified in the When() is matched.
Example:
RuleFor(person => person.Address.StreetLine)
        .NotEmpty()
        .When(person => person.Address != null);
On the example above, the Address StreetLine will be validated with NotEmpty validation only if the person.Address is not null.

Unless()

Use this rule to make the previous validation rule to be validated as normal but will be ignored if the condition specified in the Unless() is matched.
Example:
RuleFor(person => person.Address)
        .NotNull()
        .Unless(person => person.HasAddress == false);
On the example above, the Address will always be validated with NotNull validation and it can be null when person.HasAddress == false.

Set a condition on a group of rules

If you want to group a set of rules under the same condition, you can use the built-in When method of TypeValidator<T>
Example:
When(person => person.Address != null, () => 
{
        RuleFor(x => x.Address.StreetLine).NotNull();
        RuleFor(x => x.Address.PostCode).NotNull();
        RuleFor(x => x.Address.State).NotNull();
});
On the example above, the Address StreetLine, PostCode and State will be validated with if the person.Address is not null.

IgnoreValidation()

Use this rule to ignore all validation errors on a specific chain name by providing a lambda expression to the property we want to ignore. This rule affect all validation rules to the whole Type/Composite validator class.
Any nested properties from that specified property will also be ignored.
This rule can be call at anytime inside your TypeValidator or CompositeValidator constructor.
If you want something like IgnoreValidation().When(), use Unless() instead :)
Example:
RuleFor(person => person.Address)
        .SetValidator<AddressValidator>();

RuleFor(person => person.Address.StreetLine)
        .IgnoreValidation();
On the example above, the Address object of the person will be validated by AddressValidator. However, any error occurs for Address.StreetLine will be ignore.

WithMessage()

Use this method to customize the error message of the previous rule. There are two options to use this method, one with simple a string value to override the default error message of the previous rule, the other one is a Func<T> value that can be used to provide more detail about the object to be validated in the error message.

Example:
RuleFor(person => person.Address.StreetLine)
             .Length(10, 50)
             .WithMessage("StreetLine must be longer than 10 and shorter than 50");
RuleFor(person => person.Address.StreetLine)
             .Length(10, 50)
             .WithMessage(x => string.Format("StreetLine must be longer than 10 and shorter than 50, you enter {0} in length.", x.Address.StreetLine.Length));

AllWithMessage()

Use this method to customize the error messages for the all previous rules in the chain.

Example:
RuleFor(person => person.Address.StreetLine).NotNull()
                                            .NotEmpty()
                                            .Length(10, 50)
                                            .AllWithMessage(x => string.Format("StreetLine must be longer than 10 and shorter than 50, you enter {0}.", x.Address.StreetLine));

Last edited Dec 9, 2011 at 9:34 AM by nvthoai, version 7

Comments

No comments yet.