Boolean Rules
Learn how to validate boolean fields in NLG Form using true, false, equality, and custom error code validation rules.
Boolean rules validate typed bool fields defined with form.Bool.
They are useful for fields such as terms acceptance, feature flags, admin switches, visibility settings, consent checkboxes, and other true/false application state.
TL;DR
| Rule | Description |
|---|---|
rules.IsTrue(field) | Requires the boolean value to be true |
rules.IsTrueWithCode(field, code) | Requires the value to be true and returns a custom error code |
rules.IsFalse(field) | Requires the boolean value to be false |
rules.IsFalseWithCode(field, code) | Requires the value to be false and returns a custom error code |
rules.BoolEquals(field, expected) | Requires the boolean value to match expected |
rules.BoolEqualsWithCode(field, expected, code) | Requires the value to match expected and returns a custom error code |
rules.IsBool(field) | No-op for Go typed inputs; JSON type errors are handled before validation |
Defining Boolean Fields
Boolean validation starts by defining typed boolean fields.
BoolForm := struct {
TermsAccepted form.BoolField[BoolRulesRequest]
Admin form.BoolField[BoolRulesRequest]
}{
TermsAccepted: form.Bool[BoolRulesRequest]("terms_accepted", func(r *BoolRulesRequest) bool {
return r.TermsAccepted
}),
Admin: form.Bool[BoolRulesRequest]("admin", func(r *BoolRulesRequest) bool {
return r.Admin
}),
}Each field contains the response field name and a typed accessor function.
Applying Boolean Rules
Boolean rules are then attached to the field references.
return form.Schema[BoolRulesRequest]{
rules.IsTrue(BoolForm.TermsAccepted),
rules.IsFalse(BoolForm.Admin),
}This keeps validation explicit and type-safe.
Rule Examples
IsTrue
Requires the boolean field to be true.
rules.IsTrue(BoolForm.TermsAccepted)Typical use cases:
- terms acceptance
- GDPR consent
- required confirmation flags
IsTrueWithCode
Requires the boolean field to be true and returns a custom validation code.
const CodeMustBeAccepted = form.Code("must_be_accepted")
rules.IsTrueWithCode(
BoolForm.TermsAccepted,
CodeMustBeAccepted,
)Useful when APIs require stable frontend-facing validation codes.
IsFalse
Requires the boolean field to be false.
rules.IsFalse(BoolForm.Admin)Typical use cases:
- disabled feature flags
- restricted admin fields
- forbidden public settings
IsFalseWithCode
Requires the boolean field to be false and returns a custom validation code.
const CodeMustBeDisabled = form.Code("must_be_disabled")
rules.IsFalseWithCode(
BoolForm.PublicProfile,
CodeMustBeDisabled,
)BoolEquals
Requires the field value to match the expected boolean value.
rules.BoolEquals(BoolForm.Enabled, true)This is useful when the expected value is dynamic or configurable.
BoolEqualsWithCode
Requires the field value to match the expected value and returns a custom validation code.
rules.BoolEqualsWithCode(
BoolForm.Enabled,
true,
CodeMustBeEnabled,
)IsBool
Checks whether the field contains a boolean value.
rules.IsBool(BoolForm.Enabled)For typed Go structures this rule is typically unnecessary because JSON decoding already validates the input type before schema validation begins.
Custom Error Codes
Use WithCode variants when your API needs stable error codes.
const CodeMustBeAccepted = form.Code("must_be_accepted")
rules.IsTrueWithCode(BoolForm.TermsAccepted, CodeMustBeAccepted)Custom codes are useful for frontend translations, API contracts, and consistent validation responses.
Notes
- Boolean rules work on Go
boolvalues. IsBoolexists for API symmetry, but it is a no-op for typed Go inputs.- Invalid JSON types are handled during request decoding before schema validation runs.
- Use
BoolEqualswhen the expected value is dynamic or when the rule should read more explicitly. - Use
IsTrueandIsFalsefor common intent-based validation such as required consent or disabled flags.
Validation Rules
Learn how validation rules work in NLG Form using reusable, composable, type-safe rule pipelines for strings, numbers, booleans, slices, formats, and cross-field validation.
Float64 Rules
Learn how to validate float64 fields in NLG Form using minimum, maximum, range, equality, and custom validation rules for decimal values.