-
Notifications
You must be signed in to change notification settings - Fork 186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query validation with "valibot" creates string / array unions. #899
Comments
Hi @ajotaos Thanks. This seems to be a bug. |
Hi ZerNico! Can you take a look at this? |
I was able to get the types working by porting over the export type Hook<
T extends v.GenericSchema | v.GenericSchemaAsync,
E extends Env,
P extends string,
Target extends keyof ValidationTargets = keyof ValidationTargets,
O = {}
> = (
result: v.SafeParseResult<T> & {
target: Target
},
c: Context<E, P>
) => Response | void | TypedResponse<O> | Promise<Response | void | TypedResponse<O>>
type HasUndefined<T> = undefined extends T ? true : false
export const vValidator = <
T extends v.GenericSchema | v.GenericSchemaAsync,
Target extends keyof ValidationTargets,
E extends Env,
P extends string,
In = v.InferInput<T>,
Out = v.InferOutput<T>,
I extends Input = {
in: HasUndefined<In> extends true
? {
[K in Target]?: In extends ValidationTargets[K]
? In
: { [K2 in keyof In]?: ValidationTargets[K][K2] }
}
: {
[K in Target]: In extends ValidationTargets[K]
? In
: { [K2 in keyof In]: ValidationTargets[K][K2] }
}
out: { [K in Target]: Out }
},
V extends I = I
>(
target: Target,
schema: T,
hook?: Hook<T, E, P, Target>
): MiddlewareHandler<E, P, V> =>
// @ts-expect-error
validator(target, async (value, c) => {
const parsed = await v.safeParseAsync(schema, value)
if (hook) {
const hookResult = await hook({ ...parsed, target }, c)
if (hookResult) {
if (hookResult instanceof Response) {
return hookResult
}
if ('response' in hookResult) {
return hookResult.response
}
}
}
if (!parsed.success) {
return c.json({
target,
issues: v.flatten(parsed.issues)
}, 400)
}
return parsed.output
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using the
vValidator('query', schema)
with an object schema with string keys like this:it should result on an inferred type like:
{ foo: string }
but the input type is allowing the following type to pass through:{ foo: string | string[] }
.This behavior is not consistent with
zod
's validator.The text was updated successfully, but these errors were encountered: