UNPKG

@roqueform/doubter-plugin

Version:
50 lines (49 loc) 1.53 kB
/** * Validates Roqueform fields with [Doubter](https://github.com/smikhalevski/doubter#readme) shapes. * * ```sh * npm install --save-prod @roqueform/doubter-plugin * ``` * * Create a field validated by a Doubter shape: * * ```ts * import * as d from 'doubter'; * import { createField } from 'roqueform'; * import errorsPlugin from 'roqueform/plugin/errors'; * import doubterPlugin, { concatDoubterIssues } from '@roqueform/doubter'; * * const fieldShape = d.object({ * hello: d.string() * }); * * const field = createField({ hello: 'world' }, [ * errorsPlugin(concatDoubterIssues), * doubterPlugin(fieldShape) * ]); * * field.at('hello').validate() // ⮕ true * ``` * * @module @roqueform/doubter-plugin */ import { Issue, ParseOptions, Shape } from 'doubter'; import { FieldPlugin } from 'roqueform'; import { ValidationMixin } from 'roqueform/plugin/validation'; import { ErrorsConcatenator } from 'roqueform/plugin/errors'; /** * The mixin added to fields by the {@link doubterPlugin}. */ export interface DoubterMixin extends ValidationMixin<ParseOptions | void> { } /** * Validates field with a [Doubter](https://github.com/smikhalevski/doubter#readme) shape. * * @param shape The shape that parses the field value. * @template Value The root field value. */ export default function doubterPlugin<Value>(shape: Shape<Value, any>): FieldPlugin<Value, DoubterMixin>; /** * Concatenates unique Doubter issues. */ export declare const concatDoubterIssues: ErrorsConcatenator<Issue>;