git-zodyac / mongoose

Converts your Zod schemas into fully-functional Mongoose schemas
https://www.npmjs.com/package/@zodyac/zod-mongoose
31 stars 5 forks source link

[Question] How do you handle unique fields ? #2

Closed AlexandreBrayer closed 3 months ago

AlexandreBrayer commented 9 months ago

I wonder how to handle unique fields. Do you have to delcare them after calling the function "zodSchema" ?

AlexandreBrayer commented 9 months ago

For more context : I tried const UserSchema = z.object({ username: z.string(), email: z.string(), password: z.string(), role: z.enum(['user', 'admin']), createdAt: z.date().optional(), updatedAt: z.date().optional() }); then const rawSchema = zodSchemaRaw(UserSchema); rawSchema.email.unique = true; rawSchema.username.unique = true;

Property 'unique' does not exist on type '_Field<ZodString> | _Schema<ZodString>'. Property 'unique' does not exist on type '_Field<ZodString>'.ts(2339)

Seems odd, since this code works

AlexandreBrayer commented 9 months ago

nvm i found it const schema = zodSchema(UserSchema);

schema.path('email').unique(true); schema.path('username').unique(true);

bebrasmell commented 9 months ago

Hey Alexandre! Thanks for reaching out.

For right now the only option to add non-zod functionality is to modify the output schema. I'm currently looking into adding some of that similar way to how @samchungy/zod-openapi does.

Anyway, I'll keep this discussion as a feature request for further improvements. Enjoy your day!

Kakha01 commented 3 months ago

@bebrasmell I think this issue is supposed to be closed no?