Options
All
  • Public
  • Public/Protected
  • All
Menu

validated-proxy

Index

Type aliases

BufferErrorHandler

BufferErrorHandler: function

The error handler is a function that is called when an invalid value is set. This is in addition to the error *that is already stored in the cache. By default, the error handler is a no-op (does nothing). You can specify a custom error handler; for example, you could log error messages, send them to a server, etc:

const proxy = validatedProxy(original, {
  errorHandler: errorMessages => { // do something here },
  validations: // ... });

Type declaration

    • (messages: string[]): void
    • Parameters

      • messages: string[]

      Returns void

BufferExecutionHandler

BufferExecutionHandler: function

The execution handler is a function that is used to set the changes on the target object. By default, this is Object.assign. You can specify a custom execution handler; for example, you could use Lodash's assign, Ember's set, and so forth:

const proxy = validatedProxy(original, {
  executionHandler: (target, changes) => { // do something here },
  validations: // ...
});

Type declaration

    • (target: T, changes: Partial<T>): Partial<T>
    • Parameters

      • target: T
      • changes: Partial<T>

      Returns Partial<T>

ValidationMap

ValidationMap: object

A validation map is an object containing the mapping between the target schema and validator functions.

Type declaration

ValidatorFunction

ValidatorFunction: function

Function signature for validator functions. Pass in a type parameter here to allow typechecking newValue.

Type declaration

    • Parameters

      • key: ValidKey
      • newValue: T
      • oldValue: unknown

      Returns ValidationMeta

Functions

hasOwnProperty

  • hasOwnProperty<T>(obj: T, ...args: [string | number | symbol]): boolean
  • Shorthand for Object.prototype.hasOwnProperty. Returns a boolean indicating whether the object has the specified property as own (not inherited) property. Useful when the object was created with Object.create(null).

    hasOwnProperty({ foo: 1 }, 'foo'); // true

    Type parameters

    • T

    Parameters

    • obj: T
    • Rest ...args: [string | number | symbol]

    Returns boolean

validatedProxy

  • validatedProxy<T>(target: T, __namedParameters: object): BufferedProxy<T, keyof T>
  • Wraps a target object with a BufferedProxy. Setters will first invoke a validator function, if found in a supplied validation map. The validator function returns a ValidatedResult, and the BufferedProxy is set. Getters will forward to the target object, first returning the error, then the change, and finally the original value;

    If the result is valid, the value is set into the BufferedProxy's internal value cache.

    If the result is invalid, the value is set into the BufferedProxy's internal error cache.

    const user = {
      name: 'Billy Bob',
      age: 25
    };
    const bufferedProxy = validatedProxy(user, {
      validations: {
        name: [
          validatePresence(true),
          validateLength({ min: 4 })
        ],
        age: [
          validatePresence(true),
          validateNumber({ gte: 18 })
        ]
      }
    });
    bufferedProxy.name = 'Michael Bolton';
    user.name; // 'Billy Bob'
    bufferedProxy.flush();
    user.name; // 'Michael Bolton'

    Type parameters

    Parameters

    • target: T
    • __namedParameters: object
      • errorHandler: undefined | function
      • executionHandler: undefined | function
      • validations: object

    Returns BufferedProxy<T, keyof T>

validatorLookup

  • validatorLookup<V, T, K>(validations: T, key: ValidKey): Array<ValidatorFunction<T[K]>>
  • Looks up validator function(s) from a validator map. If none are found, fall back to the defaultValidator.

    const original = { foo: null };
    const validationMap = {
      foo: validatePresence(),
      bar: [
        validatePresence(),
        validateLength({ gt: 2 })
      ]
    };
    validatorLookup(validationMap, 'foo'); // ValidatorFunction[]

    Type parameters

    Parameters

    • validations: T
    • key: ValidKey

    Returns Array<ValidatorFunction<T[K]>>

Generated using TypeDoc