flatten-mongoose-validation-error
v1.0.1
Published
Flatten messages in mongoose ValidationError
Downloads
5
Maintainers
Readme
flatten-mongoose-validation-error
Get an unified messaging from a Schema ValidationError in mongoose.
Install
$ npm install --save flatten-mongoose-validation-error
Usage
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.
});
API
flattenMongooseValidationError(err, [separator = ' '])
Returns a String
with all messages separated by separator.
License
MIT © Nepente