-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
Add support for non optional custom zod types #350
Conversation
This looks okay to me, I'll have another look when I have time to properly suss it out. But would |
I'm using LocalDate from the js-joda lib, just used Date as an example here. |
Hmmm I'm not entirely sure I like the approach of treating a custom as non optional by default given it can potentially return anything. Including undefined which in JavaScript world is also optional |
This solution won't do that because it's still calling zod's it('returns true for a custom undefined without a check', () => {
const schema = z.custom<undefined>();
const result = isOptionalSchema(schema, createOutputState());
expect(result).toEqual({ optional: true });
});
it('returns true for a custom undefined with a check', () => {
const schema = z.custom<undefined>((_: unknown) => false);
const result = isOptionalSchema(schema, createOutputState());
expect(result).toEqual({ optional: false });
}); For ex both of these will pass because zod will call the the |
You could also return something | undefined though in a custom. |
From my understanding (I may be wrong) the type you pass to the custom won't matter. If you don't provide a This is mainly because a For custom of it('returns false for a custom string | undefined without a check', () => {
const schema = z.custom<string | undefined>();
const result = isOptionalSchema(schema, createOutputState());
expect(result).toEqual({ optional: true });
});
it('returns false for a custom string | undefined with a check that returns false for undefined', () => {
const schema = z.custom<string | undefined>(
(toCheck: string | undefined) => toCheck !== undefined,
);
const result = isOptionalSchema(schema, createOutputState());
expect(result).toEqual({ optional: false });
});
it('returns true for a custom string | undefined with a check that returns true for undefined', () => {
const schema = z.custom<string | undefined>(
(toCheck: string | undefined) => toCheck === undefined,
);
const result = isOptionalSchema(schema, createOutputState());
expect(result).toEqual({ optional: true });
}); |
Thank you for your work looking into this! I will take a final look tomorrow 🙂 |
Thanks for this, I might end up re-writing all of the optional checking logic though but I'll keep your test cases |
No description provided.