Validate function return type #780
Unanswered
campbed-adsk
asked this question in
Q&A
Replies: 1 comment
-
Have a look at #243 (comment). import * as v from 'valibot';
// Simple but less readable version
const Schema1 = v.pipe(
v.function(),
v.transform((func) => () => v.parse(v.promise(), func()))
);
// Longer and more readable version
const Schema2 = v.pipe(
v.function(),
v.transform((func) => {
return () => {
const result = func();
const parsed = v.parse(v.promise(), result);
return parsed;
};
})
); And if your function have import * as v from 'valibot';
// Simple but less readable version
const Schema1 = v.pipe(
v.function(),
v.transform(
(func) =>
(...args: unknown[]) =>
v.parse(v.promise(), func(...args))
)
);
// Longer and more readable version
const Schema2 = v.pipe(
v.function(),
v.transform((func) => {
return (...args: unknown[]) => {
const result = func(...args);
const parsed = v.parse(v.promise(), result);
return parsed;
};
})
); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is it possible to validate a functions return type using Valibot?
To be more specific, I am trying to replace my Zod implementation
z.function().returns(z.void().promise())
with something like
v.pipe(v.function(), v.returns(v.void().promise()))
Zod docs from original use: https://zod.dev/?id=functions
Valibot docs for
functions
: https://valibot.dev/api/function/Beta Was this translation helpful? Give feedback.
All reactions