Is possible to customize the error from the
Sequelize.ValidationError
Model:
var PaymentType = sequelize.define('payment_type' , {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true,
field: 'id'
},
code: {
type: DataTypes.STRING,
allowNull: false,
validate:{
notEmpty: true
},
field: 'code'
},
name: {
type: DataTypes.STRING,
allowNull: false,
validate:{
notEmpty: true
},
field: 'name'
}
}, {
timestamps: true,
paranoid: false,
underscored: true,
freezeTableName: true,
tableName: 'payment_types'
});
My controller:
update(req, res) {
paymentType
.update(req.body, {
where: {
id: req.params.id
}
})
.then( updatedRecords => {
res.status(200).json(updatedRecords);
})
.catch(Sequelize.ValidationError, error => {
res.status(400).json(error);
})
.catch( error => {
res.status(500).json(error);
});
},
The errors I get, are this way:
{
"name": "SequelizeValidationError",
"message": "Validation error: Validation notEmpty failed",
"errors": [
{
"message": "Validation notEmpty failed",
"type": "Validation error",
"path": "name",
"value": {},
"__raw": {}
}
]
}
I want to pass the errors like this(only path and message):
{
"name":"The field cannot be empty",
"other_field":"custom error message"
}
I don't know if I can specified a custom message in the model or I have to create a function to build the errors messages.
If I have to build a function how can I extract the path and customize the message.
Thanks in Advance
via joselegit
No comments:
Post a Comment