Skip to content

nepente/flatten-mongoose-validation-error

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Flatten messages in mongoose ValidationError

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published