Issue
I want to require field depend on other field value; I have field paypaleEmail , I want it to be required when field role is "particular" otherwise not required.
paypalEmail: {
type: String,
required: function () { return this.role == 'particular', 'Paypal Email is required' },
trim: true,
validate: {
validator(email) {
return validator.isEmail(email)
},
message: '{VALUE} is not a valid email!',
},
lowercase: true,
},
when try to send request I got paypalEmail is required
Solution
The "required" function should return a boolean.
Your
function () { return this.role == 'particular', 'Paypal Email is required' }
Unconditionally returns string ‘Paypal Email is required’ which evaluates to true
when casted to bool.
function () { return this.role == 'particular' }
Should do what you expect.