var userSchema = Schema({
email: {
type: String,
unique: true,
match: /^[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,4}$/,
lowercase: true,
trim: true
},
nickname: {
type: String,
trim: true,
required: true
},
password: { type: String, required: true },
url: { type: String, trim: true, default: '' },
role: {
type: String,
enum: [ 'admin', 'reader' ],
default: 'reader'
},
about: { type: String, trim: true },
created: { type: Date, default: Date.now, required: true }
});
I’d like to customize this with something more user friendly, But I don’t know how to set a custom error message for required, unique index and enum failure. How can I do?
I don’t think you can override the error message with the required option. If you need to customize the message in the schema, instead of at the point of the save, I’d think your best bet would be to write a custom validator, like this:
Update:
According to the documentation here, you can do multiple validations like this: