-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Runtypes-like .guard()
#2413
Comments
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. |
This would be so helpful. This feels very boilerplatey and avoidable with such a feature: const result = schema.safeParse(value);
if (result.success) {
const data = result.data;
// guarded code;
} |
You could just add a little helper function: export const isValid = <Output>(
schema: z.ZodType<Output>,
data: unknown
): data is Output => schema.safeParse(data).success; This allows you to use it as you intend to: const schema = zod.number();
const data = JSON.parse("1");
if (isValid(schema, data)) {
// data is a number here
} |
I was dreaming about it. Unfortunately `zod` does not provide any way to make a custom or branded or third-party schema that can be identified as one programmatically. Developer of `zod` also does not want to make any method to store metadata in schemas, instead, recommends to wrap schemas in some other structures, which is not suitable for the purposes of `express-zod-api`. There are many small inconvenient things in making custom schema classes, that I'd like to replace into native methods, and use `withMeta` wrapper for storing proprietary identifier, so the generators and walkers could still handle it. Related issues: ``` colinhacks/zod#1718 colinhacks/zod#2413 colinhacks/zod#273 colinhacks/zod#71 colinhacks/zod#37 ``` PR I've been waiting for months to merged (programmatically distinguishable branding): ``` colinhacks/zod#2860 ```
It would be really handy to have a type guard function be created for each schema automatically. This works: // zod.d.ts
import 'zod';
declare module 'zod' {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
export interface ZodType<Output = any> {
is(val: unknown): val is Output;
}
}
// in the entrypoint
import { assert } from 'tsafe';
import { z } from 'zod';
// fail loudly if upstream adds a conflicting method
assert(!Object.hasOwn(z.ZodType.prototype, 'is'));
z.ZodType.prototype.is = function (val: unknown): val is z.infer<typeof this> {
return this.safeParse(val).success;
};
// in the app
const zMySchema = z.string()
myArray.filter(zMySchema.is) Could someone who knows what they are doing advise if this is a |
Note that your type guard is checking if const MySchema = z.coerce.string();
assert(MySchema.is(5)); // passes, but shouldn't |
I’ve added type guards in #3862 for cases where @kpozin, your example got me thinking, and I realized coercion should influence the |
I miss this feature from runtypes and i would like to implement it (making issue first, as CONTRIBUTING.md said)
My use case
I am recently got tasked with integrating multiple 3rd-party APIs, and i have chosen following pattern for it
The text was updated successfully, but these errors were encountered: