xType
Simple object type checking
xType is just a simple way to quickly validate that an object is of a certain structure. It's goal is to be lightweight and fast.
Notes:
- You currently can't inherit required properties using a
check
function.
Usage
xType exposes the following functions:
- define
- get
- defineFn
- getFn
- guard
- undefine
xtype
name
: name of the definition. Can only be used once.type
: the raw type of the object, such as 'object' or 'array'options
: an object or function that is used for validation
xtype
name
: name of the function to remove
xtype
name
: name of the function.types
: each argument of the function is given one argument
xtype
name
: name of the function
xtype
name
: name of the
xtype
name
: name of the function definitionfn
: function to run if validation passes
Definitions
You use xType by creating 'definitions' that document what an object should consist of.
xtype; var test = ; ;
Options
Keys
Keys allow you to specify what keys an object can have to be valid. If the type does not match, or if an object has a key that is not in 'keys', then it will return false.
var test = ; ; // true; // false test = ; ; // true; // false
Required Keys
By default, all keys are optional. However, if you need to specify that an object is valid if it contains a key, you can set it as required.
Do this by prefixing the type with an asterisk.
var test = ; ; // true; // false
Other
Other makes the test more flexible - meaning that it will ignore any properties on an object that
test = ; ; // false test = ; ; // true
All
Setting options.all means that all properties of the object must be that type.
This can only be used by itself, and cannot be used with 'keys', 'other' or 'inheritance'.
test = ; ; // true; // false
Primitive Types
xType is very flexible about what types are. So you can easily use it with other type validation libraries.
If you supply a type and a function, they will be used to create a function that firsts check that an object is of that type, and if so will then check it against your function.
var email = ; ; // true; // false; // false; // false var validator = ;var uppercase = ;;;;
You can just have a completly raw function that will do all the checking. This is particulary useful for combining it with other libraries that already do type checking for you.
var array = ;
Inheritance
xType allows you to split definitions in multiple sections, so you don't have to repeat yourself all the time.
xtype; xtype; xtype; xtype; var task = xtype;var list = xtype; ; // true ; // true
Fancy Inheritance
I haven't thought of a good name for this yet.
Basically you can choose which definition to inherit, based on the object.
To use it you set inherit
as an possible definitions to inherit, as
well as setting check
to a function that accepts a single argument: object.
inherit
can be an object or an array, but whatever the check
function
returns will be used to try and access the property from it.
xtype; xtype; xtype; xtype; var thing = xtype; ; // true ; // true ; // true ; // false - type d doesn't exist
Function Definitions
This is used to create definitions for function arguments.
defineFn
The first argument is the name of the function. The rest are the types of each argument. Every argument is required by default.
xtype;
Optional Properties
Prefix type with a ~
symbol.
It is best to use this only the last argument, as xType won't do anything special if you make the first argument optional.
var test = xtype; ; // true; // true test = xtype; ; // true; // true; // true; // false
getFn
Use this to get a function definition by it's name.
Will throw an error if the definition doesn't exist.
var test = xtype;
guard
Use this to 'guard' a function, that will only run if validation passes.
xtype; var { // can assume that string is actually a string // and that number is really a number}; // Guard fnvar guard = xtype; // will call fn; // will not call fn;;;