ciscoheat / sveltekit-superforms

Making SvelteKit forms a pleasure to use!
https://superforms.rocks
MIT License
2k stars 59 forks source link

Only Zod schema objects can be used with superValidate. #178

Closed danielbayley80 closed 1 year ago

danielbayley80 commented 1 year ago

When I define my Zod in another module (which I do to enable reuse across projects), I get:

"SuperFormError: Only Zod schema objects can be used with superValidate."

export const cmsSiteZod = z.object({

      _id : z.string().uuid(),
      dateAdded : z.date(),
      dateUpdated  : z.date(),
      defaultLanguage : z.string().length(2) ,
      siteName : z.string().min(5).max(128),
      siteDescription : z.string().min(0).max(255),

      defaultDomain : z.string().min(5).max(128),
      domains: z.array(z.string())
    });

The same object when defined in the SvelteKit project works.

Or if I create my zod object in my module as follows :

export const cmsSiteZod = {
      _id : z.string().uuid(),
      dateAdded : z.date(),
      dateUpdated  : z.date(),
      defaultLanguage : z.string().length(2) ,
      siteName : z.string().min(5).max(128),
      siteDescription : z.string().min(0).max(255),
      defaultDomain : z.string().min(5).max(128),
      domains: z.array(z.string())
    };

then do this in svelete:

const cmsSiteVal = z.object(cmsSiteZod);

it recognizes it.

vscode seems to recognize the types correctly.

I should add I had trouble getting my modules working in SveleteKit vs my express projects, but I think I cracked it and it works in other areas.

ciscoheat commented 1 year ago

It's problematic to use schemas between modules because it will be a prototype mismatch, so instanceof checks will fail, and there are a couple of those in Superforms. Zod has a way of handling this, but I'm not sure if I can squeeze that in before 1.0.

danielbayley80 commented 1 year ago

Understood. I like to use lots of modules to allow reuse of key code across projects. I have a workaround creating the z.object() in the svelte page which only adds a line of code and a bit of tweaking to my modules. Otherwise I'm really enjoying working with the library so far.

ciscoheat commented 1 year ago

Fortunately it wasn't that much work to fix, so it will be no problem to use it cross-module schemas for 1.0! Just a few days from release.

ciscoheat commented 1 year ago

Is it working now with 1.0?

ciscoheat commented 1 year ago

Closing this, as all the tests are passing.