flatten-mongoose-validation-error

1.0.1 • Public • Published

flatten-mongoose-validation-error Build Status

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

Package Sidebar

Install

npm i flatten-mongoose-validation-error

Weekly Downloads

8

Version

1.0.1

License

MIT

Last publish

Collaborators

  • limafelipe