1.2.0 • Published 7 years ago

cicero-form-validator v1.2.0

Weekly downloads
3
License
MIT
Repository
github
Last release
7 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

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

1.0.0-rc.6

7 years ago

1.0.0-rc.5

7 years ago

1.0.0-rc.4

7 years ago

1.0.0-rc.3

7 years ago

1.0.0-rc.2

7 years ago

0.19.1

8 years ago

0.19.0

8 years ago

1.0.0-rc.1

8 years ago

0.18.0

8 years ago

0.17.0

8 years ago

0.16.0

8 years ago

0.15.0

8 years ago

0.14.3

8 years ago

0.14.2

8 years ago

0.14.1

8 years ago

0.14.0

8 years ago

0.13.0

8 years ago

0.12.0

8 years ago