diff --git a/drizzle-zod/src/column.ts b/drizzle-zod/src/column.ts index b6c50b85a..e300ff6d1 100644 --- a/drizzle-zod/src/column.ts +++ b/drizzle-zod/src/column.ts @@ -105,9 +105,9 @@ export function columnToSchema(column: Column, factory: CreateSchemaFactoryOptio } else if (column.dataType === 'bigint') { schema = bigintColumnToSchema(column, z, coerce); } else if (column.dataType === 'boolean') { - schema = coerce.boolean ? z.coerce.boolean() : z.boolean(); + schema = coerce === true || coerce.boolean ? z.coerce.boolean() : z.boolean(); } else if (column.dataType === 'date') { - schema = coerce.date ? z.coerce.date() : z.date(); + schema = coerce === true || coerce.date ? z.coerce.date() : z.date(); } else if (column.dataType === 'string') { schema = stringColumnToSchema(column, z, coerce); } else if (column.dataType === 'json') { @@ -230,7 +230,7 @@ function numberColumnToSchema( max = Number.MAX_SAFE_INTEGER; } - let schema = coerce?.number ? z.coerce.number() : z.number(); + let schema = coerce === true || coerce?.number ? z.coerce.number() : z.number(); schema = schema.min(min).max(max); return integer ? schema.int() : schema; } @@ -244,7 +244,7 @@ function bigintColumnToSchema( const min = unsigned ? 0n : CONSTANTS.INT64_MIN; const max = unsigned ? CONSTANTS.INT64_UNSIGNED_MAX : CONSTANTS.INT64_MAX; - const schema = coerce?.bigint ? z.coerce.bigint() : z.bigint(); + const schema = coerce === true || coerce?.bigint ? z.coerce.bigint() : z.bigint(); return schema.min(min).max(max); } @@ -295,7 +295,7 @@ function stringColumnToSchema( max = column.dimensions; } - let schema = coerce?.string ? z.coerce.string() : z.string(); + let schema = coerce === true || coerce?.string ? z.coerce.string() : z.string(); schema = regex ? schema.regex(regex) : schema; return max && fixed ? schema.length(max) : max ? schema.max(max) : schema; } diff --git a/drizzle-zod/src/schema.types.ts b/drizzle-zod/src/schema.types.ts index 4d67d1d6f..9ec093593 100644 --- a/drizzle-zod/src/schema.types.ts +++ b/drizzle-zod/src/schema.types.ts @@ -49,5 +49,5 @@ export interface CreateUpdateSchema { export interface CreateSchemaFactoryOptions { zodInstance?: any; - coerce?: Partial>; + coerce?: Partial> | true; } diff --git a/drizzle-zod/tests/mysql.test.ts b/drizzle-zod/tests/mysql.test.ts index 18f8b3d50..314631b6e 100644 --- a/drizzle-zod/tests/mysql.test.ts +++ b/drizzle-zod/tests/mysql.test.ts @@ -470,13 +470,7 @@ test('type coercion - all', (t) => { })); const { createSelectSchema } = createSchemaFactory({ - coerce: { - bigint: true, - boolean: true, - date: true, - number: true, - string: true, - }, + coerce: true, }); const result = createSelectSchema(table); const expected = z.object({ diff --git a/drizzle-zod/tests/pg.test.ts b/drizzle-zod/tests/pg.test.ts index 68642f073..4f82afc2d 100644 --- a/drizzle-zod/tests/pg.test.ts +++ b/drizzle-zod/tests/pg.test.ts @@ -516,13 +516,7 @@ test('type coercion - all', (t) => { })); const { createSelectSchema } = createSchemaFactory({ - coerce: { - bigint: true, - boolean: true, - date: true, - number: true, - string: true, - }, + coerce: true, }); const result = createSelectSchema(table); const expected = z.object({ diff --git a/drizzle-zod/tests/singlestore.test.ts b/drizzle-zod/tests/singlestore.test.ts index f4824e67e..cf54ea0c0 100644 --- a/drizzle-zod/tests/singlestore.test.ts +++ b/drizzle-zod/tests/singlestore.test.ts @@ -472,13 +472,7 @@ test('type coercion - all', (t) => { })); const { createSelectSchema } = createSchemaFactory({ - coerce: { - bigint: true, - boolean: true, - date: true, - number: true, - string: true, - }, + coerce: true, }); const result = createSelectSchema(table); const expected = z.object({ diff --git a/drizzle-zod/tests/sqlite.test.ts b/drizzle-zod/tests/sqlite.test.ts index a5a80fb5f..5950f6efe 100644 --- a/drizzle-zod/tests/sqlite.test.ts +++ b/drizzle-zod/tests/sqlite.test.ts @@ -364,13 +364,7 @@ test('type coercion - all', (t) => { })); const { createSelectSchema } = createSchemaFactory({ - coerce: { - bigint: true, - boolean: true, - date: true, - number: true, - string: true, - }, + coerce: true, }); const result = createSelectSchema(table); const expected = z.object({