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

Impossible to assign "config" to Form.Field when the Zod schema has a "refine" #45

Closed
BenocxX opened this issue Sep 17, 2023 · 0 comments · Fixed by #47
Closed

Impossible to assign "config" to Form.Field when the Zod schema has a "refine" #45

BenocxX opened this issue Sep 17, 2023 · 0 comments · Fixed by #47

Comments

@BenocxX
Copy link
Contributor

BenocxX commented Sep 17, 2023

With the following schema:

import { z } from 'zod';

export const formSchema = z
  .object({
    username: z.string().min(2).max(50),
    confirm: z.string(),
  })
  .refine((data) => data.confirm === data.username, {
    message: 'Username and confirm must match',
    path: ['confirm'],
  });

export type FormSchema = typeof formSchema;

I can't assing config to the Form.Field elements:

<script lang="ts">
  import * as Form from '$lib/components/ui/form';
  import { formSchema, type FormSchema } from './schema';
  import type { SuperValidated } from 'sveltekit-superforms';

  export let form: SuperValidated<FormSchema>;
</script>

<Form.Root method="POST" {form} schema={formSchema} let:config>
  <!-- Red error on "config" -->
  <Form.Field {config} name="username">
    <Form.Item>
      <Form.Label>Username</Form.Label>
      <Form.Input />
      <Form.Validation />
    </Form.Item>
  </Form.Field>
  <!-- Red error on "config" -->
  <Form.Field {config} name="confirm">
    <Form.Item>
      <Form.Label>Confirm Username</Form.Label>
      <Form.Input />
      <Form.Validation />
    </Form.Item>
  </Form.Field>
  <Form.Button>Submit</Form.Button>
</Form.Root>

Here's the typescript error for username (sadly it's being cropped by vscode...):

Type '{ form: SuperForm<ZodObject<{ username: ZodString; confirm: ZodString; }, "strip", ZodTypeAny, { username: string; confirm: string; }, { username: string; confirm: string; }>, any>; schema: ZodEffects<...>; }' is not assignable to type 'Form<AnyZodObject>'.
  Types of property 'schema' are incompatible.
    Type 'ZodEffects<ZodObject<{ username: ZodString; confirm: ZodString; }, "strip", ZodTypeAny, { username: string; confirm: string; }, { username: string; confirm: string; }>, { ...; }, { ...; }>' is missing the following properties from type 'ZodObject<any, any, any, { [x: string]: any; }, { [x: string]: any; }>': _cached, _getCached, shape, strict, and 14 more.ts(2322)

And the typescript error for confirm:

Type '{ form: SuperForm<ZodObject<{ username: ZodString; confirm: ZodString; }, "strip", ZodTypeAny, { username: string; confirm: string; }, { username: string; confirm: string; }>, any>; schema: ZodEffects<...>; }' is not assignable to type 'Form<AnyZodObject>'.ts(2322)

Note: the client-side validation seems to still be working. It might be a typescript only issue.

I did the experiment in a fresh Sveltekit projet. I followed the shadcn-svelte installation guide and I reproduced the simple Form component example.

You can see the whole repo here.

Thanks for the help :)

@BenocxX BenocxX changed the title Impossible to assign "config" to field when the Zod schema has a "refine" Impossible to assign "config" to Form.Field when the Zod schema has a "refine" Sep 17, 2023
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

Successfully merging a pull request may close this issue.

1 participant