1.0.1 • Published 8 years ago

flatten-mongoose-validation-error v1.0.1

Weekly downloads
6
License
MIT
Repository
github
Last release
8 years ago

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