Skip to main content
Mentioned doc link updated
Source Link
Mehdi Dehghani
  • 11.7k
  • 6
  • 64
  • 68

Fluent validation supports conditional validation, just use the When clause to check the value of the secondary field:

https://fluentvalidation.net/start#conditionshttps://docs.fluentvalidation.net/en/latest/conditions.html

Specifying a condition with When/Unless The When and Unless methods can be used to specify conditions that control when the rule should execute. For example, this rule on the CustomerDiscount property will only execute when IsPreferredCustomer is true:

RuleFor(customer => customer.CustomerDiscount)
    .GreaterThan(0)
    .When(customer => customer.IsPreferredCustomer);

The Unless method is simply the opposite of When.

You may also be able to use the .SetValidator operation to define a custom validator that operates on the NotEmpty condition.

RuleFor(customer => customer.CustomerDiscount)
    .GreaterThan(0)
    .SetValidator(New MyCustomerDiscountValidator);

If you need to specify the same condition for multiple rules then you can call the top-level When method instead of chaining the When call at the end of the rule:

When(customer => customer.IsPreferred, () => {
   RuleFor(customer => customer.CustomerDiscount).GreaterThan(0);
   RuleFor(customer => customer.CreditCardNumber).NotNull();
});

This time, the condition will be applied to both rules. You can also chain a call to Otherwise which will invoke rules that don’t match the condition:

When(customer => customer.IsPreferred, () => {
   RuleFor(customer => customer.CustomerDiscount).GreaterThan(0);
   RuleFor(customer => customer.CreditCardNumber).NotNull();
}).Otherwise(() => {
  RuleFor(customer => customer.CustomerDiscount).Equal(0);
});

Fluent validation supports conditional validation, just use the When clause to check the value of the secondary field:

https://fluentvalidation.net/start#conditions

Specifying a condition with When/Unless The When and Unless methods can be used to specify conditions that control when the rule should execute. For example, this rule on the CustomerDiscount property will only execute when IsPreferredCustomer is true:

RuleFor(customer => customer.CustomerDiscount)
    .GreaterThan(0)
    .When(customer => customer.IsPreferredCustomer);

The Unless method is simply the opposite of When.

You may also be able to use the .SetValidator operation to define a custom validator that operates on the NotEmpty condition.

RuleFor(customer => customer.CustomerDiscount)
    .GreaterThan(0)
    .SetValidator(New MyCustomerDiscountValidator);

If you need to specify the same condition for multiple rules then you can call the top-level When method instead of chaining the When call at the end of the rule:

When(customer => customer.IsPreferred, () => {
   RuleFor(customer => customer.CustomerDiscount).GreaterThan(0);
   RuleFor(customer => customer.CreditCardNumber).NotNull();
});

This time, the condition will be applied to both rules. You can also chain a call to Otherwise which will invoke rules that don’t match the condition:

When(customer => customer.IsPreferred, () => {
   RuleFor(customer => customer.CustomerDiscount).GreaterThan(0);
   RuleFor(customer => customer.CreditCardNumber).NotNull();
}).Otherwise(() => {
  RuleFor(customer => customer.CustomerDiscount).Equal(0);
});

Fluent validation supports conditional validation, just use the When clause to check the value of the secondary field:

https://docs.fluentvalidation.net/en/latest/conditions.html

Specifying a condition with When/Unless The When and Unless methods can be used to specify conditions that control when the rule should execute. For example, this rule on the CustomerDiscount property will only execute when IsPreferredCustomer is true:

RuleFor(customer => customer.CustomerDiscount)
    .GreaterThan(0)
    .When(customer => customer.IsPreferredCustomer);

The Unless method is simply the opposite of When.

You may also be able to use the .SetValidator operation to define a custom validator that operates on the NotEmpty condition.

RuleFor(customer => customer.CustomerDiscount)
    .GreaterThan(0)
    .SetValidator(New MyCustomerDiscountValidator);

If you need to specify the same condition for multiple rules then you can call the top-level When method instead of chaining the When call at the end of the rule:

When(customer => customer.IsPreferred, () => {
   RuleFor(customer => customer.CustomerDiscount).GreaterThan(0);
   RuleFor(customer => customer.CreditCardNumber).NotNull();
});

This time, the condition will be applied to both rules. You can also chain a call to Otherwise which will invoke rules that don’t match the condition:

When(customer => customer.IsPreferred, () => {
   RuleFor(customer => customer.CustomerDiscount).GreaterThan(0);
   RuleFor(customer => customer.CreditCardNumber).NotNull();
}).Otherwise(() => {
  RuleFor(customer => customer.CustomerDiscount).Equal(0);
});
Add-on for the scope functions
Source Link
Serg
  • 7.6k
  • 4
  • 42
  • 58

Fluent validation supports conditional validation, just use the When clause to check the value of the secondary field:

https://fluentvalidation.net/start#conditions

Specifying a condition with When/Unless The When and Unless methods can be used to specify conditions that control when the rule should execute. For example, this rule on the CustomerDiscount property will only execute when IsPreferredCustomer is true:

RuleFor(customer => customer.CustomerDiscount).GreaterThan(0).When(customer => customer.IsPreferredCustomer);`

RuleFor(customer => customer.CustomerDiscount)
    .GreaterThan(0)
    .When(customer => customer.IsPreferredCustomer);

The Unless method is simply the opposite of When.

You may also be able to use the .SetValidator operation to define a custom validator that operates on the NotEmpty condition.

RuleFor(customer => customer.CustomerDiscount)
    .GreaterThan(0)
    .SetValidator(New MyCustomerDiscountValidator);

RuleFor(customer =>If you need to specify the same condition for multiple rules then you customer.CustomerDiscount).GreaterThan(0)can call the top-level When method instead of chaining the When call at the end of the rule:

When(customer => customer.IsPreferred, () => {
   RuleFor(customer => customer.CustomerDiscount).GreaterThan(0);
   RuleFor(customer => customer.CreditCardNumber).NotNull();
});

This time, the condition will be applied to both rules.SetValidator(New MyCustomerDiscountValidator) You can also chain a call to Otherwise which will invoke rules that don’t match the condition:

When(customer => customer.IsPreferred, () => {
   RuleFor(customer => customer.CustomerDiscount).GreaterThan(0);
   RuleFor(customer => customer.CreditCardNumber).NotNull();
}).Otherwise(() => {
  RuleFor(customer => customer.CustomerDiscount).Equal(0);
});

Fluent validation supports conditional validation, just use the When clause to check the value of the secondary field:

https://fluentvalidation.net/start#conditions

Specifying a condition with When/Unless The When and Unless methods can be used to specify conditions that control when the rule should execute. For example, this rule on the CustomerDiscount property will only execute when IsPreferredCustomer is true:

RuleFor(customer => customer.CustomerDiscount).GreaterThan(0).When(customer => customer.IsPreferredCustomer);`

The Unless method is simply the opposite of When.

You may also be able to use the .SetValidator operation to define a custom validator that operates on the NotEmpty condition.

RuleFor(customer => customer.CustomerDiscount).GreaterThan(0).SetValidator(New MyCustomerDiscountValidator)

Fluent validation supports conditional validation, just use the When clause to check the value of the secondary field:

https://fluentvalidation.net/start#conditions

Specifying a condition with When/Unless The When and Unless methods can be used to specify conditions that control when the rule should execute. For example, this rule on the CustomerDiscount property will only execute when IsPreferredCustomer is true:

RuleFor(customer => customer.CustomerDiscount)
    .GreaterThan(0)
    .When(customer => customer.IsPreferredCustomer);

The Unless method is simply the opposite of When.

You may also be able to use the .SetValidator operation to define a custom validator that operates on the NotEmpty condition.

RuleFor(customer => customer.CustomerDiscount)
    .GreaterThan(0)
    .SetValidator(New MyCustomerDiscountValidator);

If you need to specify the same condition for multiple rules then you can call the top-level When method instead of chaining the When call at the end of the rule:

When(customer => customer.IsPreferred, () => {
   RuleFor(customer => customer.CustomerDiscount).GreaterThan(0);
   RuleFor(customer => customer.CreditCardNumber).NotNull();
});

This time, the condition will be applied to both rules. You can also chain a call to Otherwise which will invoke rules that don’t match the condition:

When(customer => customer.IsPreferred, () => {
   RuleFor(customer => customer.CustomerDiscount).GreaterThan(0);
   RuleFor(customer => customer.CreditCardNumber).NotNull();
}).Otherwise(() => {
  RuleFor(customer => customer.CustomerDiscount).Equal(0);
});
updated the link to a working one, documentation has been moved (since last answer edit)
Source Link

Fluent validation supports conditional validation, just use the When clause to check the value of the secondary field:

http://fluentvalidation.codeplex.com/wikipage?title=Customising&referringTitle=Documentation&ANCHOR#WhenUnlesshttps://fluentvalidation.net/start#conditions

Specifying a condition with When/Unless The When and Unless methods can be used to specify conditions that control when the rule should execute. For example, this rule on the CustomerDiscount property will only execute when IsPreferredCustomer is true:

RuleFor(customer => customer.CustomerDiscount).GreaterThan(0).When(customer => customer.IsPreferredCustomer);`

The Unless method is simply the opposite of When.

You may also be able to use the .SetValidator operation to define a custom validator that operates on the NotEmpty condition.

RuleFor(customer => customer.CustomerDiscount).GreaterThan(0).SetValidator(New MyCustomerDiscountValidator)

Fluent validation supports conditional validation, just use the When clause to check the value of the secondary field:

http://fluentvalidation.codeplex.com/wikipage?title=Customising&referringTitle=Documentation&ANCHOR#WhenUnless

Specifying a condition with When/Unless The When and Unless methods can be used to specify conditions that control when the rule should execute. For example, this rule on the CustomerDiscount property will only execute when IsPreferredCustomer is true:

RuleFor(customer => customer.CustomerDiscount).GreaterThan(0).When(customer => customer.IsPreferredCustomer);`

The Unless method is simply the opposite of When.

You may also be able to use the .SetValidator operation to define a custom validator that operates on the NotEmpty condition.

RuleFor(customer => customer.CustomerDiscount).GreaterThan(0).SetValidator(New MyCustomerDiscountValidator)

Fluent validation supports conditional validation, just use the When clause to check the value of the secondary field:

https://fluentvalidation.net/start#conditions

Specifying a condition with When/Unless The When and Unless methods can be used to specify conditions that control when the rule should execute. For example, this rule on the CustomerDiscount property will only execute when IsPreferredCustomer is true:

RuleFor(customer => customer.CustomerDiscount).GreaterThan(0).When(customer => customer.IsPreferredCustomer);`

The Unless method is simply the opposite of When.

You may also be able to use the .SetValidator operation to define a custom validator that operates on the NotEmpty condition.

RuleFor(customer => customer.CustomerDiscount).GreaterThan(0).SetValidator(New MyCustomerDiscountValidator)

added 244 characters in body
Source Link
Denis Pitcher
  • 3.3k
  • 1
  • 29
  • 20
Loading
deleted 4 characters in body
Source Link
Denis Pitcher
  • 3.3k
  • 1
  • 29
  • 20
Loading
added 22 characters in body
Source Link
Denis Pitcher
  • 3.3k
  • 1
  • 29
  • 20
Loading
Source Link
Denis Pitcher
  • 3.3k
  • 1
  • 29
  • 20
Loading