Nodeschool Public Materials
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

validity-equal

0.1.3 • Public • Published

validity-equal

Build Status

Validity style validator to ensure a property is equal to some primitive value. This might seem trivial, but it's useful when you are leveraging schemata for its validation and error message for something like 'agreed to terms and conditions'.

Installation

npm install validity-equal

Usage

Below is a simple example for usage with schemata:

 
var validity = require('validity')
  , schemata = require('schemata')
  , createValidator = require('validity-equal')
 
var schema = schemata(
    { agreedToTerms:
      { type: Boolean
      , validators: { all: [ createValidator(true, 'Must agree to T&Cs') ] }
      }
    })
 
schema.validate({ agreedToTerms: true }, function (error, errorMessage) {
  console.log(errorMessage) //-> undefined
})
 
schema.validate({ agreedToTerms: false }, function (error, errorMessage) {
  console.log(errorMessage) //-> 'Must agree to T&Cs'
})

API

var validate = createValidator(Mixed: value, String: failureMessage)

Equality check is done with === so value must be primitive.

validate(String:key, String:keyDisplayName, Object:object, Function:cb)

This is a validity compatible function, which in turn is used by schemata for schema validation.

The callback signature cb(err, errorMessage).

  • err is an Error object if something bad happened and null otherwise.
  • errorMessage is a String if a validation error happened and undefined otherwise.

Licence

Licensed under the New BSD License

Keywords

none

install

npm i validity-equal

Downloadsweekly downloads

21

version

0.1.3

license

none

last publish

collaborators

  • avatar