ciscoheat / sveltekit-superforms

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

Allow using superForms without a schema #367

Open mpost opened 7 months ago

mpost commented 7 months ago

Running on the latest superForms v2.6.1 i want to make a POST request to delete an item. Therefore i only want to send the item id to delete. My thinking is to use superforms, along with its useful callbacks to perform that task. I do not need an extra schema but would be happy to use the form.id from superforms to transfer the id.

However, creating a superValidated/superForm without schema does not seem to be supported. How would i approach such a scenario?

ciscoheat commented 7 months ago

You can use an empty object schema, or check this example: https://stackblitz.com/edit/sveltekit-superforms-list-actions

mpost commented 7 months ago

Thanks for your answer. This is an interesting example. It has similar requirements to my use-case.

An observation: The "delete" buttons carry the value of the id has defined in the schema. However, that is not required, since in the +page.server.ts the line 28 could be exchanged for the following, using the form id instead of the schema id.

const index = constellations.findIndex((s) => s.id.toString() == form.id);

This would render the schema itself not required.

Could you give an example on how to create an empty superValidate/superForm? I found that a ValidationAdapter is required and there is no adapter which accepts an "empty" object or something similar to z.unkown() (when using zod). Thanks.

ciscoheat commented 7 months ago

Doesn't z.object({}) work?

mpost commented 7 months ago

Creating an empty zod object via superValidate(request, zod(z.object({}))) does work. It would be nice however, to not having to rely on a specific validator implementation to create a dummy validator. Eg.: superValidate(request).

ciscoheat commented 7 months ago

The adapter is quite central to superValidate, so it'll take a bit of work, also to figure out defaults, so it'll have to be a future feature.