Conditional Validation
Learn how conditional validation works in NLG Form using conditional rules, runtime predicates, When helpers, and reusable validation pipelines.
Conditional validation allows rules to run only when a runtime condition is true.
This is useful when validation depends on another field, request state, account type, feature flag, workflow step, or business rule.
Why Conditional Validation Exists
Many real-world forms and API payloads are not static.
Some fields are required only when:
- another field is filled
- a user selects a specific option
- an account type changes
- a feature is enabled
- a workflow enters a specific state
- an optional section becomes active
Conditional validation makes these cases explicit and reusable.
Validation Philosophy
Conditional validation separates:
- the condition that decides whether validation should run
- the validation rules that should run when the condition is true
This keeps schemas readable and avoids hiding business logic inside struct tags.
Available Pages
| Page | Description |
|---|---|
| Conditional Rules | Convenience helpers for common conditional validation patterns |
| Conditional When | Low-level conditional validation using custom runtime predicates |
Basic Idea
A conditional rule checks a predicate first.
conditional.When(
func(in *Request) bool {
return in.AccountType == "business"
},
rules.Required(Form.CompanyName),
)If the condition returns true, the nested rules are executed.
If the condition returns false, validation is skipped.
Validation Flow
condition is false
↓
validation skippedcondition is true
↓
nested rules executedCommon Use Cases
Conditional validation is commonly used for:
- business account forms
- company billing information
- password change flows
- dependent address fields
- profile completion steps
- onboarding workflows
- feature-specific settings
- role-based validation
- multi-step forms
- PATCH APIs
When to Use Conditional Validation
Use conditional validation when a field should be validated only in a specific context.
Examples:
conditional.RequiredIfStr(
Form.CompanyName,
func(in *Request) bool {
return in.AccountType == "business"
},
)conditional.When(
func(in *Request) bool {
return in.ChangePassword
},
rules.Required(Form.NewPassword),
rules.MinLen(Form.NewPassword, 8),
)Conditional Rules vs When
Use convenience conditional rules when the validation pattern is common.
Use When when the condition or nested validation logic is custom.
| Approach | Best For |
|---|---|
| Conditional rule helpers | Common cases such as required-if or prohibited-if |
conditional.When | Custom predicates and complex rule composition |
Notes
- Conditional validation is evaluated at runtime.
- Conditions receive the full request structure.
- Nested rules run only when the condition returns
true. - Conditional validation keeps business validation explicit.
- Conditional validation works with regular rules, optional rules, and custom rules.
- Prefer
Whenfor complex logic and helper rules for simple common cases.
Optional Slice Validation
Learn how optional slice validation works in NLG Form using OptionalSlice, optional arrays, collection validation, item limits, and reusable validation pipelines.
Conditional Rules
Learn how to use NLG Form conditional rule helpers for required-if, prohibited-if, required-with, and required-without validation flows.