I am trying out sailsJs with mongodb using the sails-mongo adapter. After adding validations to a model, I get the following response when the validation fails.
Users.js Model:
module.exports = {
schema: true,
attributes: {
name: {
type: "string",
unique: true
},
email: {
type: "email",
unique: true
},
password: {
type: "string",
required: true
}
}
}
Validation error while using sails-mongo adapter:
{
"error": {
"error": "E_UNKNOWN",
"status": 500,
"summary": "Encountered an unexpected error",
"raw": {
"name": "MongoError",
"code": 11000,
"err": "E11000 duplicate key error index: eReporterDB.users.$name_1 dup key: { : \"codin\" }"
}
}
}
I get a better formatted response if I use the in development database which is the sails-disk adapter.
Validation error while using sails-disk adapter:
{
"error": {
"error": "E_VALIDATION",
"status": 400,
"summary": "2 attributes are invalid",
"invalidAttributes": {
"name": [
{
"value": "codin",
"rule": "unique",
"message": "A record with that `name` already exists (`codin`)."
}
]
}
}
}
As a developer, I would expect a standardized response from a framework,
Can anyone help me with a graceful way of handling such validation errors.
I mean I cannot just show the error "E11000 duplicate key error index: eReporterDB.users.$name_1 dup key: { : \"codin\" }"
to a layman user.
Thanks.