Order of validations
See original GitHub issueIf 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:
- Created 5 years ago
- Reactions:13
- Comments:9 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!
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.
That would double-validate the first schema, but those are simple validations so that’s probably okay.