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:
- 'name must non empty' not coming anywhere
- value coming undefined
is there way change message: 'validator "required" failed path name value
undefined'
? correct approach?
the issue was using older version mongoose. after updating it, working fine. updated version 4.5.3
Comments
Post a Comment