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

Could msgspec be used to specify expected output types when encoding? #664

Open
Lance-Drane opened this issue Apr 11, 2024 · 0 comments
Open

Comments

@Lance-Drane
Copy link

Lance-Drane commented Apr 11, 2024

Question

This is something I'm not sure should be a feature request.

Validating data against a schema as you're encoding it is more niche than validating data as you're decoding it, but there are certain usecases where this could be valuable. For example, imagine a library which handles everything from encoding/decoding to the transport layer, but allows for users to specify input/output typings that msgspec can handle and requires that users type annotate a function (for the purposes of generating a schema), in a similar fashion to a REST API. The library would then check to see that the user's return value function matches the return type annotation of their function, and would ideally do type validation through msgspec.

The API could look similar to the decoder's API, i.e. encoder.encode(12, type=int, strict=True) would validate but encoder.encode("blue", type=int, strict=True) would raise a validation error.

This is something that Pydantic supports, but this is due to them needing to construct a core_schema as they build the serialized instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant