naz6352
naz6352
TTCTheo's Typesafe Cult
Created by Mir on 5/13/2023 in #questions
Zod conditional required/optional validation
I used to do it via .refine too but then I discovered that if you do it this way the type of z.infer<typeof schema> is
{
type: "video";
otherNonMatchingKey: File;
video: File;
} | {
type: "image";
folder: File;
otherNonMatchingKey: File;
}
{
type: "video";
otherNonMatchingKey: File;
video: File;
} | {
type: "image";
folder: File;
otherNonMatchingKey: File;
}
which is very nice 🙂
29 replies
TTCTheo's Typesafe Cult
Created by Mir on 5/13/2023 in #questions
Zod conditional required/optional validation
And to include otherNonMatchingKey I've done it before via this sort of pattern
const BaseSchema = z.object({ otherNonMatchingKey: z.custom<File>() })

const VideoSchema = z
.object({ type: z.literal('video'), video: z.custom<File>() })
.merge(BaseSchema);

const ImageSchema = z
.object({ type: z.literal('image'), folder: z.custom<File>() })
.merge(BaseSchema);

const schema = z.discriminatedUnion('type', [
VideoSchema,
ImageSchema,
]);
const BaseSchema = z.object({ otherNonMatchingKey: z.custom<File>() })

const VideoSchema = z
.object({ type: z.literal('video'), video: z.custom<File>() })
.merge(BaseSchema);

const ImageSchema = z
.object({ type: z.literal('image'), folder: z.custom<File>() })
.merge(BaseSchema);

const schema = z.discriminatedUnion('type', [
VideoSchema,
ImageSchema,
]);
29 replies
TTCTheo's Typesafe Cult
Created by Mir on 5/13/2023 in #questions
Zod conditional required/optional validation
There is also z.discriminatedUnion https://zod.dev/?id=discriminated-unions So you could do something like
const schema = z.discriminatedUnion("type", [
z.object({ type: z.literal("image"), image: z.custom<File>() }),
z.object({ type: z.literal("video"), video: z.custom<File>() }),
]);
const schema = z.discriminatedUnion("type", [
z.object({ type: z.literal("image"), image: z.custom<File>() }),
z.object({ type: z.literal("video"), video: z.custom<File>() }),
]);
29 replies
TTCTheo's Typesafe Cult
Created by Froxx on 2/9/2023 in #questions
Clean definition of models using Zod schemas
The way that I handled ids was to make this:
const withId = z.object({ id: z.string().cuid() });
const withId = z.object({ id: z.string().cuid() });
And then attach it to anything that needs an id
const BaseThing = z.object({
property: z.string(),
});

const GetThing = BaseThing.merge(withId);
const BaseThing = z.object({
property: z.string(),
});

const GetThing = BaseThing.merge(withId);
I did something similar with updatedAt / createdAt timestamps, which ended up being very helpful when I changed them from z.date() to z.coerce.date() once that feature came out, didn't have to change it in a dozen places. As for your problem with refines, it is possible to add a ZodEffect as a property in a ZodObject, if that helps you solve some of the issue.
const RefinedThing = BaseThing.refine(...);
const OtherThing = z.object({
property: RefinedThing
});
const RefinedThing = BaseThing.refine(...);
const OtherThing = z.object({
property: RefinedThing
});
6 replies
TTCTheo's Typesafe Cult
Created by nexxel on 11/21/2022 in #questions
jwt vs cookies in next-auth
Found the blog with that flowchart on the prisma-session-store readme if you want to read the rest of it (there's a couple other links mentioned in the readme that are pretty good reads too) https://github.com/kleydon/prisma-session-store
151 replies
TTCTheo's Typesafe Cult
Created by nexxel on 11/21/2022 in #questions
jwt vs cookies in next-auth
A bit late to the party but I found this cool flowchart that describes some problems with using JWT for sessions http://cryto.net/~joepie91/blog/attachments/jwt-flowchart.png
151 replies
TTCTheo's Typesafe Cult
Created by zenith on 10/18/2022 in #questions
Stack suggestion - DB platform for a larger number of smaller projects
You can only have a single project on the free plan, if you want more you need the scaler plan and it's $30/mo per
25 replies