Skip to content

assert

that

Signature

js
assert.that (value, validator, errorMessage)

Description

Assert that a value passes a validator function. Throws a TypeError with the given message if the validator returns false.

Parameters

NameTypeRequiredDescription
value*yesThe value to validate
validatorfunctionyesA function that returns true if the value is valid
errorMessagestringyesThe error message thrown if validation fails

Returns

TypeDescription
void

Examples

js
assert.that(5, (v) => v > 0, 'Value must be positive')  // passes
assert.that(-1, (v) => v > 0, 'Value must be positive') // throws TypeError: 'Value must be positive'
assert.that('hello', is.string, 'Must be a string')     // passes

all

Signature

js
assert.all (validations)

Description

Assert multiple validations at once. Iterates through the array and throws a TypeError at the first failing validation.

Parameters

NameTypeRequiredDescription
validationsArrayyesArray of validation objects
validations[].value*yesThe value to validate
validations[].validatorfunctionyesThe validator function
validations[].messagestringyesThe error message if validation fails

Returns

TypeDescription
void

Examples

js
// passes
assert.all([
  { value: 'Alice', validator: (v) => is.nonEmptyString(v), message: 'name must be a non-empty string' },
  { value: 25, validator: is.positive, message: 'age must be positive' }
])
js
// throws TypeError: 'age must be positive'
assert.all([
  { value: 'Alice', validator: (v) => is.nonEmptyString(v), message: 'name must be a non-empty string' },
  { value: -1, validator: is.positive, message: 'age must be positive' }
])

any

Signature

js
assert.any (validations)

Description

Assert that at least one validation passes. Iterates through the array and throws a TypeError if none of the validators return true. The error message is built by joining all validation messages with or.

Parameters

NameTypeRequiredDescription
validationsArrayyesArray of validation objects
validations[].value*yesThe value to validate
validations[].validatorfunctionyesThe validator function
validations[].messagestringyesThe error message if this validation fails

Returns

TypeDescription
void

Examples

js
// passes — value is a string
assert.any([
  { value: 'hello', validator: is.string, message: 'must be a string' },
  { value: 'hello', validator: is.array, message: 'must be an array' }
])
js
// throws TypeError: 'must be a string or must be an array'
assert.any([
  { value: 42, validator: is.string, message: 'must be a string' },
  { value: 42, validator: is.array, message: 'must be an array' }
])