Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schema validations using Express Validator how to set custom error message?

I am using the "express-validator" middleware for validating form. In that I am using the schema validations and I have some async taks to validate in that, the first one is email validation and the next one is username validation. Some how I got the progress in working them correctly.

But the I am unable to set the proper error messages based on the different conditions. So when you look at my code when email is not valid from DNS then it should show the 'Not a valid email' but it always shows the 'This email is already in use', same is the case with username field. When i do not enter any thing username error shows 'This username is already in use' and when I enter more than 25 chars it shows 'Please enter username'

SO please guide me how to correct this and the get the desired error message.

var validation_rules = checkSchema({
    company_name: {
        errorMessage: 'Company Name should be at least 3 chars long and maximum of 50 chars',
        isLength: {
            options: { min: 3, max : 50 }
        },
        trim: true
    },
    profile_name: {
        errorMessage: 'Public Profile Name should be at least 3 chars long and maximum of 50 chars',
        isLength: {
            options: { min: 3, max: 50 }
        },
        trim: true
    },
    description: {
        errorMessage: 'Company Description should be at least 2 chars long and maximum of 200 chars',
            isLength: {
            options: { min: 2, max: 200 }
        },
        trim: true
    },
    company_street_address: {
        errorMessage: 'Company Address should be at least 3 chars long and maximum of 100 chars',
            isLength: {
            options: { min: 3, max: 100 }
        },
        trim: true
    },
    company_city: {
        errorMessage: 'City should be at least 5 chars long and maximum of 50 chars',
            isLength: {
            options: { min: 5, max: 50 }
        },
        trim: true
    },
    company_state: {
        errorMessage: 'Please select state',
        matches: {
            options: [/^[0-9]+$/],
            errorMessage: "Please enter digits"
        },
        trim: true
    },
    company_zip: {
        errorMessage: 'Zip Code should be at least 4 chars long and maximum of 6 chars',
            isLength: {
            options: { min: 4, max: 6 }
        },
        trim: true
    },
    first_name: {
        errorMessage: 'First Name should be at least 3 chars long and maximum of 50 chars',
        isLength: {
            options: { min: 3, max: 50 }
        },
        trim: true
    },
    last_name: {
        errorMessage: 'Last Name should be at least 3 chars long and maximum of 50 chars',
        isLength: {
            options: { min: 3, max: 50 }
        },
        trim: true
    },
    email: {
        errorMessage: 'Please enter a valid email address',
        isEmail : true,
        trim: true,
        custom: {
            options: (value) => {

                if(value)
                {
                    return new Promise(function (resolve, reject) {

                        dns_validate_email.validEmail(value, function(valid) {
                            console.log('valid:'+valid);
                            if (valid) {
                                resolve(value)
                            }else{
                                reject(new Error({errorMessage: 'Not a valid email'}));
                            }
                        });

                    }).then(function (email) {

                       return new Promise(function (resolve, reject) {
                           User.findBy('email', email, function (err, result) {
                               if(err){
                                   reject('Unable to validate email')
                               }else{
                                   console.log(result);
                                   resolve(result);
                               }

                            });
                       }).then(function(result){return result.length===0});



                    })
                }

            },
            errorMessage: 'This email is already in use',
        }
    },
    phone: {
        errorMessage: 'Please enter Phone Number in 10 digits',
        isLength: {
            options: { min: 10, max: 10 }
        },
        matches: {
            options: [/^\d{10}$/],
            errorMessage: "Please enter digits"
        },
        trim: true
    },
    street_address: {
        errorMessage: 'Company Address should be at least 3 chars long and maximum of 100 chars',
            isLength: {
            options: { min: 3, max: 100 }
        },
        trim: true
    },
    city: {
        errorMessage: 'City should be at least 5 chars long and maximum of 50 chars',
            isLength: {
            options: { min: 5, max: 50 }
        },
        trim: true
    },
    state: {
        errorMessage: 'Please select state',
        matches: {
            options: [/^[0-9]+$/],
            errorMessage: "Please enter digits"
        },
        trim: true
    },
    zip: {
        errorMessage: 'Zip Code should be at least 4 chars long and maximum of 6 chars',
            isLength: {
            options: { min: 4, max: 6 }
        },
        trim: true
    },
    username: {
        errorMessage: 'Please enter username',
        isLength: {
            options: { max: 25 },
            errorMessage: 'Username should not be greater than 25 chars',
        },
        trim: true,
        custom: {
            options: (value) => {
                console.log(value.length)
                if(value.length>0)
                {
                    return new Promise(function (resolve, reject) {

                        User.findBy('username', value, function (err, record) {
                            if (err) {
                                reject('Error validating username');

                            }else{
                                console.log('--------'+record)
                                resolve(record)
                            }

                        });

                    }).then(function (result) {
                        return result.length===0;    

                    })
                }

            },
            errorMessage: 'This username is already in use',
        }
    },
    password: {
        isLength: {
          errorMessage: 'Password should be at least 6 chars long',
          // Multiple options would be expressed as an array
          options: { min: 6 }
        }
    },
    confirm_password: {
        errorMessage: 'Must have the same value as the password field',
        custom: {
            options: (value, { req }) => value === req.body.password
        }
    },
    payment_mode:{
        errorMessage: 'Please select payment option',
        isIn: {
            options: [['Credit Card', 'Monthly Invoice']],
        }
    },
    // Wildcards/dots for nested fields work as well
    'card.number': {
        errorMessage: 'Please enter card number',
        custom: {
            options: (value, { req }) => {

                if(req.body.payment_mode==="Credit Card"){
                    return /^[0-9]{12,19}$/.test(value);
                }else{
                    return true;
                }

            },
            errorMessage: 'Card number must be of 12 and maximum of 19 digits',
        }
    },
    'card.type': {
        errorMessage: 'Please select card type',
        custom: {
            options: (value, { req }) => (req.body.payment_mode==="Credit Card" && value=='')?false:true,
        }
    },
    'card.exp_month': {
        errorMessage: 'Please select expiration month',
        custom: {
            options: (value, { req }) => (req.body.payment_mode==="Credit Card" && value=='')?false:true,
        },
        trim :true
    },
    'card.exp_year': {
        errorMessage: 'Please select expiration year',
        custom: {
            options: (value, { req }) => (req.body.payment_mode==="Credit Card" && value=='')?false:true,
        },
        trim :true
    },
    // Wildcards/dots for nested fields work as well
    'card.cvv': {
        errorMessage: 'Please enter card number',
        custom: {
            options: (value, { req }) => {

                if(req.body.payment_mode==="Credit Card"){
                    return /^[0-9]{3,4}$/.test(value);
                }else{
                    return true;
                }

            },
            errorMessage: 'Security code number must be of 3 and maximum of 4 digits',
        }
    }
});
like image 422
Mohsin Khan Avatar asked Jul 24 '26 12:07

Mohsin Khan


1 Answers

I think you should put the errorMessage inside the isLength property. Like:

username: {
    isLength: {
        options: { min: 6 },
        errorMessage: 'Username must be at least 6 characters long'
    }
}
like image 129
Chris V Avatar answered Jul 27 '26 01:07

Chris V



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!