Skip to content

Raising the error (opposite of forwarding) #795

Answered by fabian-hiller
micha149 asked this question in Q&A
Discussion options

You must be logged in to vote

Your goal requires that you validate the numbers in the outer pipeline. Here is an example. Check it out in this playground.

import * as v from 'valibot';

const TimeSpanSchema = v.pipe(
  v.string(),
  v.regex(
    /^\d{4}-\d{4}$/u,
    'Bitte gib zwei Jahreszahlen im Format "YYYY-YYYY" ein.',
  ),
  v.transform((input) => input.split('-').map(Number) as [number, number]),
  v.everyItem(
    (input) => input >= 1950 && input <= 2100,
    'Bitte gib sinnvolle Jahreszahlen ein.',
  ),
  v.check(
    (input) => input[0] <= input[1],
    'Bitte gib die Jahreszahlen in der richtigen Reihenfolge ein.',
  ),
);

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@micha149
Comment options

Answer selected by micha149
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants