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
Validation RulesBoolean RulesFloat64 RulesInteger RulesString RulesTime RulesRequired RulesSlice RulesFormat RulesGeneric RulesCompare Rules
Form ValidatorRules

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

RuleDescription
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 bool values.
  • IsBool exists 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 BoolEquals when the expected value is dynamic or when the rule should read more explicitly.
  • Use IsTrue and IsFalse for 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.

On this page

TL;DRDefining Boolean FieldsApplying Boolean RulesRule ExamplesIsTrueIsTrueWithCodeIsFalseIsFalseWithCodeBoolEqualsBoolEqualsWithCodeIsBoolCustom Error CodesNotes