sveltekit-superforms icon indicating copy to clipboard operation
sveltekit-superforms copied to clipboard

Allow using superForms without a schema

Open mpost opened this issue 11 months ago • 5 comments

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?

mpost avatar Feb 29 '24 09:02 mpost

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

ciscoheat avatar Mar 01 '24 14:03 ciscoheat

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.

mpost avatar Mar 01 '24 14:03 mpost

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

ciscoheat avatar Mar 01 '24 16:03 ciscoheat

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

mpost avatar Mar 01 '24 21:03 mpost

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.

ciscoheat avatar Mar 04 '24 08:03 ciscoheat