Using with NestJS and Zod

I'm trying to use drizzle-zod and nestjs-zod together to create Zod schemas and classes for insert and select. I mostly got it to work, even with Swagger! However, I'm running into a problem with one of the create schemas. My table is defined:
export const addresses = pgTable('addresses', {
id: uuid('id').defaultRandom().primaryKey(),
name: text('name'),
placeId: text('place_id'),
firstLine: text('first_line').notNull(),
secondLine: text('second_line'),
city: text('city').notNull(),
state: text('state').notNull(),
zipCode: text('zip_code').notNull(),
country: text('country').notNull(),
latitude: decimal('latitude', {
precision: 8,
scale: 6,
}).notNull(),
longitude: decimal('longitude', {
precision: 9,
scale: 6,
}).notNull(),
instructions: text('instructions'),
createdAt: timestamp('created_at').defaultNow(),
});
export const addresses = pgTable('addresses', {
id: uuid('id').defaultRandom().primaryKey(),
name: text('name'),
placeId: text('place_id'),
firstLine: text('first_line').notNull(),
secondLine: text('second_line'),
city: text('city').notNull(),
state: text('state').notNull(),
zipCode: text('zip_code').notNull(),
country: text('country').notNull(),
latitude: decimal('latitude', {
precision: 8,
scale: 6,
}).notNull(),
longitude: decimal('longitude', {
precision: 9,
scale: 6,
}).notNull(),
instructions: text('instructions'),
createdAt: timestamp('created_at').defaultNow(),
});
Then I create the Zod schema for inserting
const createAddressSchema = createInsertSchema(addresses);
const createAddressSchema = createInsertSchema(addresses);
And then I turn that into a class
import { createZodDto } from 'nestjs-zod';

export class NewAddress extends createZodDto(createAddressSchema) {}
import { createZodDto } from 'nestjs-zod';

export class NewAddress extends createZodDto(createAddressSchema) {}
However, when I try to do something like
const address = await context
.insert(addresses)
.values({} as NewAddress) // Not actually what I'm doing, just doing it to get the error
.returning({ id: addresses.id });
const address = await context
.insert(addresses)
.values({} as NewAddress) // Not actually what I'm doing, just doing it to get the error
.returning({ id: addresses.id });
I get Property 'firstLine' is optional in type 'NewAddress' but required in type '{ firstLine: string | SQL<unknown> | Placeholder<string, any>;.... Looking at the generated class, every property has ? after it, and I'm not sure if that's a drizzle-zod problem or a nestjs-zod problem. Any information would be awesome, thanks! So far I'm loving using Drizzle
1 Reply
nicolas_rusig
nicolas_rusig2y ago
It seems the issue comes from the default tsconfig.ts of Nestjs. Even normal Zod has this issue on Nestjs. strictNullChecks=false makes z.object() properties optional From the documentation of Zod, the strict mode must be enabled for Zod to work properly and strictNullChecks is part of the strict mode so needs to enable too you should modify your tsconfig.ts to have this { "compilerOptions": { "strictNullChecks": true, "strict": true, } }
Want results from more Discord servers?
Add your server