DEV Community

Discussion on: A Clean Approach to Using Express Validator

Collapse
 
pearsonhill profile image
PearsonHill

hey Chinedu - kept searching and found my answer! I am fired up - see below if anyone else is interested:

const { body, check, validationResult } = require('express-validator')
const Team = require('../models/Team')

const teamValidationRules = () => {

return [
// is name present
check('name'," a string 'name' is required.")
.notEmpty().isString()
.custom(
async (value, {req}) => {
const check = await Team.findOne({name: value})

           if(check){
            return Promise.reject('Name is already in use');
           }


        }),
Collapse
 
nedsoft profile image
Chinedu Orie

Great job, I just saw your comment, good to know you've been able to tackle it