Open skoshx opened 1 year ago
This is easily achieved by making users of the lib use the .default
function in zod when defining the schemas. That also gives user more flexibility on how they wanna set the default values for these fields:
const User = z.object({
id: z.string().uuid().default(crypto.randomUUID),
created: z
.string()
.datetime()
.default(() => new Date().toISOString()),
});
or if they prefer to use timestamps:
const User = z.object({
id: z.string().uuid().default(crypto.randomUUID),
created: z
.number()
.int()
.default(() => Date.now()),
});
@jnssnmrcs Yeah you're completely right, thus it's marked as good first issue :D The reason we would want some wrapper function is that we would be able to get IntelliSense for instance for flags such as "primary key" and "indexed key" and "unique", because having describe("primary")
isn't obvious if the end-user isn't familiar with how to use Pentagon
This is easily achieved by making users of the lib use the
.default
function in zod when defining the schemas. That also gives user more flexibility on how they wanna set the default values for these fields:const User = z.object({ id: z.string().uuid().default(crypto.randomUUID), created: z .string() .datetime() .default(() => new Date().toISOString()), });
or if they prefer to use timestamps:
const User = z.object({ id: z.string().uuid().default(crypto.randomUUID), created: z .number() .int() .default(() => Date.now()), });
i don't if this is an existing solution or just an idea but as I'm using v0.1.1 so i'm leaving this here. Leem know if it doesn't belong here and i'll create a new issue about it.
I have this user table(as seen below), the enableOptions
field value is undefined
when it's left empty during creation instead of false
.
export const User = z.object({
id: z.number().describe("primary, unique"),
username: z.string().describe("unique").optional(),
firstName: z.string(),
lastName: z.string().optional(),
enableOptions: z.boolean().default(false).optional(),
createdAt: z.date(),
updatedAt: z.date().nullable(),
});
This has been fixed in main branch but I don't think it has been released yet, so in the next release zod default values should work.
This has been fixed in main branch but I don't think it has been released yet, so in the next release zod default values should work.
Okay, thanks!
This is easily achieved by making users of the lib use the
.default
function in zod when defining the schemas. That also gives user more flexibility on how they wanna set the default values for these fields:const User = z.object({ id: z.string().uuid().default(crypto.randomUUID), created: z .string() .datetime() .default(() => new Date().toISOString()), });
or if they prefer to use timestamps:
const User = z.object({ id: z.string().uuid().default(crypto.randomUUID), created: z .number() .int() .default(() => Date.now()), });
I don't know if the latest version implements this feature so i'm not sure if this error i'm getting is related
UPDATE:
I chnaged the code to default(() => crypto.randomUUID())
and it worked perfectly
In the same way as in Prisma we have
createdAt DateTime @default(now())
,we should have in Pentagon something along the lines of: