Skip to content

optional

Signature

js
optional (validator)

Description

Create a predicate that accepts undefined or any value accepted by the provided validator.

It is typically used with conform to define optional properties in a schema.

Parameters

NameTypeRequiredDescription
validatorFunctionyesThe predicate used to validate the property when it is defined

Returns

TypeDescription
FunctionA predicate that accepts undefined or values satisfying the provided validator

Examples

js
import { conform, optional, is } from '@kalisio/common-core'

const schema = {
  name: is.string,
  description: optional(is.string)
}

conform({ name: 'foo' }, schema)                     // true
conform({ name: 'foo', description: 'bar' }, schema) // true
conform({ name: 'foo', description: 42 }, schema)    // false