Typology
Typology is a lightweight data validation library for Node.js and the browser (with or without Browserify).
It can validate variables against native JavaScript types as well as against custom types you can define.
Installation
Install with npm:
// Latest release
npm install typology
// Development version
npm install git+https://github.com/jacomyal/typology.git
Include the typology.js
(or the minified version typology.min.js
) file client-side if you do not want to use Browserify or a similar library.
Usage
Get the native type of a given variable
var types = require('typology');
types.get(true);
>>> 'boolean'
types.get(/abc/);
>>> 'regexp'
Deal with custom types
A custom type can be either be defined by a function returning a boolean or an expressive string or object describing the type you want to check.
Example
var customType = function(variable) {
return typeof variable === 'number' && variable === (variable | 0);
};
var customType = '?string';
var customType = 'string|number';
var customType = {
firstname: 'string',
lastname: 'string',
age: 'number'
};
Custom types syntax
Expression | Description | Examples | Validates |
---|
'type' | required | 'string' | 'hello' |
'?type' | optional | '?string' | 'hello' , undefined , null |
`'type1 | type2'` | multi-types | `'string |
{prop: 'type'} | complex | {firstname: 'string'} | {firstname: 'Joachim'} |
['type'] | lists | ['number'] | [1, 2, 3] |
'!type' | exclusive | '!string' | 42 |
Note also that expression can be combined. For instance '?string|number'
means an optional string or number variable and '!string|object'
means anything but a string or an object.
Overkill example
var myCustomType = {
firstname: 'string',
pseudo: '?string',
account: {
total: '?number|string',
bills: ['number']
}
}
Validate a variable against a custom type
var types = require('typology');
types.check(myType, myVariable);
types.check('number', 1);
>>> true
types.check(
{
firstname: 'string',
lastname: 'string',
age: 'number'
},
{
firstname: 'Joachim',
lastname: 'Murat'
}
);
>>> false
Getting more information about what does not match
var types = require('typology');
types.scan(myType, myVariable);
types.scan('number', 1);
>>> { expected: 'number',
>>> type: 'number',
>>> value: 1 }
types.scan(
{
firstname: 'string',
lastname: 'string',
age: 'number'
},
{
firstname: 'Joachim',
lastname: 'Murat'
}
);
>>> { error: 'Expected a "number" but found a "undefined".',
>>> expected: 'number',
>>> type: 'undefined',
>>> value: undefined,
>>> path: [ 'age' ] }
Create your own typology to add custom types
var Typology = require('typology');
var myTypology = new Typology();
myTypology.add(myCustomType);
myTypology.add('User', {
firstname: 'string',
lastname: 'string',
age: '?number'
});
myTypology.check('User', {hello: 'world'});
>>> false
myTypology.check('User|number', myVar);
Checking whether a custom type's definition is valid
var types = require('typology');
types.isValid(customType);
types.isValid('?string');
>>> true
types.isValid('randomcrap');
>>> false
Contribution
Contributions are welcome. Please be sure to add and pass unit tests if relevant before submitting any code.
To setup the project, just install npm dependencies with npm install
and run tests with npm test
.
License
Typology is under a MIT license.