Get an unified messaging from a Schema ValidationError in mongoose.
$ npm install --save flatten-mongoose-validation-error
const flattenMongooseValidationError = require('flatten-mongoose-validation-error');
const schema = new Schema({
foo: {
type: String,
required: true
},
bar: {
type: String,
required: true
}
});
const foo = new FooModel();
foo.save((error) => {
const message = flattenMongooseValidationError(error);
console.log(message);
// Path `foo` is required. Path `bar` is required.
const messageSeparatedByDash = flattenMongooseValidationError(error, ' - ');
console.log(message);
// Path `foo` is required. - Path `bar` is required.
});
Returns a String
with all messages separated by separator.
MIT © Nepente