Giter Club home page Giter Club logo

Comments (9)

ciscoheat avatar ciscoheat commented on May 27, 2024 1

It's a matter of type-safety - if the type is YourEnum (as it is with required fields) compared to YourEnum | undefined (optional fields), then it must have a value, it cannot be undefined.

I'm considering adding an enumProxy or similar to handle this.

from sveltekit-superforms.

wesharper avatar wesharper commented on May 27, 2024 1

Our app has giant forms with dozens of required select inputs, so having to do this manually for each form is a pain.

Also, we have a handful of large forms that will autosave a draft using a partial schema and validate a full schema and publish a record on submit. I will let you know what that mess looks like once I start migrating those larger forms over.

from sveltekit-superforms.

Nelhoa avatar Nelhoa commented on May 27, 2024

Thanks ! I’ll wait to see how you handle this 👍

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on May 27, 2024

To make it work with all validation libraries, you have to be able to map the inferred schema type T to a type that extends certain fields in it with for example undefined or even an empty string. But I don't think that's the whole solution, as you need to specify the default (empty) values somehow too.

The best workaround I can come up with is to modify the adapter defaults.

const schema = z.object({
  dir: z.enum(['north', 'south', 'east', 'west'])
});

const adapter = zod(schema);
adapter.defaults.dir = '' as 'north';

const form = await superValidate(adapter);

from sveltekit-superforms.

Nelhoa avatar Nelhoa commented on May 27, 2024

Don’t you think that it may run into the same issue ?

I’m not sure to understand the role of the adapter.

If dir = '' instead of undefined, the select fields will consider themselves as selected ?

If the user click on submit he won’t have an HTML alert with a tooltip on the select field, but he’s going to have a Zod error message telling the value doesn’t fit the enum.

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on May 27, 2024

Modifying the adapter defaults makes it possible to have a required enum field, while still having a default value that isn't part of the enum.

dir cannot be undefined in the above schema, but an empty string will coerce with the strings in the enum, so you don't have to cast with as unknown. In either case, no option will be initially selected.

from sveltekit-superforms.

shellicar avatar shellicar commented on May 27, 2024

Looks like this (at least the specific issue I was encountering) may have been fixed in 2.6.1

from sveltekit-superforms.

wesharper avatar wesharper commented on May 27, 2024

This also works in the server, but it would be very nice if it was handled natively by superForms:

// +page.server.ts
const form = await superValidate(zod(mySchema));
form.data.dir = undefined as unknown as Direction;

return { form }

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on May 27, 2024

I suggest making a helper function/wrapper for superValidate, as this problem won't easily go away, due to how the JSON schema generators are detecting required fields.

from sveltekit-superforms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.