Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom validation async validator #2381

Closed
TheDhejavu opened this issue Jun 7, 2020 · 4 comments
Closed

Custom validation async validator #2381

TheDhejavu opened this issue Jun 7, 2020 · 4 comments
Assignees
Labels
follow instructions The issue failed to follow instructions, try again

Comments

@TheDhejavu
Copy link


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

@TheDhejavu TheDhejavu added the feature New functionality or improvement label Jun 7, 2020
@brianle1301
Copy link
Contributor

@TheDhejavu Use any.external()

@hueniverse hueniverse self-assigned this Jun 10, 2020
@hueniverse hueniverse added follow instructions The issue failed to follow instructions, try again and removed feature New functionality or improvement labels Jun 10, 2020
@niteshrawat1995
Copy link

@brianle1301 Can you please share code snippet for it?

@bnymn
Copy link

bnymn commented Dec 28, 2021

Can we use the combination of string() and any()?

@jonyeezs
Copy link

FYI: The current documentation on external is much to be desired. It doesn't have a very good helper nor information on how to return an invalid schema (throw doesn't seem to work nicely). There's this PR that will make external work very similar to custom #2773

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
follow instructions The issue failed to follow instructions, try again
Projects
None yet
Development

No branches or pull requests

6 participants