I would like to transform all mongoose schema errors.
Let's say I've a UserSchema:
const UserSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
required: [true, 'E-Mail is required']
},
password: {
type: String,
required: [true, 'Password is required']
}
}
If I would try to save a user without email and password, I would get following error object back:
{
"errors": {
"password": {
"message": "Password is required",
"name": "ValidatorError",
"properties": {
"type": "required",
"message": "Password is required",
"path": "password"
},
"kind": "required",
"path": "password"
},
"email": {
// ...
}
},
"_message": "User validation failed",
"name": "ValidationError"
}
I would like to transform this error response globally for all my schemas, for example to
{
errors: [
{ message: "Password is required", path: "password" }
// ...
]
}
Is there a way to apply a transform method globally to all Schema errors? (I would like to avoid to transform each response manually)
via mbppv
No comments:
Post a Comment