Validate an Object that probably came from JSON
Parameter validation for builtins and custom types. Accepts params
and a schema
and returns an array of Error
s or false
.
var validate = require('@smallwins/validate')
function hi(params, callback) {
var schema = {
'name': {required:true, type:Object},
'name.first': {required:true, type:String},
'name.last': {required:false, type:String}
}
var errors = validate(params, schema)
if (errors) {
callback(errors)
}
else {
callback(null, 'hi ' + params.first)
}
}
// logs: null, hi brian
hi({name:{first:'brian', last:'leroux'}}, console.log)
// logs: [ [ReferenceError: missing required param name.first] ]
hi({name:{}}, console.log)
considerations
- For testing an
Object
that presumably came from a JSON payload - Thusly, primarily concerned with JSON value types:
Object
,String
,Number
,Array
andBoolean
- Custom types are easily supported
- Extra custom types bundled:
Email
,ISO
,DateRange
andUUID
- Designed to test keys and nested keys
- Optionally validate
required
- Optionally validate
min
andmax
forString
,Number
andArray
builtin types (and easily implement for custom types / seeDateRange
for an example)
Read full article on GitHub

We are a world class software startup focusing on building scalable, high-performance web & mobile application. We have started our journey back in 2015. And in those years we have provided support for more than 15 thousand customers over the world. We build products that create values. RedQ, Inc