Options
All
  • Public
  • Public/Protected
  • All
Menu

Type parameters

Hierarchy

  • Validator

Callable

  • Validator(proposed: any, current?: any, source?: Record<any, any>): ValidationPromise<T>
  • Function used to ensure a value conforms to a specific rule.

    example
    export function valueMatches(field, message) {
    // return a validator that matches the given value
    // against another value in the source object; if
    // they aren't equal, throws a ValidationError
    return async function valuesMatch(proposed, current, source) {
    if (!isEqual(proposed, get(source, field)))
    throw errors.error(message, {
    field,
    current,
    proposed,
    name: 'ValidationError',
    });
    };
    }

    // usage:
    const signup = validators.object({
    email: validators.string('email is required'),
    confirm: valueMatches('email', 'emails do not match'),
    }, 'signup information is invalid');

    Parameters

    • proposed: any

      The value to validate.

    • Optional current: any

      The current value, if known.

    • Optional source: Record<any, any>

      The source of the value change, if available.

    Returns ValidationPromise<T>

    A promise that is rejected if the value is not valid.