Replies: 3 comments 5 replies
-
|
Or maybe Imo it makes far more sense to do easier validations first such as a length check, I can't currently think of a use case where doing them after more expensive checks would be useful. |
Beta Was this translation helpful? Give feedback.
-
const customPipe2 = v.pipe(
v.array(v.unknown()),
v.maxLength(5),
v.array(v.number())
);
console.log(v.safeParse(
customPipe2,
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,"5"],
{abortPipeEarly: true},
));
let i2: v.InferInput<typeof customPipe2>; // unknown[]
let o2: v.InferOutput<typeof customPipe2>; // number[]This does also work, would it be the recommended solution / workaround? |
Beta Was this translation helpful? Give feedback.
-
|
What is your main motivation: deciding which error to return first or optimizing for maximum performance? If you're trying to maximize performance, have you encountered any noticeable performance issues? I also try to optimize as much as possible, but sometimes it doesn't matter. Currently, Valibot uses a "depth-first" approach, parsing the deeper schemas first, which allows us to make the pipe fully type-safe. Here is another workaround. Try it out in this playground. import * as v from 'valibot';
const arrayOfMaxLenght = (length: number, message?: string) =>
v.custom<unknown[]>(
(input) => Array.isArray(input) && input.length <= length,
message,
);
const Schema = v.pipe(
arrayOfMaxLenght(3, 'Custom error message'),
v.array(v.number()),
); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
If there was a more processing-heavy item schema such as a db lookup or an expensive calculation, ideally we would be able to perform simple checks first such as maxLength to filter out the easier issues. If a user submitted 1000 array items, each one would be validated before the max length of 5 is checked, meaning a ton of extra processing for no reason.
The above does seem to work, although it means copying some of the existing functionality of
v.array, therefore duplicated code means:Array.isArrayin this case)v.arraywas updated, the custom function would be out of syncIt also means more work type hinting everything, which introduces more room for error.
I think overall it would be great to be able to specify the order of validations, for example:
Very rough idea, just the first thing that came to mind. For each validator, the args could instead be an object which contains options, where one is the order in which they should be executed.
Then
v.parserecurses through the schemas and collects all the validation in an array and runs them on each field, in order?I have no experience with production-ready api design so this is likely the completely wrong way to do it, but I hope it helps get the idea across.
I'd love to know people's thoughts on an execution-order feature.
Thanks,
Peter
Beta Was this translation helpful? Give feedback.
All reactions