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

Validate test() only after all other validations #503

Closed
kevinwolfcr opened this issue Mar 25, 2019 · 2 comments
Closed

Validate test() only after all other validations #503

kevinwolfcr opened this issue Mar 25, 2019 · 2 comments

Comments

@kevinwolfcr
Copy link

kevinwolfcr commented Mar 25, 2019

The problem

Hello. I am wondering if there is a way to only run the async validations at mixed.test() only after all other validations are successful.

Scenario

I have a schema that for the email path, has .email(), .required() and .test() validations. I want the .test() validation to run only if the other two validations are correct. On that way, I won't send an empty or invalid email to my asynchronous validation function.

Schema

const schema = yup.object().shape({
  firstName: yup.string().required(),
  lastName: yup.string().required(),
  email: yup
    .string()
    .email()
    .required()
    .test('is-incorrect', '${path} should be hi@kevinwolf.me', async value => {
      console.log('running async validations')
      await sleep(1)
      return value === 'hi@kevinwolf.me'
    }),
})

Validations

This shouldn't log anything to the console

schema.validate({ firstName: 'Kevin', lastName: 'Wolf', email: '' })
schema.validate({ firstName: 'Kevin', lastName: 'Wolf', email: 'invalid email' })

This should

schema.validate({ firstName: 'Kevin', lastName: 'Wolf', email: 'hi@kevinwolf.me' })
@DaddyWarbucks
Copy link
Contributor

This is not currently possible because all validations are run at the same time to maintain performance. There have been a number of other requests for this as well. For example, this is a similar request: #256.

I am in favor of there being some way to handle some or all validations in some order/sequence.

@SwapnilSoni1999
Copy link

Hi there I've checked several other issues and wanted to ask if its possible now to do checks sequencially ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants