node.js - Having custom validation messages for schema violations in mongodb and nodejs -


my schema following:

var mongoose     = require('mongoose'); var schema       = mongoose.schema;  var studentschema   = new schema({     name: {         type: string,         required: [true, 'name must non empty']     },     family: {         type: string,         required: [true, 'family must non empty']     },     subjects: {         type: [string],         validate: [{             validator: function(val) {             return val.length > 0;             },             msg: 'continents must have more or equal 1 elements',             errorcode: 25         }         ]     },     created: {         type: date,         default: date.now     }, }); 

but when post json without name, see following error object:

{ [validationerror: validation failed]   message: 'validation failed',   name: 'validationerror',   errors:     { name:        { [validatorerror: validator "required" failed path name value `undefined`]         message: 'validator "required" failed path name value `undefined`',         name: 'validatorerror',         path: 'name',         type: 'required',         value: undefined } } } 

so, there issue above response:

  1. 'name must non empty' not coming anywhere
  2. value coming undefined

is there way change message: 'validator "required" failed path name valueundefined'? correct approach?

the issue was using older version mongoose. after updating it, working fine. updated version 4.5.3


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -