Skip to content

Custom validation async validator  #2381

@TheDhejavu

Description

@TheDhejavu

const schema = Joi.object().keys({
        firstName: Joi.string().required(),
        lastName: Joi.string().required(),
        password: Joi.string().min(6).pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')).required(),
        email: Joi.string().email().required().custom(async (value, helpers)=>{
            const emailExist = await User.find({ email: value })
            if (!emailExist) {
                return helper.message("Email already exist")

            } else {
                return true
            }
        }),
    });

    await schema.validateAsync(req.body, {abortEarly: false});

I have tried every possible way to make the above code work without using async but still doesn't work.

Any technical explanation on why JOi decides to support only sync custom validator

Metadata

Metadata

Assignees

Labels

follow instructionsThe issue failed to follow instructions, try again

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions