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

OpenAPI/Pydantic Validation Support #416

Open
mplemay opened this issue Apr 23, 2024 · 3 comments
Open

OpenAPI/Pydantic Validation Support #416

mplemay opened this issue Apr 23, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@mplemay
Copy link

mplemay commented Apr 23, 2024

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.

@mplemay mplemay added the enhancement New feature or request label Apr 23, 2024
@ciscoheat
Copy link
Owner

Do you use Superforms in SPA mode for this? (As it's not a Svelte library, but SvelteKit)

@mplemay
Copy link
Author

mplemay commented May 22, 2024

@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)?

@ciscoheat
Copy link
Owner

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

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

2 participants