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

Accept custom schemas via a ZodSchema() method #10

Merged

Conversation

josiah-tt
Copy link
Contributor

Passing a centralized map of custom schema functions to NewConverter() separates the custom schema definition from its type. Sometimes this is necessary (as in the shopspring/decimal.Decimal example), but when the supervillain user has control over the type that needs a custom schema, it's nicer to define a method that returns the schema.

New examples are included in the custom/ directory.

New examples are included in the custom/ directory.
@Southclaws
Copy link
Owner

Really nice, thanks!

@Southclaws Southclaws merged commit 17642c7 into Southclaws:master Dec 13, 2023
@josiah-tt josiah-tt deleted the josiah/schema-interfaces branch December 14, 2023 19:23
josiah-tt added a commit to token-transit/supervillain that referenced this pull request Jan 10, 2024
PRs Southclaws#10 and Southclaws#11 added reflection-based checks for interface implementations
(internal interfaces defining a ZodSchema() methods, and the json.Marshaler
interface). But since ConvertType() unwraps pointers before proceeding with
conversion, and the underlying type does not implement the interface if the
method was defined with a pointer receiver, we did not detect interface
implementations using pointer receivers.

This change addresses that problem by checking reflect.Implements() both for
the type directly and for a pointer to the type.
josiah-tt added a commit to token-transit/supervillain that referenced this pull request Jan 10, 2024
PRs Southclaws#10 and Southclaws#11 added reflection-based checks for interface implementations
(internal interfaces defining a ZodSchema() methods, and the json.Marshaler
interface). But since ConvertType() unwraps pointers before proceeding with
conversion, and the underlying type does not implement the interface if the
method was defined with a pointer receiver, we did not detect interface
implementations using pointer receivers.

This change addresses that problem by checking .Implements() both on the type
directly and on a pointer to the type.
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

Successfully merging this pull request may close these issues.

2 participants