-
-
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
Removing the type guards? #293
Comments
Wrote this little wrapper that seems to do the right thing: function check<T>(schema: ZodSchema<T>, data: unknown): data is T {
const parsed = schema.safeParse(data);
return parsed.success;
} — But still not clear why this can't be how |
Actually it's probably because transformers transform types. For example:
|
Ahh, that makes sense. So not only do we want to avoid the type guard in this situation, we should always use the parsed data directly to avoid accidentally asserting something about a type before it has been transformed. So to build off of your example: const countLength = z.transformer(
z.string(),
z.number(),
(myString) => myString.length
);
const originalString = "foobar";
const parsed = countLength.safeParse(originalString);
if (!parsed.success) {
throw new Error("parsing error! oh no!");
}
parsed.data; // number |
Do you want me to take a swing at adding to the At any rate, the issue is definitely "resolved", so I'll close this now. Thanks again @hixus for helping me to understand! |
Finally I used the same way as @scotttrinh did. It works nicely with Full context here: joeltg/next-rest#1 // zod-check.ts
import { ZodSchema } from "zod"
export function check<T>(schema: ZodSchema<T>) {
function is(data: unknown): data is T {
return schema.safeParse(data).success
}
return { is }
} // pages/api/widgets/[id].ts
...
import { ZodSchema } from "zod"
import { check } from "./zod-check"
const getRequestHeaders = z.object({ accept: z.literal("application/json") })
const getRequestBody = z.void()
export default makeHandler("/api/widgets/[id]", {
GET: {
headers: check(getRequestHeaders).is,
body: check(getRequestBody).is,
exec: async ({ params }) => {
// ... method implementation
return {
headers: { "content-type": "application/json" },
body: { id: params.id, isGizmo: false },
}
},
},
})
|
Saw the note about removing
check
, but if it is simplysafeParse().success
, why can we not implement it that way in the base type? I assume this has already been tried, but if there is a problem with that, it would be helpful to know what the problem is so that I don't accidentally reproduce the issue in my own usage.The text was updated successfully, but these errors were encountered: