PsychoType
Simple and lightweight type validation library for node.
const type = const referece = 'foo': type 'bar': type type// => true
Installation
This is a node.js module available via npm. Requires node.js 4.9.1 or highter.
$ npm i psycho-type
Usage
This module can be used for runtime type validation for one item or (nested) object.
/* Validating single item */const valid_type = type /* You can do it with check function */type//=> true /* Or only with valid type function *///=> true
Object validation is a little similar to TypeScript Interfaces.
/* Validating objects */const reference = // Reference object, contain types. 'foo': type // 'bar': type // 'fizz': // 'buzz': type // const object = // Object to validate. 'foo': 'String' // 'bar': true // 'fizz': // 'buzz': 15 // type//=> true
Types / Methods
Basic Types
Any
Just returns true on everything.
Param {Any} item
Return {Boolean}
const any = type //=> true
String
Allow both ' "string" ' and ' new String() '.
Param {Any} item
Return {Boolean}
const string = type //=> true //=> false //=> true
Numbers
Any Number
Allow number, bigInt or NaN.
Param {Any} item
Return {Boolean}
const anyNumber = type //=> true //=> true //=> false
Number
Allow only regular number.
Param {Any} item
Return {Boolean}
const number = type //=> true //=> false //=> false
BigInt
Allow only BigInt. Have two aliases:
- bigInt
- bigNumber
Param {Any} item
Return {Boolean}
const bigint = type //=> false //=> true //=> false
Boolean
Allow only boolean. Have two aliases:
- bool
- boolean
Param {Any} item
Return {Boolean}
const bool = type //=> false //=> true //=> true
Function
Allow only functions.
! Any class/constructor will return true !
Param {Any} item
Return {Boolean}
const func = type //=> false //=> true // Because ' Boolean ' it's the constructor//=> true
Object
Allow only objects.
! Doesn't allow null or basic data types even if it's created by ' new Object() ' !
Param {Any} item
Return {Boolean}
const object = typeobject //=> true //=> false //=> true
Array
Allow array with any value.
Param {Any} item
Return {Boolean}
const array = type //=> true //=> true //=> false
Symbol
Allow symbol.
Param {Any} item
Return {Boolean}
const symbol = type //=> false //=> true //=> false
Undefined
Allow only undefined.
Param {Any} item
Return {Boolean}
const not_defined = typeundefined //=> false //=> false //=> true
Null
Allow only null.
Param {Any} item
Return {Boolean}
const null_type = type //=> false //=> true //=> false
NaN
Allow only NaN. Have two aliases:
- nan
- NaN
Param {Any} item
Return {Boolean}
const null_type = type //=> false //=> true //=> false
Complex Types
No Value
Allow ' no value ' types such as undefined, null, NaN.
Param {Any} item
Return {Boolean}
const no_value = type //=> false //=> true //=> true
Empty
Allow ' empty ' values such as {}, [], '' and 0.
! Doesn't work NOW with values created from constructor ( like ' new String() ' ) !
Param {Any} item
Return {Boolean}
const empty = type //=> true //=> false //=> false
Array Of <types>
Allow array with some types.
Param {Array} ...types
Return {function}:
Param {Array} item
Return {Boolean}
const array = type //=> true //=> false //=> true
Enum <values>
Allow only some values.
! Doesn't work NOW for values, created by like ' new Number() ' !
Param {Array} ...values
Return {function}:
Param {Any} item
Return {Boolean}
const enum = type enum 'value' //=> true enum 3 //=> true enum 4 //=> false
Some Of <types>
Allow some types.
Param {Array} ...types
Return {function}:
Param {Any} item
Return {Boolean}
const some = type //=> true //=> false //=> true
Not <types>
Inverted ' someOf '. Disallow some types.
Param {Array} ...types
Return {function}:
Param {Any} item
Return {Boolean}
const not = type //=> false //=> true //=> false
'Check' Method
Compare types of input object with reference.
! Method will return false if reference is not valid, without any exeption !
Param {Object|Function} reference/type
Return {Boolean}
const reference = // Reference object, contain types. 'foo': type // 'bar': type // 'fizz': // 'buzz': type // const object = // Valid object. 'foo': 'String' // 'bar': true // 'fizz': // 'buzz': 15 // type//=> true
'Of' Method
Returns type of input even if input created like ' new Object(3) ' and has custom types: 'null', 'NaN'.
Param {Any} item
Return {String}
type;//=> 'function' type;//=> 'null' type;//=> 'NaN' type;//=> 'string'
Contacts
If you have any questions or suggestions please contact me: dmitry.kokhanevich@gmail.com