thenativeweb / ensurethat Goto Github PK
View Code? Open in Web Editor NEWensurethat validates function arguments.
ensurethat validates function arguments.
At the moment, a check can only be done for the actual type, such as number
, string
, …
It would be great to also have checks for custom data types such as uuid
, positiveNumber
, and so on. These checks should be pluggable.
At the moment, the object
validator only checks whether a given argument is of type object
or not. It would be awesome if there was a way to validate an object for a specific schema. The syntax might be:
var args = ensure.that(arguments).are({
options: { foo: 'string', bar: [ 'number' ] }
});
This could also be combined with making the entire object optional:
var args = ensure.that(arguments).are({
options: [ { foo: 'string', bar: [ 'number' ] } ]
});
Or, with making it optional and providing a default value:
var args = ensure.that(arguments).are({
options: [ { foo: 'string', bar: [ 'number' ] }, { foo: 'Hello world!', bar: 42 } ]
});
Provide a more lightweight syntax for just validating arguments, without actually modifying them. An idea might be:
ensure.that(arguments).are({
options: {
id: 'uuid',
version: [ 'positiveNumber' ]
},
callback: 'function'
});
You could think of a range
validator which has a min
and a max
value.
Basically the syntax should be something such as:
var args = ensure.that(arguments).are({
first: 'number',
second: [ 'range', 1, 10 ]
});
Unfortunately this interferes with optional arguments. Another idea may be to use object syntax:
var args = ensure.that(arguments).are({
first: 'number',
second: { validator: 'range', min: 1, max: 10 }
});
But somehow this looks … well, strange ;-).
Other ideas?
Look at the code, comments will show the bug:
'use strict';
var ensure = require('node-ensurethat');
var doSomethingVeryComplex = function () {
var args = ensure.that(arguments).are({
first: 'number',
second: [ 'boolean', false ],
callback: [ 'function', function () {console.log('cb default'); } ],
callback2: [ 'function', function () {console.log('cb default2'); } ]
});
console.log(args);
if (args.second === true) {
console.log("second is true?:" + args.second);
args.callback();
} else {
console.log("second is false?:" + args.second);
console.log(args.second);
args.callback2();
}
};
var cb = function () {
console.log('cb real');
};
var cb2 = function () {
console.log('cb real2');
};
doSomethingVeryComplex(23); //all fine
console.log('--------');
doSomethingVeryComplex(23,true); //all fine
console.log('--------');
doSomethingVeryComplex(23,cb); //aspect's "cb2 default" but out will be "cb real" ?
console.log('--------');
doSomethingVeryComplex(23,cb,cb2); //aspect's "cb real2" but out will be "cb real" ?
console.log('--------');
doSomethingVeryComplex(23,true,cb,cb2); //aspect's "cb real" but out will be "cb default" ?
console.log('--------');
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.