Comments (9)
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.
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.
Thanks ! I’ll wait to see how you handle this 👍
from sveltekit-superforms.
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.
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.
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.
Looks like this (at least the specific issue I was encountering) may have been fixed in 2.6.1
from sveltekit-superforms.
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.
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)
- New reset-behaviour HOT 18
- Small typo in docs: reference to z despite switching the validation library to Joi HOT 2
- In SPA mode, navigating to a page that triggers a redirect inside `onUpdate` fails silently HOT 1
- TypeError: Cannot read properties of undefined (reading 'defaults') at Module.superValidate HOT 1
- Reverse In and Out type parameter in superForm return type HOT 1
- FormPathLeaves<FormObj, Date> = never HOT 2
- When I build, I am getting a Circular Dependency warning. HOT 4
- using pick on a valibot schema causes superforms to fail to submit when use:enhance is on. HOT 3
- Allow using superForms without a schema HOT 5
- Zod superRefine not updating $errors HOT 5
- zod() Incompatible with z.ZodType HOT 3
- Version 2.6.2 breaks my SvelteKit app, causing infinite loading on localhost HOT 3
- Custom callback for when submission is completed HOT 5
- Form gets cleared after submit. HOT 3
- Error: Unsupported valibot schema: enum HOT 2
- Support for schema references ($ref) HOT 4
- Target attribute not supported on <form> with use:enhance HOT 2
- Superdebug collapsed too much when without label HOT 7
- SuperForms doesn't quite support input names including `.` (setError(), splitPath()) HOT 1
- Support for "raw" JSON schema HOT 14
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sveltekit-superforms.