Namespace: validators

validators

Allows for validating changeset changes. Each validator has the same signature (opts:Object, changeset:Object) -> changeset:Object. Validators are curried. You can chain validators using pipe or compose from functional style libraries like lodash/fp, ramda. If you pass custom options to the validator, it will be available when traversing errors.
Source:

Example

_.compose(
  required({fields: ['title', 'body']}),
  length({fields: ['body'], min: 10, max 300}),
  acceptance({fields: ['rules']})
)(changeset)

Methods

(static) acceptance(opts, changeset) → {Object}

Validates that the given field is true.
Parameters:
Name Type Description
opts Object options
Properties
Name Type Description
message String [must be accepted] - error message
fields Array fields to validate
changeset Object target changeset
Source:
Returns:
validated changeset
Type
Object

(static) change(opts, changeset) → {Object}

Use custom validator on values from the given fields. Validator should return Array. If array is not empty, its values will be add to the field errors.
Parameters:
Name Type Description
opts Object options
Properties
Name Type Description
fields Array fields to validate
validator function custom validator
changeset Object target changeset
Source:
Returns:
validated changeset
Type
Object

(static) confirmation(opts, changeset) → {Object}

Validates that the given field matches the confirmation field. When calling confirmation({fields: ['email']}, changeset), validator will check if both "email" and "emailConfirmation" are equal.
Parameters:
Name Type Description
opts Object options
Properties
Name Type Description
message String [does not match] - error message
fields Array fields to validate
changeset Object target changeset
Source:
Returns:
validated changeset
Type
Object

(static) equal(opts, number, changeset) → {Object}

Validates that the given field is equal the given number. Works with numbers.
Parameters:
Name Type Description
opts Object options
Properties
Name Type Description
message String [has invalid format] - error message
fields Array fields to validate
number Number number to match
changeset Object target changeset
Source:
Returns:
validated changeset
Type
Object

(static) exclusion(opts, reserved, changeset) → {Object}

Validates that the given field does not match any value from the given reserved values.
Parameters:
Name Type Description
opts Object options
Properties
Name Type Description
message String [is reserved] - error message
fields Array fields to validate
reserved Array reserved values
changeset Object target changeset
Source:
Returns:
validated changeset
Type
Object

(static) format(opts, match, changeset) → {Object}

Validates that the given field match the given format. Works with strings.
Parameters:
Name Type Description
opts Object options
Properties
Name Type Description
message String [has invalid format] - error message
fields Array fields to validate
match RegEx regex to match
changeset Object target changeset
Source:
Returns:
validated changeset
Type
Object

(static) greaterThan(opts, number, changeset) → {Object}

Validates that the given field is greater than the given number. Works with numbers.
Parameters:
Name Type Description
opts Object options
Properties
Name Type Description
message String [has invalid format] - error message
fields Array fields to validate
number Number number to match
changeset Object target changeset
Source:
Returns:
validated changeset
Type
Object

(static) greaterThanOrEqualTo(opts, number, changeset) → {Object}

Validates that the given field is greater than or equal to the given number. Works with numbers.
Parameters:
Name Type Description
opts Object options
Properties
Name Type Description
message String [has invalid format] - error message
fields Array fields to validate
number Number number to match
changeset Object target changeset
Source:
Returns:
validated changeset
Type
Object

(static) inclusion(opts, include, changeset) → {Object}

Validates that the given field match any value from the given include values.
Parameters:
Name Type Description
opts Object options
Properties
Name Type Description
message String [is reserved] - error message
fields Array fields to validate
include Array included values
changeset Object target changeset
Source:
Returns:
validated changeset
Type
Object

(static) length(opts, changeset) → {Object}

Validates that the given field has correct length. Works with arrays and strings. When min and max length are equal, validation message will check if length is exactly this value. The messsage of validation failure depends on the validation: for strings: `should be ${x} character(s)`, `should be at least ${x} character(s)`, `should be at most ${x} character(s)` for arrays: `should have ${x} items(s)`, `should have at least ${x} items(s)`, `should have at most ${x} items(s)`
Parameters:
Name Type Description
opts Object options
Properties
Name Type Description
message String error message
fields Array fields to validate
min Number the length must be greater than or equal to this value
max Number the length must be less than or equal to this value
changeset Object target changeset
Source:
Returns:
validated changeset
Type
Object

(static) lessThan(opts, number, changeset) → {Object}

Validates that the given field is less than the given number. Works with numbers.
Parameters:
Name Type Description
opts Object options
Properties
Name Type Description
message String [has invalid format] - error message
fields Array fields to validate
number Number number to match
changeset Object target changeset
Source:
Returns:
validated changeset
Type
Object

(static) lessThanOrEqualTo(opts, number, changeset) → {Object}

Validates that the given field is less than or equal to the given number. Works with numbers.
Parameters:
Name Type Description
opts Object options
Properties
Name Type Description
message String [has invalid format] - error message
fields Array fields to validate
number Number number to match
changeset Object target changeset
Source:
Returns:
validated changeset
Type
Object

(static) required(opts, changeset) → {Object}

Validates the given field is present in the changeset.
Parameters:
Name Type Description
opts Object options
Properties
Name Type Description
message String [can't be blank] - error message
fields Array fields to validate
changeset Object target changeset
Source:
Returns:
validated changeset
Type
Object