Skip to content

S.taggedUnion for easier creation of tagged unions vs S.union and S.attachPropertySignature #1834

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

Open
jessekelly881 opened this issue Aug 6, 2023 · 2 comments · May be fixed by #4736
Open
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. schema

Comments

@jessekelly881
Copy link
Contributor

jessekelly881 commented Aug 6, 2023

Instead of having to use S.union with S.attachPropertySignature directly to create tagged objects, a simpler solution might be to add a S.attachSignatures helper that fn that abstracts this behavior. The naming might not be the best but the idea is that the original api and the alternative api below produce identical schemas.

const Circle = S.struct({ radius: S.number });
const Square = S.struct({ sideLength: S.number });

// original api

const DiscriminatedShape = S.union(
  Circle.pipe(S.attachPropertySignature("kind", "circle")),
  Square.pipe(S.attachPropertySignature("kind", "square")),
); 

// alternative (/complimentary) api

S.attachSignatures("kind", {
  "circle": Circle,
  "square": Square
})
@jessekelly881 jessekelly881 changed the title S.taggedUnion for easier creation of Schemas with attachPropertySignature S.taggedUnion for easier creation of Schemas vs union and attachPropertySignature Aug 6, 2023
@jessekelly881 jessekelly881 changed the title S.taggedUnion for easier creation of Schemas vs union and attachPropertySignature S.taggedUnion for easier creation of tagged unions vs S.union and S.attachPropertySignature Aug 6, 2023
@fubhy fubhy added the schema label Jan 1, 2024
@fubhy fubhy transferred this issue from another repository Jan 1, 2024
@DadeSko DadeSko moved this to Created in Issues Management Jan 30, 2024
@gcanti gcanti added the Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. label Mar 15, 2024
@mikearnaldi
Copy link
Member

Arguably not needed but we could do:

Schema.taggedUnion({ Circle, Square })

where _tag gets automatically added

@jessekelly881
Copy link
Contributor Author

That seems like the best approach.

@alphashuro alphashuro linked a pull request Apr 14, 2025 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. schema
Projects
Status: Created
Development

Successfully merging a pull request may close this issue.

4 participants