Closed iyezhou closed 7 years ago
You must ensure that req.body is valid otherwise User.check will throw
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 = result.value // <= instance of validate.User.check
}
}
@gcanti Also can't pass validation, throws code 422
[
{
"message": "Invalid value \"30\" supplied to /age: Number",
"actual": "30",
"path": [
"age"
]
},
{
"message": "Invalid value \"1\" supplied to /active: Boolean",
"actual": "1",
"path": [
"active"
]
}
]
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));
});
}
}
how to convert a http param(String) to a Number value
hi, when I use restify create an API, and use validation like:
when I send a post request with params "age=20", then throw an Exception, cannot cat age param type to number