Skip to main content

Boolean Validator

The Boolean Validator validates boolean values (true/false) with support for accepted/declined patterns and strict boolean checks. Perfect for checkboxes, toggles, feature flags, and permission systems.


Basic Usage

import { v, validate } from "@warlock.js/seal";

const schema = v.boolean()
.required()
.accepted();

const result = await validate(schema, true);
// Result: { isValid: true, data: true }

Available Methods

Boolean Validators (14 methods)


Accepted Values

The accepted() method accepts flexible boolean-like values:

  • true, "true", "yes", "y", "on", 1, "1"
v.boolean().accepted()
// Input: true, "yes", "1", "on" → Valid ✅
// Input: false, "no", "0" → Invalid ❌

Conditional Accepted

// Accepted if another field equals a value
v.boolean().acceptedIf("subscribe", true)

// Accepted unless another field equals a value
v.boolean().acceptedUnless("premium", false)

// Accepted if another field is required
v.boolean().acceptedIfRequired("newsletter")

// Accepted if another field is present
v.boolean().acceptedIfPresent("newsletter")

// Accepted if another field is missing
v.boolean().acceptedWithout("newsletter")

Declined Values

The declined() method accepts flexible boolean-like values:

  • false, "false", "no", "n", "off", 0, "0"
v.boolean().declined()
// Input: false, "no", "0", "off" → Valid ✅
// Input: true, "yes", "1" → Invalid ❌

Conditional Declined

// Declined if another field equals a value
v.boolean().declinedIf("subscribe", false)

// Declined unless another field equals a value
v.boolean().declinedUnless("premium", true)

// Declined if another field is required
v.boolean().declinedIfRequired("newsletter")

// Declined if another field is present
v.boolean().declinedIfPresent("newsletter")

// Declined if another field is missing
v.boolean().declinedWithout("newsletter")

Strict Boolean Checks

mustBeTrue()

Value must be strictly true (not "yes", "on", 1, etc.)

v.boolean().mustBeTrue()
// Input: true → Valid ✅
// Input: "yes", "on", 1 → Invalid ❌

mustBeFalse()

Value must be strictly false (not "no", "off", 0, etc.)

v.boolean().mustBeFalse()
// Input: false → Valid ✅
// Input: "no", "off", 0 → Invalid ❌

Common Patterns

Terms Acceptance (Checkbox)

const termsSchema = v.boolean()
.required()
.accepted();

const result = await validate(termsSchema, "yes");
// Result: { isValid: true, data: "yes" }

Feature Flag

const featureSchema = v.boolean()
.required()
.mustBeTrue();

const result = await validate(featureSchema, true);
// Result: { isValid: true, data: true }

Conditional Newsletter

const newsletterSchema = v.boolean()
.acceptedIf("subscribe", true);

const result = await validate(newsletterSchema, "yes", {
subscribe: true
});
// Result: { isValid: true, data: "yes" }

Strict Toggle

const toggleSchema = v.boolean()
.required()
.mustBeFalse();

const result = await validate(toggleSchema, false);
// Result: { isValid: true, data: false }

Inherited Methods

The Boolean Validator extends BaseValidator and inherits all base validation methods:

  • Basic Rules: required(), optional(), present(), nullable()
  • Comparison: equal(), notEqual(), sameAs(), differentFrom()
  • Type Checking: typeOf(), instanceOf()
  • Advanced: when(), custom(), transform()

View Base Validator →


Accepted vs MustBeTrue

MethodInput Types AcceptedUse Case
accepted()true, "true", "yes", "on", 1, "1"Flexible form inputs
mustBeTrue()true onlyStrict boolean values

Declined vs MustBeFalse

MethodInput Types AcceptedUse Case
declined()false, "false", "no", "off", 0, "0"Flexible form inputs
mustBeFalse()false onlyStrict boolean values

See Also