bjv v0.2.1
BJV (the Billionth Javascript Validation Utility)
What is it?
A simple API to help you validate your variables.
Why?
Because there isn't enough javascript validation libraries available.
...jk, I made this for the fun of it.
Installation
npm install bjv
Introduction
Here's some notes on how bjv
works:
- BJV's type functions, when called, return validator functions. Validator functions are asynchronous (i.e. they return a promise), and are used to validate and parse input data.
- Each validator function has a set of option methods defined on the function itself. The option methods available vary from type to type.
- Each option method, when called, returns a new validator function. This helps when you want to reuse validation logic.
- BJV exports and uses the following error classes:
SchemaError
: Thrown whenever an option method is used incorrectly.ValidationError
: Thrown whenever a validator is called with invalid data.
Type functions and option methods
any
any()
validators don't typecheck the input. They provide the following option methods, each of which is available on all validator functions (regardless of type):
named(str)
: Sets the validator name. Useful when not using object validators, which automatically assign validator names. Names are displayed in validation error messages, but aren't used otherwise.
Default:'<any>'
,'<array>'
,'<boolean>'
,'<number>'
,'<object>'
, or'<string>'
fallback(anything)
: Sets the validator fallback toanything
. The validator fallback is returned when the data being validated isundefined
. If the validator fallback is an async function, it's called on-demand to get the actual fallback value. If the fallback isundefined
, there is no fallback, and validation isn't stopped early. Fallback values don't undergo validation or parsing.
Default:undefined
optional(bool = true)
: Sets whether or notundefined
is considered valid input data.
Default:false
nullable(bool = true)
: Sets whether or notnull
is considered valid input data.
Default:false
custom(func)
: Adds a custom validator to the custom validation chain. Iffunc
isnull
, the chain is reset to empty. Custom validators accept input data as an argument, and returntrue
if the data is valid,false
if not. They are run in the order they are defined in the schema.
Default: empty ([]
)parse(func)
: Adds a parsing function to the parsing chain. Iffunc
isnull
, the chain is reset to empty. Parsing functions accept input data as an argument, and return the new, parsed input. They are run in the order they are defined in the schema.
Default: empty ([]
)
array
array()
validators check that the input is an array. They provide the following additional option methods:
minLength(num)
: Sets the minimum length of a valid array tonum
.
Default:0
maxLength(num)
: Sets the maximum length of a valid array tonum
.
Default:Number.POSITIVE_INFINITY
type(func)
: Sets the type validator function used to validate each item in the array. Iffunc
is null, the type validator function is unset. An array validator without a type validator will only check that input data is an array.
Default:null
boolean
boolean()
validators check that the input is a boolean value. They don't provide any additional option methods.
number
number()
validators check that the input is a number value. They provide the following additional option methods:
min(num)
: Sets the minimum valid value tonum
.
Default:Number.NEGATIVE_INFINITY
max(num)
: Sets the maximum valid value tonum
.
Default:Number.POSITIVE_INFINITY
integer(bool = true)
: Sets whether or not valid values must be integers.
Default:false
object
object()
validators check that the input is an object. They provide the following additional option method:
keys(obj)
: Sets the schema for the object validator toobj
. The schema is an object of key-validator function pairs. Ifobj
isnull
, the schema is unset. An object validator without a schema will only check that input data is an object.
Default:null
string
string()
validators check that the input is a string value. They provide the following additional option methods:
minLength(num)
: Sets the minimum valid string length tonum
.
Default:0
maxLength(num)
: Sets the maximum valid string length tonum
.
Default:Number.POSITIVE_INFINITY
choices(arr)
: Sets the valid string choices to those inarr
. Input strings that aren't in the choices array are considered invalid. Ifarr
isnull
, the choices array is unset.
Default:null