0.0.2 • Published 10 years ago

validity-number-or-null v0.0.2

Weekly downloads
2
License
-
Repository
github
Last release
10 years ago

validity-number-or-null

Build Status

Validity style validator to ensure a property is either null or numeric (and not NaN).

Installation

npm install validity-number-or-null

Usage

Below is a simple example for usage with schemata:

var validity = require('validity')
  , schemata = require('schemata')
  , createValidator = require('validity-number-or-null')

var schema = schemata(
    { waitTime:
      { type: Number
      , validators: { all: [ createValidator() ] }
      }
    })

schema.validate({ waitTime: 30 }, function (error, errorMessage) {
  console.log(errorMessage) //-> undefined
})

schema.validate({ waitTime: null }, function (error, errorMessage) {
  console.log(errorMessage) //-> undefined
})

schema.validate({ waitTime: 'abc' }, function (error, errorMessage) {
  console.log(errorMessage) //-> 'Wait time must be a number if present'
})

API

var validate = createValidator()

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