validity-number-or-null

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

Usage no npm install needed!

<script type="module">
  import validityNumberOrNull from 'https://cdn.skypack.dev/validity-number-or-null';
</script>

README

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