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.
[RequiredIf(string field, params object?[] values)]
[RequiredIf("Status", "scheduled")]RuleFor(x => x.PublishAt)
.RequiredIf(x => x.Status, "scheduled");RequiredUnless
Requires the field unless another field equals one of the provided values.
[RequiredUnless(string field, params object?[] values)]
[RequiredUnless("Status", "draft")]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.
[RequiredIfAccepted(string field)]
[RequiredIfAccepted("TermsAccepted")]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.
[RequiredIfDeclined(string field)]
[RequiredIfDeclined("MarketingAccepted")]RuleFor(x => x.DeclineReason)
.RequiredIfDeclined(x => x.MarketingAccepted);RequiredWith
Requires the field when any listed field is present.
[RequiredWith(params string[] fields)]
[RequiredWith("Email", "PhoneNumber")]RuleFor(x => x.ContactName)
.RequiredWith(x => x.Email, x => x.PhoneNumber);RequiredWithAll
Requires the field when all listed fields are present.
[RequiredWithAll(params string[] fields)]
[RequiredWithAll("StartsAt", "EndsAt")]RuleFor(x => x.Timezone)
.RequiredWithAll(x => x.StartsAt, x => x.EndsAt);RequiredWithout
Requires the field when any listed field is missing.
[RequiredWithout(params string[] fields)]
[RequiredWithout("Email", "PhoneNumber")]RuleFor(x => x.Email)
.RequiredWithout(x => x.PhoneNumber);RequiredWithoutAll
Requires the field when all listed fields are missing.
[RequiredWithoutAll(params string[] fields)]
[RequiredWithoutAll("Email", "PhoneNumber")]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.
[PresentIf(string field, params object?[] values)]
[PresentIf("Mode", "manual")]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.
[PresentUnless(string field, params object?[] values)]
[PresentUnless("Mode", "automatic")]RuleFor(x => x.ExternalReference)
.PresentUnless(x => x.Mode, "automatic");PresentWith
Requires the field to be present when any listed field is present.
[PresentWith(params string[] fields)]
[PresentWith("Street", "City")]RuleFor(x => x.AddressLine2)
.PresentWith(x => x.Street, x => x.City);PresentWithAll
Requires the field to be present when all listed fields are present.
[PresentWithAll(params string[] fields)]
[PresentWithAll("Street", "PostalCode")]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.
[MissingIf(string field, params object?[] values)]
[MissingIf("AccountType", "guest")]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.
[MissingUnless(string field, params object?[] values)]
[MissingUnless("AccountType", "business")]RuleFor(x => x.BusinessNumber)
.MissingUnless(x => x.AccountType, "business");MissingWith
Requires the field to be missing when any listed field is present.
[MissingWith(params string[] fields)]
[MissingWith("ArchiveReason")]RuleFor(x => x.DeleteReason)
.MissingWith(x => x.ArchiveReason);MissingWithAll
Requires the field to be missing when all listed fields are present.
[MissingWithAll(params string[] fields)]
[MissingWithAll("StartsAt", "EndsAt")]RuleFor(x => x.PublishImmediately)
.MissingWithAll(x => x.StartsAt, x => x.EndsAt);ProhibitedIf
Prohibits the field when another field equals one of the provided values.
[ProhibitedIf(string field, params object?[] values)]
[ProhibitedIf("AccountType", "guest")]RuleFor(x => x.BusinessNumber)
.ProhibitedIf(x => x.AccountType, "guest");ProhibitedUnless
Prohibits the field unless another field equals one of the provided values.
[ProhibitedUnless(string field, params object?[] values)]
[ProhibitedUnless("AccountType", "business")]RuleFor(x => x.BusinessNumber)
.ProhibitedUnless(x => x.AccountType, "business");ProhibitedIfAccepted
Prohibits the field when another field contains an accepted value.
[ProhibitedIfAccepted(string field)]
[ProhibitedIfAccepted("UseDefaultBilling")]RuleFor(x => x.BillingAddress)
.ProhibitedIfAccepted(x => x.UseDefaultBilling);ProhibitedIfDeclined
Prohibits the field when another field contains a declined value.
[ProhibitedIfDeclined(string field)]
[ProhibitedIfDeclined("CustomBillingEnabled")]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.
[Prohibits(params string[] fields)]
[Prohibits("ScheduledAt", "PublishImmediately")]RuleFor(x => x.PublishImmediately)
.Prohibits(x => x.ScheduledAt);