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

Enums without default values #341

Open
Nelhoa opened this issue Feb 14, 2024 · 13 comments
Open

Enums without default values #341

Nelhoa opened this issue Feb 14, 2024 · 13 comments
Labels
enhancement New feature or request

Comments

@Nelhoa
Copy link

Nelhoa commented Feb 14, 2024

Thanks for repairing #333
But would it be possible to not have default values for enums ?
I would like to have my select fields empty when the page load.
Instead I’m forced to set values to undefined with typescript errors in the script tags.

Severity : Minor

@Nelhoa Nelhoa added the bug Something isn't working label Feb 14, 2024
@ciscoheat
Copy link
Owner

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.

@ciscoheat ciscoheat added enhancement New feature or request and removed bug Something isn't working labels Feb 14, 2024
@Nelhoa
Copy link
Author

Nelhoa commented Feb 14, 2024

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

@ciscoheat
Copy link
Owner

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);

@Nelhoa
Copy link
Author

Nelhoa commented Feb 19, 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.

@ciscoheat
Copy link
Owner

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.

@shellicar
Copy link

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

@wesharper
Copy link

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 }

@wesharper
Copy link

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.

@ciscoheat
Copy link
Owner

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.

@ciscoheat
Copy link
Owner

Best workaround currently, which keeps things in the schema and with the same type, so it won't cause a native type error:

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

@Nelhoa
Copy link
Author

Nelhoa commented Mar 21, 2024

Thanks but this is dirty x)

@ciscoheat
Copy link
Owner

You can't have it both ways, if you want type-safety you must add '' as a value to the enum, and invalidate it in a refine function.

@ciscoheat
Copy link
Owner

This is type-safe:

const schema = z.object({
  dir: z
    .enum(['', 'north', 'south', 'east', 'west'])
    .refine((dir) => dir != '', 'No direction specified')
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants