Define contracts for functions and express handlers to validate and filter parameters. This library bundles the Json Schema Validator package with customizable format validation and filter support. It includes some nice filters and formats from the node-validator library.
var contracts = require('contracts')
, f = contracts.filters;
var addSchema = {
type: 'array',
items: {
type: 'number',
filter: f.toFloat()
}
};
var userSchema = {
type: 'object',
additionalProperties: false,
filters: f.cleanObject(),
properties: {
email: { type: 'string', required: true, format: 'email' },
password: { type: 'string', required: true, minLength: 6 },
age: { type: 'integer', required: true, filter: f.toInt() }
}
};
var report = contracts.validate([5, '13', 8], addSchema);
report.errors; // []
report.instance; // [5, 13, 8]
var new_obj = contracts.transform({email: '[email protected]', password: 'foobar', age: '15', DANGEROUS: '...'}, userSchema);
new_obj; // {email: '[email protected]', password: 'foobar', age: 15}
var add = contracts.wrap(function() {
var args = [].slice.call(arguments)
, sum = args.reduce(function(a,b) { return a + b; }, 0);
return sum;
}, addSchema);
add('5', '19.5', 3); // 27.5
add(1, {}); // throw ValidationError()
var app = express.createServer();
app.use express.bodyParser();
app.post('/users', contracts.view(userSchema), function(req, res) {
req.body; // {email: '[email protected]', password: 'foobar', age: '15', DANGEROUS: '...'}
req.data; // {email: '[email protected]', password: 'foobar', age: 15}
});
- cleanObject
- cleanArray
- removeEmpty
From node-validator:
- xss
- entityDecode
- entityEncode
- ltrim
- rtrim
- trim
- ifNull
- toFloat
- toInt
- toBoolean
- toBooleanStrict
From node-validator:
- url
- ip
- int
- decimal
- float
- uuid
var schema = {
type: 'string',
filter: function(str) {
return str.replace(/-/g, '');
}
}
contracts.filters.add('stripSlashes', function(str) { return str.replace(/-/g, ''); });
schema = {
type: 'string',
filter: contracts.filters.stripSlashes().trim()
}
contracts.formats.add('ssn', function(str) {
// TODO: Validate that str is a ssn.
return true;
});
var schema = {
type: 'string',
format: 'ssn'
}
(The MIT License)
Copyright (c) 2011 Eirikur Nilsson <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.