Built-in validators

NValidator has quite alot built-in validators that can cover most of the validation logics:

In() Validator

Use this rule to ensures that the property value must be one of the specified values. Null value is not allowed.
This rule can be used as a combination with Not() as Not().In().
Example:
RuleFor(person => person.Title).In("MR", "MS", "MRS", "DR", "PROF", "REV", "OTHER");
Default error message: @PropertyName must be one of the allowed values. Allowed values: @AllowedValues.
Default negated error : @PropertyName must not be one of the following values: @ProhibitValues.
  • @PropertyName: the name of the property to be validated
  • @AllowedValues: the list of specified values separated by comma.
  • @ProhibitValues: the list of specified values separated by comma. Only available when the rule is used as Not().In()
Sample error message: "Title must be one one of the allowed values. Allowed values: MR, MS, MRS, DR, PROF, REV, OTHER."
Sample negated error : "Title must not be one one of the following values: MR, MS, MRS, DR, PROF, REV, OTHER." (Haha it does not make sense to have a silly rule like this)

Allow() Validator

Same as In(). Use this rule to ensures that the property value can be one of the specified values. Different to In(), null value is allowed.
This rule can be used as a combination with Not() as Not().Allow()
Example:
RuleFor(person => person.Title).Allow("MR", "MS", "MRS", "DR", "PROF", "REV", "OTHER");
Default error message: @PropertyName must be one of the allowed values. Allowed values: @AllowedValues.
Default negated error : @PropertyName must not be one of the following values: @ProhibitValues.
  • @PropertyName: the name of the property to be validated
  • @AllowedValues: the list of specified values separated by comma.
  • @ProhibitValues: the list of specified values separated by comma. Only available when the rule is used as Not().Allow()
Sample error message: "Title must be one one of the allowed values. Allowed values: MR, MS, MRS, DR, PROF, REV, OTHER."
Sample negated error : "Skill must not be one one of the following values: Powerpoint, Word, Excel.."

Null() Validator

Use this rule to ensures that the property value must be null. This rule is often used as a combination with Not() as Not().Null()
Example:
RuleFor(person => person.Title).Null();
Default error message: @PropertyName must be null.
Default negated error : @PropertyName must not be null.
  • @PropertyName: the name of the property to be validated
Sample error message: "Title must be null."
Sample negated error : "Title must not be null."

NotNull() Validator

Use this rule to ensures that the property value must not be null. This rule can not be used as a combination with Not() as Not().NotNull().
Well if you want something like Not().NotNull(), use Null() instead :)
Example:
RuleFor(person => person.Title).NotNull();
Default error message: @PropertyName must not be null.
  • @PropertyName: the name of the property to be validated
Sample error message: "Title must not be null."

NotEmpty() Validator

Use this rule to ensures that the enumerable property value must not be empty. This rule can not be used as a combination with Not() as Not().NotEmpty().
Example:
RuleFor(person => person.Title).NotEmpty();
Default error message: @PropertyName cannot be empty.
  • @PropertyName: the name of the property to be validated
Sample error message: "Title cannot be empty."

Length() Validator

Use this rule to ensures that the enumerable property value must have the expected length. This rule can not be used as a combination with Not() as Not().Length().
You can use this rule with any property type that implement IEnumerable like a list, a collection, an array or simply a string.
Example:
RuleFor(person => person.Title).Length(4, 10);
Default error message: @PropertyName must be between @MinimumLength and @MaximumLength in length. You had @TotalLength in length.
  • @PropertyName: the name of the property to be validated
  • @MinimumLength: the minimum value in length that was specified
  • @MaximumLength: the maximum value in length that was specified
  • @TotalLength: the length of the property to be validated
Sample error message: "Title must be between 4 and 10 in length. You had 3 in length."

NotEqual() Validator

Use this rule to ensures that the property value must not equal to the expected value. This rule can not be used as a combination with Not() as Not().NotEqual().
You can also use this rule to ensure that the specified property must not equal to the value of other property by specifying a lambda expression instead of a constant value.
Example:
RuleFor(person => person.Skill).NotEqual("MSWORD");
RuleFor(person => person.Password).NotEqual(x => x.UserName);
Default error message: @PropertyName must not equal to @ComparisonValue.
  • @PropertyName: the name of the property to be validated
  • @ComparisonValue: the value that is used for comparison
Sample error message: "Skill must not equal to MSWORD."

Equal() Validator

Use this rule to ensures that the property value must equal to the expected value. This rule can be used as a combination with Not() as Not().Equal().
You can also use this rule to ensure that the specified property must equal to the value of other property by specifying a lambda expression instead of a constant value.
Example:
RuleFor(person => person.Skill).Equal("C#");
RuleFor(person => person.Password).Equal(x => x.ConfirmPassword);
Default error message: @PropertyName must equal to @ComparisonValue.
Default negated error : @PropertyName must not equal to @ComparisonValue.
  • @PropertyName: the name of the property to be validated
  • @ComparisonValue: the value that is used for comparison
Sample error message: "Skill must equal to C#."
Sample negated error : "Skill must not equal to MSWORD."

LessThan() Validator

Use this rule to ensures that the property value must be less than an expected value. This rule can be used as a combination with Not() as Not().LessThan().
You can also use this rule to ensure that the specified property must be less than the value of other property by specifying a lambda expression instead of a constant value.
Example:
RuleFor(person => person.Age).LessThan(100);
RuleFor(account => account.WithdrawnAmount).LessThan(x => x.Balance);
Default error message: @PropertyName must be less than @ComparisonValue.
Default negated error : @PropertyName must be greater than or equal @ComparisonValue.
  • @PropertyName: the name of the property to be validated
  • @ComparisonValue: the value that is used for comparison
Sample error message: "Age must be less than 100."
Sample negated error : "Age must be greater than or equal 1."

LessThanOrEqual() Validator

Use this rule to ensures that the property value must be less than or equal an expected value. This rule can be used as a combination with Not() as Not().LessThanOrEqual().
You can also use this rule to ensure that the specified property must be less than or equal the value of other property by specifying a lambda expression instead of a constant value.
Example:
RuleFor(person => person.Age).LessThanOrEqual(100);
RuleFor(account => account.WithdrawnAmount).LessThanOrEqual(x => x.Balance);
Default error message: @PropertyName must be less than or equal @ComparisonValue.
Default negated error : @PropertyName must be greater than @ComparisonValue.
  • @PropertyName: the name of the property to be validated
  • @ComparisonValue: the value that is used for comparison
Sample error message: "Age must be less than or equal 100."
Sample negated error : "Age must be greater than 0."

GreaterThan() Validator

Use this rule to ensures that the property value must be greater than an expected value. This rule can be used as a combination with Not() as Not().GreaterThan().
You can also use this rule to ensure that the specified property must be greater than the value of other property by specifying a lambda expression instead of a constant value.
Example:
RuleFor(person => person.Age).GreaterThan(0);
RuleFor(account => account.Balance).GreaterThan(x => x.SavingAccount);
Default error message: @PropertyName must be greater than @ComparisonValue.
Default negated error : @PropertyName must be less than or equal @ComparisonValue.
  • @PropertyName: the name of the property to be validated
  • @ComparisonValue: the value that is used for comparison
Sample error message: "Age must be greater than 0."
Sample negated error : "Mark must be less than or equal 100."

GreaterThanOrEqual() Validator

Use this rule to ensures that the property value must be greater than or equal an expected value. This rule can be used as a combination with Not() as Not().GreaterThanOrEqual().
You can also use this rule to ensure that the specified property must be greater than or equal the value of other property by specifying a lambda expression instead of a constant value.
Example:
RuleFor(person => person.Age).GreaterThanOrEqual(0);
RuleFor(account => account.Balance).GreaterThanOrEqual(x => x.SavingAccount);
Default error message: @PropertyName must be greater than or equal @ComparisonValue.
Default negated error : @PropertyName must be less than @ComparisonValue.
  • @PropertyName: the name of the property to be validated
  • @ComparisonValue: the value that is used for comparison
Sample error message: "Age must be greater than or equal 0."
Sample negated error : "WithdrawnPerDay must be less than 1500."

Must() Validator

Use this rule to ensures that the property value must match the specified predicate condition. This rule can not be used as a combination with Not() as Not().Must().
Example:
RuleFor(person => person.Address).Must(address => !string.IsNullOrEmpty(address.StreetLine));
RuleFor(person => person.Address2).Must((person, address2) => person.Address1 != null && person.Address1 != address2);
Default error message: @PropertyName does not match condition.
  • @PropertyName: the name of the property to be validated
Sample error message: "Address does not match condition."

Match() Validator

Use this rule to ensures that the property string value must match the specified regular expression. This rule can not be used as a combination with Not() as Not().Match().
You can specify whether allow null value to be valid or not.
Example:
RuleFor(account => account.Number).Match(@"^\d{4}-\d{5}$"); //xxxx-xxxxx
RuleFor(account => account.Number).Match(@"^\d{4}-\d{5}$", true); //xxxx-xxxxx or NULL
Default error message: @PropertyName is not in the correct format.
  • @PropertyName: the name of the property to be validated
Sample error message: "Number is not in the correct format."

Email() Validator

Use this rule to ensures that the property string value must be a valid email format. This rule can not be used as a combination with Not() as Not().Email().
Example:
RuleFor(user => user.Email).Email();
Default error message: @PropertyName is not a valid email address.
  • @PropertyName: the name of the property to be validated
Sample error message: "Email is not a valid email address."

ValidateUsing() Validator

If you have a set of defined data annotation validation attribute, you can reuse those logic using this rule. This rule can not be used as a combination with Not() as Not().ValidateUsing().
Example:
RuleFor(address => address.StreetLine).ValidateUsing(typeof(RequiredAttribute));
Default error message: will be the message return by the data annotation attribute.

Not() Validator

Basically, it's will negate the logic of the following validation rule as I described above. Not() validator can be used as a combination with some validation rule only, such as:

- Null()
- Equal()
- LessThan()
- LessThanOrEqual()
- GreaterThan()
- GreaterThanOrEqual()
- In()
- Allow()

If you want to make a your custom validation rule work with Not(), the custom validator must implement an extra interface: INegatableValidator. There is a base class for you to inherit: "BaseNegatableValidator<T>"

Example:
RuleFor(address => address.StreetLine).Not().Null().Not().Empty();
Default error message: will be the message return by the negatable validator followed after Not().

Apply custom validator by calling SetValidator()

If you have a custom validator by implementing the interface IValidator or IValidator<T>, you can use it as a rule in the chain by calling SetValidator().
Example:
RuleFor(user => user.Address).SetValidator<AddressValidator>();
RuleFor(user => user.Address).SetValidator(typeof(AddressValidator));
Default error message: will be the message return by the custom validator.

Last edited Dec 8, 2011 at 9:44 AM by nvthoai, version 13

Comments

No comments yet.