Skip to content
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

Open
ajotaos opened this issue Dec 20, 2024 · 3 comments
Open

Query validation with "valibot" creates string / array unions. #899

ajotaos opened this issue Dec 20, 2024 · 3 comments

Comments

@ajotaos
Copy link

ajotaos commented Dec 20, 2024

Using the vValidator('query', schema) with an object schema with string keys like this:

v.object({
  foo: v.string()
})

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.

@yusukebe
Copy link
Member

Hi @ajotaos

Thanks. This seems to be a bug.

@yusukebe
Copy link
Member

Hi ZerNico! Can you take a look at this?

@ajotaos
Copy link
Author

ajotaos commented Dec 21, 2024

I was able to get the types working by porting over the zod implementation into valibot, I just was not creative on how to get the lowercase headers done.

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants