rails
rails copied to clipboard
Allow an Input type param to be passed to Generate
Zod has a concept of z.input<> which makes default things optional. It is often equal to z.infer<> but nice to use when it is not.
This adds a second type param Input which is equal to Output (previously T) by default.
The fact that parse is typed with Output reads a little weird but I think correct because it uses that type as the return type, and z.input<typeof x> will return z.infer<typeof x>.
Thanks for the start, but sadly I think the fix is more involved since parse is used both on the read and write path. See, e.g., updateImpl.
Also I think we'd want to add tests to be sure the right values are accepted and rejected, and also figure out the corresponding changes to generate-presence.ts (used by Reflect).
I added tests just to confirm there are no type errors and things default correctly.
I believe the implementation can still use parse because parse can take either z.infer<> or z.input<> and will always generate the same shape response.
As far as generate-presence since this is backwards compatible it should still work. It doesn't take an Input type though, obviously. Looking at it I can probably take a stab at updating it although I don't use reflect so I am a little less confident.
Oh also happy add type tests explicitly. I know its possible but honestly have never done it before, but with the tests I added at least the typechecker complains without my change (or if I delete the input type on generate)
OK I understand now, it was my mistake.
Its a little weird to use a parse function which allows missing optional fields when reading out of storage, since we don't actually expect that to happen (because it would have gotten defaulted on the write path). But I don't have a better solution.