NetLife Guru

Open source Go packages for fast, maintainable web systems. Built with a documentation-first approach.

Product
OverviewGolang packagesNews
Documentation
DocumentationGo LoggerGo RouterGo DB Form
Company
OverviewContactNewsGitHub
Community / Support
Supportinfo@netlife.guru
© 2026 NetLife Guru. All rights reserved.
GitHubinfo@netlife.guru
NetLife GuruNetLife GuruNetLife Guru
NetLife GuruNetLife GuruNetLife Guru
OverviewDocumentationNewsSupportContact

Golang packages

About
Conditional ValidationConditional RulesConditional When
Form ValidatorConditional Validation

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

PageDescription
Conditional RulesConvenience helpers for common conditional validation patterns
Conditional WhenLow-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 skipped
condition is true
    ↓
nested rules executed

Common 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.

ApproachBest For
Conditional rule helpersCommon cases such as required-if or prohibited-if
conditional.WhenCustom 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 When for 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.

On this page

Why Conditional Validation ExistsValidation PhilosophyAvailable PagesBasic IdeaValidation FlowCommon Use CasesWhen to Use Conditional ValidationConditional Rules vs WhenNotes