question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Order of validations

See original GitHub issue

If have this validation schema that i use to validate a youtube video id as part of a multi page form wizard:

const firstPageSchema = yup.object().shape({
  youtubeId: yup
    .string()
    .min(11, 'has to be 11 characters')
    .max(11, 'has to be 11 characters')
    .matches(
      /^[a-zA-Z0-9_-]{11}$/,
      'allowed characters are letters, numbers, underscore and dash'
    )
    .test(
      'youtube-api',
      'video not found or not embeddable',
      async value => {
        const parts = 'snippet,contentDetails,status'
        const response = await fetch(
          `${YOUTUBE_API_V3}/videos?id=${value}&key=${YOUTUBE_API_KEY}&part=${parts}`
        )
        const body = await response.json()
        return body.items.length !== 0 && body.items[0].status.embeddable
      }
    )
    .required(),
})

Whenever i validate a string that has less than 11 characters, the .test is executed and tries to fetch information about the video from youtube.

I would like the .test to only run when .min, .max and .matches validation did not produce an error. How would i do that?

I tried when on the same field, but that seems to be forbidden.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:13
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

14reactions
ryanjwesselcommented, Aug 12, 2019

Have there been any updates with this? Sequential validation would definitely be useful.

Also, I’m not sure but it seems like .sequence is something that was custom-built for the previous example and not part of the official yup API. Is that correct?

Edit: Should have looked at the PR created by @codepunkt before asking the question. My bad!

8reactions
evankennedycommented, Aug 12, 2019

This would be pretty valuable. For anyone coming here wondering if they can do the same thing, you could save the ID format schema and call it within your other schema.

const youtubeIdSchema = yup.string()
  .length(11, 'has to be 11 characters')
  .matches(
    /^[a-zA-Z0-9_-]{11}$/,
    'allowed characters are letters, numbers, underscore and dash'
  );

const firstPageSchema = yup.object({
  youtubeId: youtubeIdSchema
    .test(
      'youtube-api',
      'video not found or not embeddable',
      async value => {
        if (await youtubeIdSchema.isValid(value)) {
          const parts = 'snippet,contentDetails,status';
          const response = await fetch(
            `${YOUTUBE_API_V3}/videos?id=${value}&key=${YOUTUBE_API_KEY}&part=${parts}`
          );
          const body = await response.json();
          return body.items.length !== 0 && body.items[0].status.embeddable;
        }
        return true;
      }
    )
    .required(),
});

That would double-validate the first schema, but those are simple validations so that’s probably okay.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Control validation annotations order? - Stack Overflow
Use JSR-303 validation groups. If no groups are specified a constraint is part of the Default Bean Validation group (see: javax.validation.groups.Default ).
Read more >
Order validation - IBM
After items are added to an order, validations must be performed to verify whether the items or the quantity of the items in...
Read more >
Introduce an evaluation order for constraints defined on a ...
This solution will only work on constraints written for Bean Validation 1.1 and above as we would require to add an order parameter...
Read more >
Spring MVC Validation Order Example - JavaPointers
In this spring mvc validation order example, we will demonstrate how to use validation groups to set the order or sequence of validation....
Read more >
Grouping Javax Validation Constraints - Baeldung
A quick and practical overview of grouping Javax validation constraints. ... Specifying Constraint Group Validation Order with GroupSequence.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found