Skip to content

gunshi / combinators / combinator

Function: combinator()

ts
function combinator<T>(config): CombinatorSchema<T>;

Experimental

Create a custom argument schema with a user-defined parse function.

This is the most general custom combinator. Use it when none of the built-in base combinators (string, number, integer, float, boolean, choice) fit your needs.

The returned schema has type: 'custom'.

Type Parameters

Type ParameterDescription
TThe parsed value type.

Parameters

ParameterTypeDescription
configCombinatorOptions<T>Configuration with a parse function and optional metavar.

Returns

CombinatorSchema<T>

A combinator schema that resolves to the parse function's return type.

Example

ts
const date = combinator({
  parse: (value) => {
    const d = new Date(value)
    if (isNaN(d.getTime())) {
      throw new Error('Invalid date format')
    }
    return d
  },
  metavar: 'date'
})

Released under the MIT License.