Open mplemay opened 2 months ago
Do you use Superforms in SPA mode for this? (As it's not a Svelte library, but SvelteKit)
@ciscoheat Sorry for the delayed response - I have been out traveling (and away from my computer).
To answer your question, yes, I have been using Superforms in SPA mode. In the +page.ts
I do something very similar to this example. Currently, I used a zod model for validation, but ideally I would love to remove all of the validation from zod
and return the messages sent from API endpoint; thus effectively treating the endpoint as a validator of sorts.
For instance, if we were to setup a FastAPI (i.e. OpenAPI) server to handle the example above, we would have a schema struct (automatically generated by https://openapi-ts.pages.dev/ or in general) that looks like:
interface UserSchema {
id: integer
name: string
email: string
}
When we called the API, it would return a json object with validation errors which at least in the case of fastapi looks something like this:
{
"detail":[
{
"loc":[
"name"
],
"msg":"field required",
"type":"value_error.missing"
},
{
"loc":[
"id"
],
"msg": "value is not a valid integer",
"type": "type_error.float"
}
]
}
Given this paradigm, where the "schema" is separated from the "validator", how would one go about integrating this into superforms (if this is a workflow that superforms supports)?
I'm thinking that you can use the onUpdate event, call the API there and loop through its validation errors with setError, similar to https://superforms.rocks/concepts/spa#displaying-the-form
Is your feature request related to a problem? Please describe. Many backends for svelte apps are written in python using FastAPI and Pydantic. When a project uses this stack, Sveltekit Superforms is not able to natively display the validation errors returned by this backend.
Describe the solution you'd like While pydantic is not a javascript validation library, it does do validation (and return validation errors) and a schema is available through the OpenAPI Specification. As such, I was wondering if it would be possible to parse and show the validation errors for form fields returned by pydantic.
Describe alternatives you've considered Currently, I either replicate the schema I originally defined in Pydantic using zod or I don't use Sveltekit Superforms for validation.