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

operations to check subset and identity of types #598

Closed
faassen opened this issue Aug 20, 2021 · 7 comments
Closed

operations to check subset and identity of types #598

faassen opened this issue Aug 20, 2021 · 7 comments
Labels
wontfix This will not be worked on

Comments

@faassen
Copy link

faassen commented Aug 20, 2021

I find myself with the use case to check whether a Zod type is a subset of another Zod type, and also, to help implement it, whether a Zod type is identical to another Zod type. I saw in the code earlier versions of Zod had an .is operation that has been removed.

So given:

const main = z.object({foo: z.string(), bar: z.string()});
const a = z.object({foo: z.string()});
const b = z.object({foo: z.number()});
const c = z.object({other: z.string()});

Then:

a.isSubset(main) // true, as foo is in main and the same type
b.isSubset(main) // false, as foo is not the same
c.isSubset(main) // false, as other is not present in main

This might get hairy with intersection types, though I'm not particularly concerned with this right now. Is anyone working on anything like this or is there a solution already?

@scotttrinh
Copy link
Collaborator

That's a fun one! I could see something like an extends method that works like a conditional type. Nothing built in that I'm aware of, but would be happy to help point you in the right direction or review a PR to add it.

@scotttrinh
Copy link
Collaborator

I think extends would be the "other way" compared to isSubset, so it'd look more like:

// Runtime equivalent of `Main extends A`
main.extends(a) // true
main.extends(b) // false
main.extends(c) // false

@faassen
Copy link
Author

faassen commented Aug 20, 2021

There's already an extend so extends might be a bit confusing. Perhaps isSuperset would be a good name for the inverse operation.

@scotttrinh
Copy link
Collaborator

There's already an extend so extends might be a bit confusing.

That's fair, but also true of TypeScript itself, and has some nice symmetry. I definitely get that the symmetry is also potentially confusing. Maybe doesExtend to make it clear that it's a predicate? It'd be nice to stick to something close to the compile-time language since we attempt to do that elsewhere.

My only small qualm with using superset/subset in this context, is that it's a potentially confusing term when not dealing with Set or a mathematical set. "Is this schema compatible with another schema?" (for some definition of "compatible") seems like the question we're answering. Maybe if you squint you can say "Is the set of schemas in this definition, a superset or subset of this other set of schemas in another definition?" That seems valid, but maybe a bit non-intuitive?

@faassen
Copy link
Author

faassen commented Aug 20, 2021 via email

@scotttrinh
Copy link
Collaborator

but both z.string and z.number offer a bunch of checkers that need some work to make them comparable.

Oh, that's a good point: if we want it to return false if the schema type has additional refinements, we might have some trouble knowing that for sure. ZodEffect is a bit opaque and might just make this only useful if you have no additional transforms or refinements, which limits the usefulness of this method, IMHO. Definitely still open to helping explore this!

@stale
Copy link

stale bot commented Mar 2, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Mar 2, 2022
@stale stale bot closed this as completed Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants