1.2.0 • Published 9 years ago

cicero-form-validator v1.2.0

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

Validator

A form validator that takes a JSON based configuration.

Install

npm i --save cicero-form-validator

Usage

import validator, {multipleValidator} from 'cicero-form-validator'

const validate = validator({
  field: { required: true }
})

validate(['field'], {})
// => { field: { field: 'field', rule: 'required', value: undefined } }


const validateMultiple = multipleValidator({
  field: { required: true }
})


const validateMultiple = multipleValidator({ field: { required: true } })

validateMultiple(['field'], { 1: {} })
// => { 1: { field: { field: 'field', rule: 'required', value: undefined } } }

Configuration

Options that work for all rules

if

Should be a function which returns a boolean. It will be called with an object containing all the fields as a first argument.

The example below will make field required if otherField is filled with "100".

{
  field: {
    required: { if: (fields) => fields.otherField === "100" }
  }
}

Adding new rules

import {extend} from 'cicero-form-validator'

const {validator, multipleValidator} = extend({
  mod: (field, value, options) => value % options.constant === 0 ? null : 'mod'
});

validator({ f: { mod: { constant: 2 } })(['f'], { f: 3 })
// => { f: { field: 'f', rule: 'mod', value: 3 } }

multipleValidator({ f: { mod: { constant: 2 } } })(['f'], { 1: { f: 3 } })
// => { '1': { f: { field: 'f', rule: 'mod', value: 3, config: [Object] } } }

Creating rules

A rule is a function that takes three arguments: field, value and options. It then validates the value based on a given conditions and returns null if the value validates. If not it should return the name of the rule that did not validate. A rule can have sub-rules (e.g. numeric validation with max value). It should then return 'rule/sub-rule' if the data does not validate.

1.2.0

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago

1.0.0-rc.6

9 years ago

1.0.0-rc.5

9 years ago

1.0.0-rc.4

9 years ago

1.0.0-rc.3

9 years ago

1.0.0-rc.2

9 years ago

0.19.1

9 years ago

0.19.0

9 years ago

1.0.0-rc.1

9 years ago

0.18.0

9 years ago

0.17.0

9 years ago

0.16.0

10 years ago

0.15.0

10 years ago

0.14.3

10 years ago

0.14.2

10 years ago

0.14.1

10 years ago

0.14.0

10 years ago

0.13.0

10 years ago

0.12.0

10 years ago