JavaScript library to help building functions and methods with qualification checks for specific conditions.
License is MIT.
Install using NPM:
npm install qualify
Use it in the application:
var qualify = require('qualify');
qualify.create
can be used to build functions and methods that validate their arguments and return values.
You can create functions like this:
var foo = qualify.create({
type: 'async',
defaults: 'right',
max: 2,
validate: [{type:'string'}]
}, function(path, fn) {
fn("Path was " + path);
});
...and it would be same as something like this:
var foo = function(path, fn) {
try {
// ... Handle arguments ...
// Actual code block here
fn("Path was " + path);
} catch(e) {
// ... handle exceptions ...
}
};
The type of the function's error handling. This is required if you use differend styles of error handling and want to use those styles to catch conform's errors too.
Possible values are:
"basic"
is the default option. This is a standard function. Conform's errors are handled by throwing exceptions."async"
is a basic async function. Last argument must be a function which can be called with an argument as an optional error message."promise"
the function will return a Promise object (CommonJS/PromiseA). [Not implemented yet!]
Note: This is a planned feature. It is not implemented yet!
Validate arguments for the asynchronous function call based on this schema.
The direction of fulfilling missing arguments. If set as "right"
then all optional arguments are fulfilled from right to left. This is optional argument. Default is "left"
.
How many arguments the function must have. length
will override max
and min
as the same value.
An array for each schema that will be used to specify how arguments are validated. Optional arguments should be set as undefined
.
Validate the return value of the function in our schema format.
The library uses json-schema to validate JavaScript objects. As an exception conform
also supports {type:'function'}
with
support for optional required
field. (However at the moment it does not support functions outside top level.)
Returns true
if what
is a function.
Returns true
if what
is an object.
Is the same as JSONSchema.validate()
except it has extended support for {type:'function'}
. Returns an object in format {valid:true|false, errors:[]}
.