Skip to content

Commit

Permalink
Add shorthand syntax to coerce option
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Mario564 committed Dec 26, 2024
1 parent 016000a commit 6201629
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 34 deletions.
10 changes: 5 additions & 5 deletions drizzle-zod/src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion drizzle-zod/src/schema.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ export interface CreateUpdateSchema {

export interface CreateSchemaFactoryOptions {
zodInstance?: any;
coerce?: Partial<Record<'bigint' | 'boolean' | 'date' | 'number' | 'string', true>>;
coerce?: Partial<Record<'bigint' | 'boolean' | 'date' | 'number' | 'string', true>> | true;
}
8 changes: 1 addition & 7 deletions drizzle-zod/tests/mysql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
8 changes: 1 addition & 7 deletions drizzle-zod/tests/pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
8 changes: 1 addition & 7 deletions drizzle-zod/tests/singlestore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
8 changes: 1 addition & 7 deletions drizzle-zod/tests/sqlite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 6201629

Please sign in to comment.