Skip to content

optional

Allows undefined while keeping a strict parser for the present value. DDataParser.optional(inner) returns the output of inner when a value is provided and lets undefined pass otherwise.

Interactive example

Parameters

  • inner: parser applied when a value is provided.
  • coalescingValue: (optional) replaces undefined with a default value and narrows the output type to Output<inner>.
  • checkers: checkerRefine or custom helpers evaluating Output<inner> | undefined.
  • errorMessage: message used when the input is neither undefined nor valid for inner.

Return value

A DataParserOptional. schema.parse(data) returns DEither.success<Output | undefined> or DEither.error<DataParserError> with the incriminated path. Checkers/restrictions apply only when the value is not undefined (unless you implement different logic in your refinements).

Other examples

Extended mode

See also

  • nullable - Allows accepting null in a parser
  • array - Parser for arrays

Released under the MIT license.