README
Doormen
Validate, sanitize and assert: the silver bullet of data!
Beta version.
/!\ This documentation is still a Work In Progress /!\
Basic validation
- sanitize
Array
ofstring
the sanitizer's name to apply before any type checking - optional
boolean
the data can benull
orundefined
, if so the data validate immediately - default (anything) the data can be
null
orundefined
, if so it is overwritten by the default value and it validates immediately - type
string
the name of the type checker - instanceOf
- min
- max
- length
- minLength
- maxLength
- match
- in
- notIn
- when
- properties
object
of schema, it iterates through each properties and checks that they all match their own schema - elements
Array
same than properties but for arrays - only
boolean
used in conjunction with properties or elements, it checks that no properties other than those listed are present - of
object
contains one schema that will check each elements of an array or each properties
Type Checkers
Javascript primitive types:
- undefined: the data should be
undefined
- null: the data should be
null
- boolean: the data should be a
boolean
, i.e.true
orfalse
- number: the data should be a
number
. NOTE thatInfinity
andNaN
are ok, so you may consider using real instead of number in almost all cases - string: the data should be a
string
- object: the data should be an
Object
- function: the data should be a
Function
Javascript/Node.js built-in types:
- array: the data should be an
Array
- error: the data should be an instance of
Error
- date: the data should be an instance of
Date
- buffer: the data should be a Node.js
Buffer
Common meta types:
- real: a
number
that is notNaN
nor +/-Infinity
- integer: a
number
that is notNaN
nor +/-Infinity
, and that do not have decimal part
Sanitizers
- toNumber: try to convert a
string
to anumber
- trim: trim a
string
, removing whitespace at the beginning and the end
TOC
- Assertion utilities
- Equality checker
- Basic types
- Optional, default and forced data
- Built-in types
- Mixed types
- Top-level filters
- Filters
- Children and recursivity
- Mask
- Numbers meta types
- Strings meta types
- Sanitize
- Sanitize + Patch reporting
- Full report mode
- Patch validation
- Patch application
- 'keys' attribute
- Alternatives
- Complex multiple-children constraints
- Schema validation
- Purify
- Export mode
- Expect BDD assertion library
- Sub-schema path
- Extract constraint-only schema
- Forms
- MongoDB's ObjectID
- Misc
Assertion utilities
doormen.shouldThrow() should throw if the callback has not throw, and catch if it has throw.
var thrown ;
thrown = false ;
try {
doormen.shouldThrow( () => {} ) ;
}
catch ( error ) {
thrown = true ;
}
if ( ! thrown ) { throw new Error( 'It should throw!' ) ; }
thrown = false ;
try {
doormen.shouldThrow( () => { throw new Error( 'Fatal error' ) ; } ) ;
}
catch ( error ) {
thrown = true ;
}
if ( thrown ) { throw new Error( 'It should *NOT* throw' ) ; }
doormen.not() should throw if the data validate, and catch if it has throw.
var thrown ;
thrown = false ;
try {
doormen.not( { type: 'string' } , 'text' ) ;
}
catch ( error ) {
thrown = true ;
}
if ( ! thrown ) { throw new Error( 'It should throw' ) ; }
thrown = false ;
try {
doormen.not( { type: 'string' } , 1 ) ;
}
catch ( error ) {
thrown = true ;
}
if ( thrown ) { throw new Error( 'It should *NOT* throw' ) ; }
Equality checker
Equality of simple type.
doormen.equals( undefined , undefined ) ;
doormen.equals( null , null ) ;
doormen.equals( true , true ) ;
doormen.equals( false , false ) ;
doormen.not.equals( undefined , null ) ;
doormen.not.equals( true , false ) ;
doormen.not.equals( null , false ) ;
doormen.not.equals( undefined , false ) ;
doormen.equals( NaN , NaN ) ;
doormen.not.equals( NaN , null ) ;
doormen.not.equals( NaN , undefined ) ;
doormen.equals( Infinity , Infinity ) ;
doormen.equals( -Infinity , -Infinity ) ;
doormen.not.equals( Infinity , -Infinity ) ;
doormen.equals( 0 , 0 ) ;
doormen.equals( 123 , 123 ) ;
doormen.equals( 0.123 , 0.123 ) ;
doormen.equals( "" , "" ) ;
doormen.equals( "abc" , "abc" ) ;
doormen.equals( " abc" , " abc" ) ;
doormen.equals( "abc " , "abc " ) ;
doormen.equals( " abc " , " abc " ) ;
doormen.not.equals( 0 , "" ) ;
doormen.not.equals( false , "" ) ;
Equality of objects.
var o = {} ;
doormen.equals( {} , {} ) ;
doormen.equals( o , o ) ;
doormen.equals( { a: 2 , b: 5 } , { a: 2 , b: 5 } ) ;
doormen.not.equals( { a: 2 , b: 6 } , { a: 2 , b: 5 } ) ;
doormen.equals( { b: 5 , a: 2 } , { a: 2 , b: 5 } ) ;
doormen.not.equals( { a: 2 , b: 5 , c: null } , { a: 2 , b: 5 } ) ;
doormen.not.equals( { a: 2 , b: 5 } , { a: 2 , b: 5 , c: null } ) ;
doormen.not.equals( { a: 2 , b: 5 , c: {} } , { a: 2 , b: 5 } ) ;
doormen.equals( { a: 2 , b: 5 , c: {} } , { a: 2 , b: 5 , c: {} } ) ;
doormen.equals( { a: 2 , b: 5 , c: { d: 'titi' } } , { a: 2 , b: 5 , c: { d: 'titi' } } ) ;
doormen.not.equals( { a: 2 , b: 5 , c: { d: 'titi' } } , { a: 2 , b: 5 , c: { d: 'toto' } } ) ;
doormen.equals(
{ a: 2 , b: 5 , c: { d: 'titi' , e: { f: 'f' , g: 7 } } } ,
{ a: 2 , b: 5 , c: { d: 'titi' , e: { f: 'f' , g: 7 } } }
) ;
when a property is undefined in the left-side and non-existant in the right-side, they should be equals.
doormen.equals( { a: 2 , b: 5 , c: undefined } , { a: 2 , b: 5 } ) ;
doormen.equals( { a: 2 , b: 5 } , { a: 2 , b: 5 , c: undefined } ) ;
doormen.equals( { a: 2 , b: 5 , c: undefined } , { a: 2 , b: 5 , c: undefined } ) ;
doormen.equals( { a: 2 , b: 5 , c: undefined } , { a: 2 , b: 5 , d: undefined } ) ;
doormen.equals( { a: 2 , b: 5 , c: { d: 'titi' } } , { a: 2 , b: 5 , c: { d: 'titi' , e: undefined } } ) ;
Equality of functions.
var o = {} ;
var fn = function() {} ;
var fn2 = function() {} ;
doormen.equals( fn , fn ) ;
doormen.not.equals( fn , fn2 ) ;
doormen.equals( { a: fn } , { a: fn } ) ;
doormen.equals( { b: 2 , a: fn } , { a: fn , b: 2 } ) ;
doormen.equals( [ fn ] , [ fn ] ) ;
doormen.equals( [ 1 , 2 , fn ] , [ 1 , 2 , fn ] ) ;
doormen.not.equals( [ 1 , 2 , fn ] , [ 1 , 2 ] ) ;
Equality of arrays.
var o = [] ;
doormen.equals( [] , [] ) ;
doormen.equals( o , o ) ;
doormen.equals( [ 1 ] , [ 1 ] ) ;
doormen.not.equals( [ 1 , undefined ] , [ 1 ] ) ;
doormen.not.equals( [ 1 ] , [ 1 , undefined ] ) ;
doormen.not.equals( [ 1 ] , [ 2 ] ) ;
doormen.equals( [ 1 , 2 , 3 ] , [ 1 , 2 , 3 ] ) ;
doormen.equals( [ 1 , [] , 3 ] , [ 1 , [] , 3 ] ) ;
doormen.equals( [ 1 , [ 2 ] , 3 ] , [ 1 , [ 2 ] , 3 ] ) ;
doormen.equals( [ 1 , [ 2 , 'a' ] , 3 ] , [ 1 , [ 2 , 'a' ] , 3 ] ) ;
doormen.not.equals( [ 1 , [ 2 , 'a' ] , 3 ] , [ 1 , [ 2 , 'b' ] , 3 ] ) ;
doormen.equals( [ 1 , [ 2 , [ null ] , 'a' ] , 3 ] , [ 1 , [ 2 , [ null ] , 'a' ] , 3 ] ) ;
doormen.not.equals( [ 1 , [ 2 , [ undefined ] , 'a' ] , 3 ] , [ 1 , [ 2 , [ null ] , 'a' ] , 3 ] ) ;
doormen.equals( [ [ 'one' ] , [ 'two' ] ] , [ [ 'one' ] , [ 'two' ] ] ) ;
doormen.not.equals( [ [ 'one' ] , [ 'two' ] ] , [ [ 'one' ] , [ 'twoa' ] ] ) ;
Equality of nested and mixed objects and arrays.
doormen.not.equals( {} , [] ) ;
doormen.equals(
{ a: 2 , b: 5 , c: [ 'titi' , { f: 'f' , g: 7 } ] } ,
{ a: 2 , b: 5 , c: [ 'titi' , { f: 'f' , g: 7 } ] }
) ;
doormen.equals(
[ 'a' , 'b' , { c: 'titi' , d: [ 'f' , 7 ] } ] ,
[ 'a' , 'b' , { c: 'titi' , d: [ 'f' , 7 ] } ]
) ;
Circular references: stop searching when both part have reached circular references.
var a , b ;
a = { a: 1 , b: 2 } ;
a.c = a ;
b = { a: 1 , b: 2 } ;
b.c = b ;
doormen.equals( a , b ) ;
a = { a: 1 , b: 2 , c: { a: 1 , b: 2 } } ;
a.c.c = a ;
b = { a: 1 , b: 2 } ;
b.c = b ;
doormen.equals( a , b ) ;
Date.
var date1 = new Date( '2019-06-18' ) ,
date2 = new Date( '2019-08-21' ) ,
date3 = new Date( '2019-08-21' ) ;
doormen.not.equals( date1 , date2 ) ;
doormen.equals( date2 , date3 ) ;
Buffers.
var buf , buf2 , i ;
buf = Buffer.allocUnsafe( 80 ) ;
buf2 = Buffer.allocUnsafe( 80 ) ;
for ( i = 0 ; i < 80 ; i ++ ) { buf[ i ] = i ; }
buf.copy( buf2 ) ;
doormen.equals( buf , buf2 ) ;
buf2[ 4 ] = 117 ;
doormen.not.equals( buf , buf2 ) ;
Basic types
should validate undefined accordingly.
doormen( { type: 'undefined' } , undefined ) ;
doormen.not( { type: 'undefined' } , null ) ;
doormen.not( { type: 'undefined' } , false ) ;
doormen.not( { type: 'undefined' } , true ) ;
doormen.not( { type: 'undefined' } , 0 ) ;
doormen.not( { type: 'undefined' } , 1 ) ;
doormen.not( { type: 'undefined' } , '' ) ;
doormen.not( { type: 'undefined' } , 'text' ) ;
doormen.not( { type: 'undefined' } , {} ) ;
doormen.not( { type: 'undefined' } , [] ) ;
should validate null accordingly.
doormen.not( { type: 'null' } , undefined ) ;
doormen( { type: 'null' } , null ) ;
doormen.not( { type: 'null' } , false ) ;
doormen.not( { type: 'null' } , true ) ;
doormen.not( { type: 'null' } , 0 ) ;
doormen.not( { type: 'null' } , 1 ) ;
doormen.not( { type: 'null' } , '' ) ;
doormen.not( { type: 'null' } , 'text' ) ;
doormen.not( { type: 'null' } , {} ) ;
doormen.not( { type: 'null' } , [] ) ;
should validate boolean accordingly.
doormen.not( { type: 'boolean' } , undefined ) ;
doormen.not( { type: 'boolean' } , null ) ;
doormen( { type: 'boolean' } , false ) ;
doormen( { type: 'boolean' } , true ) ;
doormen.not( { type: 'boolean' } , 0 ) ;
doormen.not( { type: 'boolean' } , 1 ) ;
doormen.not( { type: 'boolean' } , '' ) ;
doormen.not( { type: 'boolean' } , 'text' ) ;
doormen.not( { type: 'boolean' } , {} ) ;
doormen.not( { type: 'boolean' } , [] ) ;
should validate number accordingly.
doormen.not( { type: 'number' } , undefined ) ;
doormen.not( { type: 'number' } , null ) ;
doormen.not( { type: 'number' } , false ) ;
doormen.not( { type: 'number' } , true ) ;
doormen( { type: 'number' } , 0 ) ;
doormen( { type: 'number' } , 1 ) ;
doormen( { type: 'number' } , Infinity ) ;
doormen( { type: 'number' } , NaN ) ;
doormen.not( { type: 'number' } , '' ) ;
doormen.not( { type: 'number' } , 'text' ) ;
doormen.not( { type: 'number' } , {} ) ;
doormen.not( { type: 'number' } , [] ) ;
should validate string accordingly.
doormen.not( { type: 'string' } , undefined ) ;
doormen.not( { type: 'string' } , null ) ;
doormen.not( { type: 'string' } , false ) ;
doormen.not( { type: 'string' } , true ) ;
doormen.not( { type: 'string' } , 0 ) ;
doormen.not( { type: 'string' } , 1 ) ;
doormen( { type: 'string' } , '' ) ;
doormen( { type: 'string' } , 'text' ) ;
doormen.not( { type: 'string' } , {} ) ;
doormen.not( { type: 'string' } , [] ) ;
should validate object accordingly.
doormen.not( { type: 'object' } , undefined ) ;
doormen.not( { type: 'object' } , null ) ;
doormen.not( { type: 'object' } , false ) ;
doormen.not( { type: 'object' } , true ) ;
doormen.not( { type: 'object' } , 0 ) ;
doormen.not( { type: 'object' } , 1 ) ;
doormen.not( { type: 'object' } , '' ) ;
doormen.not( { type: 'object' } , 'text' ) ;
doormen( { type: 'object' } , {} ) ;
doormen( { type: 'object' } , { a: 1 , b: 2 } ) ;
doormen( { type: 'object' } , [] ) ;
doormen( { type: 'object' } , [ 1 , 2 , 3 ] ) ;
doormen( { type: 'object' } , new Date() ) ;
doormen.not( { type: 'object' } , () => {} ) ;
should validate function accordingly.
doormen.not( { type: 'function' } , undefined ) ;
doormen.not( { type: 'function' } , null ) ;
doormen.not( { type: 'function' } , false ) ;
doormen.not( { type: 'function' } , true ) ;
doormen.not( { type: 'function' } , 0 ) ;
doormen.not( { type: 'function' } , 1 ) ;
doormen.not( { type: 'function' } , '' ) ;
doormen.not( { type: 'function' } , 'text' ) ;
doormen.not( { type: 'function' } , {} ) ;
doormen.not( { type: 'function' } , [] ) ;
doormen( { type: 'function' } , () => {} ) ;
Optional, default and forced data
when a data is null, undefined or unexistant, and the optional flag is set the schema, it should validate.
doormen.not( { type: 'string' } , null ) ;
doormen( { optional: true , type: 'string' } , null ) ;
doormen.not( { type: 'string' } , undefined ) ;
doormen( { optional: true , type: 'string' } , undefined ) ;
doormen( { type: 'string' } , 'text' ) ;
doormen( { optional: true , type: 'string' } , 'text' ) ;
doormen.not( { type: 'string' } , 1 ) ;
doormen.not( { optional: true , type: 'string' } , 1 ) ;
doormen.not( { properties: { a: { type: 'string' } } } , {} ) ;
doormen( { properties: { a: { optional: true , type: 'string' } } } , {} ) ;
doormen( { properties: { a: { optional: true , type: 'string' } } } , { a: null } ) ;
doormen( { properties: { a: { optional: true , type: 'string' } } } , { a: undefined } ) ;
if 'nullIsValue' and 'optional' flags are set, null values should validate instead being considerate a non-value.
doormen.not( { nullIsValue: true , type: 'string' } , null ) ;
doormen.not( { nullIsValue: true , optional: true , type: 'string' } , null ) ;
doormen.not( { nullIsValue: true , type: 'string' } , undefined ) ;
doormen( { nullIsValue: true , optional: true , type: 'string' } , undefined ) ;
doormen.not( { properties: { a: { nullIsValue: true , type: 'string' } } } , {} ) ;
doormen( { properties: { a: { nullIsValue: true , optional: true , type: 'string' } } } , {} ) ;
doormen.not( { properties: { a: { nullIsValue: true , optional: true , type: 'string' } } } , { a: null } ) ;
doormen( { properties: { a: { nullIsValue: true , optional: true , type: 'string' } } } , { a: undefined } ) ;
if 'nullIsUndefined' is set null values are turned to 'undefined' before applying anything else.
doormen.equals( doormen( { nullIsUndefined: false } , null ) , null ) ;
doormen.equals( doormen( { nullIsUndefined: true } , null ) , undefined ) ;
doormen.not( { nullIsUndefined: true , type: 'string' } , null ) ;
doormen( { nullIsUndefined: true , optional: true , type: 'string' } , null ) ;
doormen.not( { nullIsUndefined: true , type: 'string' } , undefined ) ;
doormen( { nullIsUndefined: true , optional: true , type: 'string' } , undefined ) ;
doormen.equals( doormen( { properties: { a: { nullIsUndefined: true , optional: true , type: 'string' } } } , { a: null } ) , { a: undefined } ) ;
doormen.equals( doormen( { properties: { a: { nullIsUndefined: true , optional: true , type: 'string' } } } , { a: null } ) , {} ) ;
missing optional properties should not be created (i.e. with undefined)..
var result ;
result = doormen( { properties: { a: { optional: true , type: 'string' } } } , {} ) ;
// {a:undefined} is equals to {} for doormen.equals() (this is the correct behaviour), but here we want to know for sure
// that a key is not defined, so we have to check it explicitly
doormen.equals( 'a' in result , false ) ;
result = doormen(
{
properties: {
a: { optional: true , type: 'string' } ,
b: { optional: true , type: 'string' } ,
c: {
optional: true ,
properties: {
d: { optional: true , type: 'string' }
}
}
}
} ,
{}
) ;
doormen.equals( 'a' in result , false ) ;
doormen.equals( 'b' in result , false ) ;
doormen.equals( 'c' in result , false ) ;
result = doormen(
{
properties: {
a: { optional: true , type: 'string' } ,
b: { optional: true , type: 'string' } ,
c: {
optional: true ,
properties: {
d: { optional: true , type: 'string' }
}
}
}
} ,
{ c: undefined }
) ;
doormen.equals( 'a' in result , false ) ;
doormen.equals( 'b' in result , false ) ;
doormen.equals( 'c' in result , true ) ;
doormen.equals( result.c , undefined ) ;
result = doormen(
{
properties: {
a: { optional: true , type: 'string' } ,
b: { optional: true , type: 'string' } ,
c: {
optional: true ,
properties: {
d: { optional: true , type: 'string' }
}
}
}
} ,
{ c: null }
) ;
doormen.equals( 'a' in result , false ) ;
doormen.equals( 'b' in result , false ) ;
doormen.equals( 'c' in result , true ) ;
doormen.equals( result.c , null ) ;
result = doormen(
{
properties: {
a: { optional: true , type: 'string' } ,
b: { optional: true , type: 'string' } ,
c: {
optional: true ,
properties: {
d: { optional: true , type: 'string' }
}
}
}
} ,
{ c: {} }
) ;
doormen.equals( 'a' in result , false ) ;
doormen.equals( 'b' in result , false ) ;
doormen.equals( 'c' in result , true ) ;
doormen.equals( 'd' in result.c , false ) ;
when a data is null, undefined or unexistant, and a default value is specified in the schema, that default value should overwrite the original one.
doormen.equals( doormen( { type: 'string' , "default": 'default!' } , null ) , 'default!' ) ;
doormen.equals(
doormen(
{ properties: { a: { type: 'string' , "default": 'default!' } } } ,
{ a: null } ) ,
{ a: 'default!' }
) ;
doormen.equals(
doormen(
{ properties: { a: { type: 'string' , "default": 'default!' } , b: { type: 'object' , "default": { c: 5 } } } } ,
{ a: null , b: undefined } ) ,
{ a: 'default!' , b: { c: 5 } }
) ;
doormen.equals(
doormen(
{ properties: { a: { type: 'string' , "default": 'default!' } , b: { type: 'object' , "default": { c: 5 } } } } ,
{} ) ,
{ a: 'default!' , b: { c: 5 } }
) ;
// verify that default value can be a function (regression of v0.10.9)
var fn = () => null ;
doormen.equals(
doormen(
{ properties: { a: { type: 'string' , "default": fn } } } ,
{ a: null } ) ,
{ a: fn }
) ;
when the 'defaultFn' is specified in the schema and is a string, that builtin default function is executed and its return-value is used as the default.
//doormen.equals( doormen( { type: 'date' , defaultFn: 'now' } , null ) , new Date() ) ;
doormen.equals(
doormen(
{ properties: { a: { type: 'date' , defaultFn: 'now' } } } ,
{ a: null } ) ,
{ a: new Date() }
) ;
when the 'defaultFn' is specified in the schema and is a function, that function is executed and its return-value is used as the default.
var date , count = 0 ;
doormen.equals( doormen( { type: 'date' , defaultFn: () => date = new Date() } , null ) , date ) ;
doormen.equals(
doormen(
{ properties: { a: { type: 'date' , defaultFn: () => date = new Date() } } } ,
{ a: null } ) ,
{ a: date }
) ;
doormen.equals( doormen( { type: 'integer' , defaultFn: () => ++ count } , null ) , 1 ) ;
doormen.equals(
doormen(
{ properties: {
a: { type: 'integer' , defaultFn: () => ++ count } ,
b: { type: 'integer' , defaultFn: () => ++ count }
} } ,
{ a: null } ) ,
{ a: 2 , b: 3 }
) ;
if 'nullIsValue' is set and a 'default' value is set, null values are not replaced by the default value.
doormen.not( { type: 'string' , nullIsValue: true , "default": 'default!' } , null ) ;
doormen.equals( doormen( { nullIsValue: true , "default": 'default!' } , null ) , null ) ;
doormen.equals( doormen( { nullIsValue: true , "default": 'default!' } , undefined ) , 'default!' ) ;
doormen.not(
{ properties: { a: { type: 'string' , nullIsValue: true , "default": 'default!' } } } ,
{ a: null } ) ,
{ a: null }
doormen.equals(
doormen(
{ properties: { a: { nullIsValue: true , "default": 'default!' } } } ,
{ a: null } ) ,
{ a: null }
) ;
doormen.equals(
doormen(
{ properties: { a: { nullIsValue: true , "default": 'default!' } } } ,
{ a: undefined } ) ,
{ a: 'default!' }
) ;
doormen.equals(
doormen(
{ properties: { a: { nullIsValue: true , "default": 'default!' } , b: { type: 'object' , nullIsValue: true , "default": { c: 5 } } } } ,
{ a: null , b: undefined } ) ,
{ a: null , b: { c: 5 } }
) ;
when the schema has a forced value, it should validate the data and set it to that value.
var schema ;
schema = { value: 'forced!' } ;
doormen.equals( doormen( schema , null ) , 'forced!' ) ;
doormen.equals( doormen( schema , undefined ) , 'forced!' ) ;
doormen.equals( doormen( schema , 'bob' ) , 'forced!' ) ;
doormen.equals( doormen( schema , {} ) , 'forced!' ) ;
schema = { properties: { a: { value: 'forced!' } } } ;
doormen.equals( doormen( schema , {} ) , { a: 'forced!' } ) ;
doormen.equals( doormen( schema , { a: undefined } ) , { a: 'forced!' } ) ;
doormen.equals( doormen( schema , { a: null } ) , { a: 'forced!' } ) ;
doormen.equals( doormen( schema , { a: 'bob' } ) , { a: 'forced!' } ) ;
doormen.equals( doormen( schema , { a: { jack: 'bob' } } ) , { a: 'forced!' } ) ;
schema = { properties: { a: { value: 'forced!' } , b: { value: { c: 5 } } } } ;
doormen.equals( doormen( schema , { a: null , b: undefined } ) , { a: 'forced!' , b: { c: 5 } } ) ;
doormen.equals( doormen( schema , {} ) , { a: 'forced!' , b: { c: 5 } } ) ;
Built-in types
should validate 'unset' accordingly (undefined or null).
doormen( { type: 'unset' } , undefined ) ;
doormen( { type: 'unset' } , null ) ;
doormen.not( { type: 'unset' } , false ) ;
doormen.not( { type: 'unset' } , true ) ;
doormen.not( { type: 'unset' } , 0 ) ;
doormen.not( { type: 'unset' } , 1 ) ;
doormen.not( { type: 'unset' } , '' ) ;
doormen.not( { type: 'unset' } , 'text' ) ;
doormen.not( { type: 'unset' } , {} ) ;
doormen.not( { type: 'unset' } , [] ) ;
should validate array accordingly.
doormen.not( { type: 'array' } , undefined ) ;
doormen.not( { type: 'array' } , null ) ;
doormen.not( { type: 'array' } , false ) ;
doormen.not( { type: 'array' } , true ) ;
doormen.not( { type: 'array' } , 0 ) ;
doormen.not( { type: 'array' } , 1 ) ;
doormen.not( { type: 'array' } , '' ) ;
doormen.not( { type: 'array' } , 'text' ) ;
doormen.not( { type: 'array' } , {} ) ;
doormen.not( { type: 'array' } , { a: 1 , b: 2 } ) ;
doormen( { type: 'array' } , [] ) ;
doormen( { type: 'array' } , [ 1 , 2 , 3 ] ) ;
doormen.not( { type: 'array' } , () => {} ) ;
should validate date accordingly.
doormen( { type: 'date' } , new Date() ) ;
doormen.not( { type: 'date' } , undefined ) ;
doormen.not( { type: 'date' } , null ) ;
doormen.not( { type: 'date' } , false ) ;
doormen.not( { type: 'date' } , true ) ;
doormen.not( { type: 'date' } , 0 ) ;
doormen.not( { type: 'date' } , 1 ) ;
doormen.not( { type: 'date' } , '' ) ;
doormen.not( { type: 'date' } , 'text' ) ;
doormen.not( { type: 'date' } , {} ) ;
doormen.not( { type: 'date' } , { a: 1 , b: 2 } ) ;
doormen.not( { type: 'date' } , [] ) ;
doormen.not( { type: 'date' } , [ 1 , 2 , 3 ] ) ;
doormen.not( { type: 'date' } , () => {} ) ;
should validate error accordingly.
doormen( { type: 'error' } , new Error() ) ;
doormen.not( { type: 'error' } , undefined ) ;
doormen.not( { type: 'error' } , null ) ;
doormen.not( { type: 'error' } , false ) ;
doormen.not( { type: 'error' } , true ) ;
doormen.not( { type: 'error' } , 0 ) ;
doormen.not( { type: 'error' } , 1 ) ;
doormen.not( { type: 'error' } , '' ) ;
doormen.not( { type: 'error' } , 'text' ) ;
doormen.not( { type: 'error' } , {} ) ;
doormen.not( { type: 'error' } , { a: 1 , b: 2 } ) ;
doormen.not( { type: 'error' } , [] ) ;
doormen.not( { type: 'error' } , [ 1 , 2 , 3 ] ) ;
doormen.not( { type: 'error' } , () => {} ) ;
should validate arguments accordingly.
var fn = function() { doormen( { type: 'arguments' } , arguments ) ; } ; // eslint-disable-line prefer-rest-params
fn() ;
fn( 1 ) ;
fn( 1 , 2 , 3 ) ;
doormen.not( { type: 'arguments' } , undefined ) ;
doormen.not( { type: 'arguments' } , null ) ;
doormen.not( { type: 'arguments' } , false ) ;
doormen.not( { type: 'arguments' } , true ) ;
doormen.not( { type: 'arguments' } , 0 ) ;
doormen.not( { type: 'arguments' } , 1 ) ;
doormen.not( { type: 'arguments' } , '' ) ;
doormen.not( { type: 'arguments' } , 'text' ) ;
doormen.not( { type: 'arguments' } , {} ) ;
doormen.not( { type: 'arguments' } , { a: 1 , b: 2 } ) ;
doormen.not( { type: 'arguments' } , [] ) ;
doormen.not( { type: 'arguments' } , [ 1 , 2 , 3 ] ) ;
doormen.not( { type: 'arguments' } , () => {} ) ;
Mixed types
should validate 'strictObject' accordingly, i.e. objects that are NOT arrays.
doormen.not( { type: 'strictObject' } , undefined ) ;
doormen.not( { type: 'strictObject' } , null ) ;
doormen.not( { type: 'strictObject' } , false ) ;
doormen.not( { type: 'strictObject' } , true ) ;
doormen.not( { type: 'strictObject' } , 0 ) ;
doormen.not( { type: 'strictObject' } , 1 ) ;
doormen.not( { type: 'strictObject' } , '' ) ;
doormen.not( { type: 'strictObject' } , 'text' ) ;
doormen( { type: 'strictObject' } , {} ) ;
doormen( { type: 'strictObject' } , { a: 1 , b: 2 } ) ;
doormen.not( { type: 'strictObject' } , [] ) ;
doormen.not( { type: 'strictObject' } , [ 1 , 2 , 3 ] ) ;
doormen.not( { type: 'strictObject' } , () => {} ) ;
should validate 'regexp' accordingly, i.e. RegExp instance or string convertible to RegExp.
doormen( { type: 'regexp' } , /Random/ ) ;
doormen( { type: 'regexp' } , new RegExp( "Random" ) ) ;
doormen( { type: 'regexp' } , "Random" ) ;
doormen.not( { type: 'regexp' } , "(Random" ) ;
doormen.not( { type: 'regexp' } , undefined ) ;
doormen.not( { type: 'regexp' } , null ) ;
doormen.not( { type: 'regexp' } , false ) ;
doormen.not( { type: 'regexp' } , true ) ;
doormen.not( { type: 'regexp' } , 0 ) ;
doormen.not( { type: 'regexp' } , 1 ) ;
doormen( { type: 'regexp' } , '' ) ;
doormen( { type: 'regexp' } , 'text' ) ;
doormen.not( { type: 'regexp' } , {} ) ;
doormen.not( { type: 'regexp' } , { a: 1 , b: 2 } ) ;
doormen.not( { type: 'regexp' } , [] ) ;
doormen.not( { type: 'regexp' } , [ 1 , 2 , 3 ] ) ;
doormen.not( { type: 'regexp' } , () => {} ) ;
should validate 'classId' accordingly, i.e. function (constructor) or non-empty string.
doormen.not( { type: 'classId' } , undefined ) ;
doormen.not( { type: 'classId' } , null ) ;
doormen.not( { type: 'classId' } , false ) ;
doormen.not( { type: 'classId' } , true ) ;
doormen.not( { type: 'classId' } , 0 ) ;
doormen.not( { type: 'classId' } , 1 ) ;
doormen.not( { type: 'classId' } , '' ) ;
doormen( { type: 'classId' } , 'text' ) ;
doormen.not( { type: 'classId' } , {} ) ;
doormen.not( { type: 'classId' } , { a: 1 , b: 2 } ) ;
doormen.not( { type: 'classId' } , [] ) ;
doormen.not( { type: 'classId' } , [ 1 , 2 , 3 ] ) ;
doormen( { type: 'classId' } , () => {} ) ;
Top-level filters
'instanceOf' should validate object accordingly.
function MyClass() {}
if ( doormen.isBrowser ) { window[ 'MyClass' ] = MyClass ; }
else { global[ 'MyClass' ] = MyClass ; }
doormen( { instanceOf: Date } , new Date() ) ;
doormen( { instanceOf: Array } , new Array() ) ;
doormen( { instanceOf: MyClass } , new MyClass() ) ;
doormen( { instanceOf: Object } , new MyClass() ) ;
doormen( { instanceOf: 'MyClass' } , new MyClass() ) ;
doormen( { instanceOf: 'Object' } , new MyClass() ) ;
doormen.not( { instanceOf: Date } , new Array() ) ;
doormen.not( { instanceOf: 'Date' } , new Array() ) ;
min filter should validate accordingly, non-number should throw.
doormen( { min: 3 } , 10 ) ;
doormen( { min: 3 } , 3 ) ;
doormen.not( { min: 3 } , 1 ) ;
doormen.not( { min: 3 } , 0 ) ;
doormen.not( { min: 3 } , -10 ) ;
doormen( { min: 3 } , Infinity ) ;
doormen( { min: Infinity } , Infinity ) ;
doormen.not( { min: 3 } , -Infinity ) ;
doormen.not( { min: 3 } , NaN ) ;
doormen.not( { min: 3 } , true ) ;
doormen.not( { min: 3 } , false ) ;
doormen.not( { min: 3 } , undefined ) ;
doormen.not( { min: 0 } , undefined ) ;
doormen.not( { min: -3 } , undefined ) ;
doormen.not( { min: 3 } , '10' ) ;
max filter should validate accordingly, non-number should throw.
doormen.not( { max: 3 } , 10 ) ;
doormen( { max: 3 } , 3 ) ;
doormen( { max: 3 } , 1 ) ;
doormen( { max: 3 } , 0 ) ;
doormen( { max: 3 } , -10 ) ;
doormen.not( { max: 3 } , Infinity ) ;
doormen( { max: 3 } , -Infinity ) ;
doormen( { max: -Infinity } , -Infinity ) ;
doormen.not( { max: 3 } , NaN ) ;
doormen.not( { max: 3 } , true ) ;
doormen.not( { max: 3 } , false ) ;
doormen.not( { max: 3 } , '1' ) ;
min + max filter should validate accordingly, non-number should throw.
doormen.not( { min: 3 , max: 10 } , 15 ) ;
doormen( { min: 3 , max: 10 } , 10 ) ;
doormen( { min: 3 , max: 10 } , 5 ) ;
doormen( { min: 3 , max: 10 } , 3 ) ;
doormen.not( { min: 3 , max: 10 } , 1 ) ;
doormen.not( { min: 3 , max: 10 } , 0 ) ;
doormen.not( { min: 3 , max: 10 } , -10 ) ;
doormen.not( { min: 3 , max: 10 } , Infinity ) ;
doormen.not( { min: 3 , max: 10 } , -Infinity ) ;
doormen.not( { min: 3 , max: 10 } , NaN ) ;
doormen.not( { min: 3 , max: 10 } , true ) ;
doormen.not( { min: 3 , max: 10 } , false ) ;
doormen.not( { min: 3 , max: 10 } , '6' ) ;
'length' filter should validate accordingly, data that do not have a length should throw.
doormen( { length: 3 } , "abc" ) ;
doormen.not( { length: 3 } , "abcde" ) ;
doormen.not( { length: 3 } , "ab" ) ;
doormen.not( { length: 3 } , "" ) ;
doormen.not( { length: 3 } , 1 ) ;
doormen.not( { length: 0 } , 1 ) ;
doormen.not( { length: 3 } , NaN ) ;
doormen.not( { length: 3 } , true ) ;
doormen.not( { length: 3 } , false ) ;
minLength filter should validate accordingly, data that do not have a length should throw.
doormen( { minLength: 3 } , "abc" ) ;
doormen( { minLength: 3 } , "abcde" ) ;
doormen.not( { minLength: 3 } , "ab" ) ;
doormen.not( { minLength: 3 } , "" ) ;
doormen( { minLength: 3 } , [ 1,2,3 ] ) ;
doormen( { minLength: 3 } , [ 1,2,3,4 ] ) ;
doormen.not( { minLength: 3 } , [ 1,2 ] ) ;
doormen.not( { minLength: 3 } , 1 ) ;
doormen.not( { minLength: 0 } , 1 ) ;
doormen.not( { minLength: 3 } , NaN ) ;
doormen.not( { minLength: 3 } , true ) ;
doormen.not( { minLength: 3 } , false ) ;
maxLength filter should validate accordingly, data that do not have a length should throw.
doormen( { maxLength: 3 } , "abc" ) ;
doormen.not( { maxLength: 3 } , "abcde" ) ;
doormen( { maxLength: 3 } , "ab" ) ;
doormen( { maxLength: 3 } , "" ) ;
doormen( { maxLength: 3 } , [ 1,2,3 ] ) ;
doormen.not( { maxLength: 3 } , [ 1,2,3,4 ] ) ;
doormen( { maxLength: 3 } , [ 1,2 ] ) ;
doormen.not( { maxLength: 3 } , 1 ) ;
doormen.not( { maxLength: 0 } , 1 ) ;
doormen.not( { maxLength: 3 } , NaN ) ;
doormen.not( { maxLength: 3 } , true ) ;
doormen.not( { maxLength: 3 } , false ) ;
minLength + maxLength filter should validate accordingly, data that do not have a length should throw.
doormen( { minLength: 3 , maxLength: 5 } , "abc" ) ;
doormen( { minLength: 3 , maxLength: 5 } , "abcd" ) ;
doormen( { minLength: 3 , maxLength: 5 } , "abcde" ) ;
doormen.not( { minLength: 3 , maxLength: 5 } , "abcdef" ) ;
doormen.not( { minLength: 3 , maxLength: 5 } , "ab" ) ;
doormen.not( { minLength: 3 , maxLength: 5 } , "" ) ;
doormen.not( { minLength: 3 , maxLength: 5 } , 1 ) ;
doormen.not( { maxLength: 0 } , 1 ) ;
doormen.not( { minLength: 3 , maxLength: 5 } , NaN ) ;
doormen.not( { minLength: 3 , maxLength: 5 } , true ) ;
doormen.not( { minLength: 3 , maxLength: 5 } , false ) ;
'match' filter should validate accordingly using a RegExp.
doormen( { match: "^[a-f]*