nucleartide / ember-changeset-hofs

(unmaintained, don't use) Higher-order validation functions for ember-changeset

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool



ember-changeset-hofs


ember-changeset-hofs provides higher-order validation functions for ember-changeset-validations.

This addon provides and and or higher-order functions, that allow you to compose together validations. It is useful if you want the short-circuit behavior of && and ||. For example:

// app/validations/user.js
import and from 'ember-changeset-hofs/utils/and'
import or from 'ember-changeset-hofs/utils/or'

export const {
  email: and(
    validateFormat({ type: 'email' }),
    askServerIfExists(), // will not get called if validateFormat fails
  ),

  anotherEmail: or(
    isUndefinedOrNull(),
    askServerIfExists(), // will not get called if isUndefinedOrNull succeeds
  ),
}

Note that the and and or utils work with both synchronous and asynchronous validators. You can nest and and or expressions arbitrarily, and you can mix sync and async validators however you want:

const validationFn = and(
  and(
    and(...someValidators),
    or(...someMoreValidators),
    or(
      or(...someValidators),
      and(...someMoreValidators),
    )
  ),
  or(...evenMoreValidators)
)

Install

$ ember install ember-changeset-hofs

License

MIT

About

(unmaintained, don't use) Higher-order validation functions for ember-changeset

License:MIT License


Languages

Language:JavaScript 93.2%Language:HTML 6.8%