tcomb-validation
tcomb-validation copied to clipboard
http post validation (Number Params Problem)
hi, when I use restify create an API, and use validation like:
var check = t.struct({
username: t.String,
realname: t.String,
password: t.String,
age: t.Number,
active: t.Boolean
});
module.exports = {
name : 'User',
check: check
};
function create(req, res, next) {
var result = t.validate(req.body, validate.User.check);
if (!result.isValid()) {
res.send(422, result.errors);
} else {
//var values = new validate.User.check(req.body);
var values = result.value;
//var values = new validate.User.check(result.value);
console.log(values);
return User.create(values)
.then(function (entity) {
res.send(entity);
})
.catch(function (err) {
return next(new errors.InternalError(err.message));
});
}
}
when I send a post request with params "age=20", then throw an Exception, and return 422, cannot cat age param type from string to number
[
{
"message": "Invalid value \"30\" supplied to /age: Number",
"actual": "30",
"path": [
"age"
]
},
{
"message": "Invalid value \"1\" supplied to /active: Boolean",
"actual": "1",
"path": [
"active"
]
}
]
Relevant https://github.com/gcanti/tcomb-validation/issues/61#issuecomment-307301841