Skip to content

Conditional Presence Rules ​

Conditional presence rules apply required, present, missing, or prohibited behavior only when other request fields match a condition. They are useful when one field changes the meaning of another field, such as payment method details, scheduled publishing, or mutually exclusive options.

RequiredIf ​

Requires the field when another field equals one of the provided values.

csharp
[RequiredIf(string field, params object?[] values)]

[RequiredIf("Status", "scheduled")]
csharp
RuleFor(x => x.PublishAt)
    .RequiredIf(x => x.Status, "scheduled");

RequiredUnless ​

Requires the field unless another field equals one of the provided values.

csharp
[RequiredUnless(string field, params object?[] values)]

[RequiredUnless("Status", "draft")]
csharp
RuleFor(x => x.ApprovalReason)
    .RequiredUnless(x => x.Status, "draft");

RequiredIfAccepted ​

Requires the field when another field contains an accepted value such as true, yes, on, or 1.

csharp
[RequiredIfAccepted(string field)]

[RequiredIfAccepted("TermsAccepted")]
csharp
RuleFor(x => x.Signature)
    .RequiredIfAccepted(x => x.TermsAccepted);

RequiredIfDeclined ​

Requires the field when another field contains a declined value such as false, no, off, or 0.

csharp
[RequiredIfDeclined(string field)]

[RequiredIfDeclined("MarketingAccepted")]
csharp
RuleFor(x => x.DeclineReason)
    .RequiredIfDeclined(x => x.MarketingAccepted);

RequiredWith ​

Requires the field when any listed field is present.

csharp
[RequiredWith(params string[] fields)]

[RequiredWith("Email", "PhoneNumber")]
csharp
RuleFor(x => x.ContactName)
    .RequiredWith(x => x.Email, x => x.PhoneNumber);

RequiredWithAll ​

Requires the field when all listed fields are present.

csharp
[RequiredWithAll(params string[] fields)]

[RequiredWithAll("StartsAt", "EndsAt")]
csharp
RuleFor(x => x.Timezone)
    .RequiredWithAll(x => x.StartsAt, x => x.EndsAt);

RequiredWithout ​

Requires the field when any listed field is missing.

csharp
[RequiredWithout(params string[] fields)]

[RequiredWithout("Email", "PhoneNumber")]
csharp
RuleFor(x => x.Email)
    .RequiredWithout(x => x.PhoneNumber);

RequiredWithoutAll ​

Requires the field when all listed fields are missing.

csharp
[RequiredWithoutAll(params string[] fields)]

[RequiredWithoutAll("Email", "PhoneNumber")]
csharp
RuleFor(x => x.ContactName)
    .RequiredWithoutAll(x => x.Email, x => x.PhoneNumber);

PresentIf ​

Requires the field to be present when another field equals one of the provided values. The field may still be empty unless another rule rejects empty values.

csharp
[PresentIf(string field, params object?[] values)]

[PresentIf("Mode", "manual")]
csharp
RuleFor(x => x.ManualNote)
    .PresentIf(x => x.Mode, "manual");

PresentUnless ​

Requires the field to be present unless another field equals one of the provided values.

csharp
[PresentUnless(string field, params object?[] values)]

[PresentUnless("Mode", "automatic")]
csharp
RuleFor(x => x.ExternalReference)
    .PresentUnless(x => x.Mode, "automatic");

PresentWith ​

Requires the field to be present when any listed field is present.

csharp
[PresentWith(params string[] fields)]

[PresentWith("Street", "City")]
csharp
RuleFor(x => x.AddressLine2)
    .PresentWith(x => x.Street, x => x.City);

PresentWithAll ​

Requires the field to be present when all listed fields are present.

csharp
[PresentWithAll(params string[] fields)]

[PresentWithAll("Street", "PostalCode")]
csharp
RuleFor(x => x.AddressNote)
    .PresentWithAll(x => x.Street, x => x.PostalCode);

MissingIf ​

Requires the field to be missing when another field equals one of the provided values.

csharp
[MissingIf(string field, params object?[] values)]

[MissingIf("AccountType", "guest")]
csharp
RuleFor(x => x.BusinessNumber)
    .MissingIf(x => x.AccountType, "guest");

MissingUnless ​

Requires the field to be missing unless another field equals one of the provided values.

csharp
[MissingUnless(string field, params object?[] values)]

[MissingUnless("AccountType", "business")]
csharp
RuleFor(x => x.BusinessNumber)
    .MissingUnless(x => x.AccountType, "business");

MissingWith ​

Requires the field to be missing when any listed field is present.

csharp
[MissingWith(params string[] fields)]

[MissingWith("ArchiveReason")]
csharp
RuleFor(x => x.DeleteReason)
    .MissingWith(x => x.ArchiveReason);

MissingWithAll ​

Requires the field to be missing when all listed fields are present.

csharp
[MissingWithAll(params string[] fields)]

[MissingWithAll("StartsAt", "EndsAt")]
csharp
RuleFor(x => x.PublishImmediately)
    .MissingWithAll(x => x.StartsAt, x => x.EndsAt);

ProhibitedIf ​

Prohibits the field when another field equals one of the provided values.

csharp
[ProhibitedIf(string field, params object?[] values)]

[ProhibitedIf("AccountType", "guest")]
csharp
RuleFor(x => x.BusinessNumber)
    .ProhibitedIf(x => x.AccountType, "guest");

ProhibitedUnless ​

Prohibits the field unless another field equals one of the provided values.

csharp
[ProhibitedUnless(string field, params object?[] values)]

[ProhibitedUnless("AccountType", "business")]
csharp
RuleFor(x => x.BusinessNumber)
    .ProhibitedUnless(x => x.AccountType, "business");

ProhibitedIfAccepted ​

Prohibits the field when another field contains an accepted value.

csharp
[ProhibitedIfAccepted(string field)]

[ProhibitedIfAccepted("UseDefaultBilling")]
csharp
RuleFor(x => x.BillingAddress)
    .ProhibitedIfAccepted(x => x.UseDefaultBilling);

ProhibitedIfDeclined ​

Prohibits the field when another field contains a declined value.

csharp
[ProhibitedIfDeclined(string field)]

[ProhibitedIfDeclined("CustomBillingEnabled")]
csharp
RuleFor(x => x.CustomBillingAddress)
    .ProhibitedIfDeclined(x => x.CustomBillingEnabled);

Prohibits ​

Makes this field prohibit the listed fields from being present with a value. Use it for mutually exclusive request options.

csharp
[Prohibits(params string[] fields)]

[Prohibits("ScheduledAt", "PublishImmediately")]
csharp
RuleFor(x => x.PublishImmediately)
    .Prohibits(x => x.ScheduledAt);

All packages are released under the MIT License.