leepowelldev / mongoose-validator

Validators for mongoose models utilising validator.js
MIT License
378 stars 43 forks source link

error is in the validationError #49

Closed Twois closed 6 years ago

Twois commented 6 years ago

We got the following when write out to the console:

************************
{ ValidatorError: Invalid email address!
   at new ValidatorError (C:\wamp\www\gregmidi\server\dist\node_modules\mongoose\lib\error\validator.js:27:11)
   at validate (C:\wamp\www\gregmidi\server\dist\node_modules\mongoose\lib\schematype.js:959:13)
   at C:\wamp\www\gregmidi\server\dist\node_modules\mongoose\lib\schematype.js:995:9
   at Array.forEach (<anonymous>)
   at SchemaString.SchemaType.doValidateSync (C:\wamp\www\gregmidi\server\dist\node_modules\mongoose\lib\schematype.js:973:14)
   at C:\wamp\www\gregmidi\server\dist\node_modules\mongoose\lib\document.js:1840:17
   at Array.forEach (<anonymous>)
   at model.Document.validateSync (C:\wamp\www\gregmidi\server\dist\node_modules\mongoose\lib\document.js:1824:9)
   at SaveUser_DashboardEvent.<anonymous> (C:\wamp\www\gregmidi\server\dist\events\dashboard\user\save-user.event.js:63:39)
   at Generator.next (<anonymous>)
   at C:\wamp\www\gregmidi\server\dist\events\dashboard\user\save-user.event.js:7:71
   at new Promise (<anonymous>)
   at __awaiter (C:\wamp\www\gregmidi\server\dist\events\dashboard\user\save-user.event.js:3:12)
   at SaveUser_DashboardEvent.processEvent (C:\wamp\www\gregmidi\server\dist\events\dashboard\user\save-user.event.js:24:16)
   at SaveUser_DashboardEvent.self.callOnPermission (C:\wamp\www\gregmidi\server\dist\events\dashboard\user\save-user.event.js:21:23)
   at SaveUser_DashboardEvent.<anonymous> (C:\wamp\www\gregmidi\server\dist\events\core\core.event.js:33:17)
   at Generator.next (<anonymous>)
   at fulfilled (C:\wamp\www\gregmidi\server\dist\events\core\core.event.js:4:58)
   at process._tickCallback (internal/process/next_tick.js:68:7)
 message: 'Invalid email address!',
 name: 'ValidatorError',
 properties:
  { validator: [Function: validator],
    message: 'Invalid email address!',
    type: 'user defined',
    path: 'email',
    value: 'asdfgaslgf' },
 kind: 'user defined',
 path: 'email',
 value: 'asdfgaslgf',
 reason: undefined,
 '$isValidatorError': true }
************************

How we use: Mongoose version: ^5.2.9 validator.js: ^10.4.0

UserModel:

var emailValidator = [
   validate({
       validator: 'isEmail',
       passIfEmpty: true,
       message: 'Invalid email address!'
   })
];

export let UserSchema: Schema = new Schema({
   "email": {
       "type": String,
       "unique": true,
       "required": true,
       'validate': emailValidator
   },
   "firstname": {
       "type": String,
       "required": true
   },
   "surname": {
       "type": String,
       "required": true
   },
   "password": {
       "type": String,
       "required": true
   },
}, {
   "minimize": false,
   "timestamps": true,
   "collection": 'users',
   'toJSON': { getters: true, virtuals: true },
   'toObject': { getters: true, virtuals: true }
});

On usage:

self.msg.data.password = Devora.toPassword(self.msg.data.passwords.password);
const member: IUser = new UserModel(self.msg.data);

const errors:any = member.validateSync();

console.log('************************');
console.log(errors.errors.email);
console.log('************************');

What is that can cause the problem? Is it normal to see the stackTree?

leepowelldev commented 6 years ago

Yes this is correct - error creation is handled by Mongoose, not this library.