From 60cf31cdc1eb028406b2041137bfafb42836681f Mon Sep 17 00:00:00 2001 From: Masanori Doizaki Date: Tue, 19 Nov 2024 21:54:39 +0900 Subject: [PATCH] Add support for bigint (#366) Co-authored-by: Sam Chung --- README.md | 2 ++ src/create/schema/index.test.ts | 8 ++++++++ src/create/schema/parsers/bigint.test.ts | 23 +++++++++++++++++++++++ src/create/schema/parsers/bigint.ts | 11 +++++++++++ src/create/schema/parsers/index.ts | 5 +++++ 5 files changed, 49 insertions(+) create mode 100644 src/create/schema/parsers/bigint.test.ts create mode 100644 src/create/schema/parsers/bigint.ts diff --git a/README.md b/README.md index 6430a9c..1494a13 100644 --- a/README.md +++ b/README.md @@ -763,6 +763,8 @@ As an example `z.string().nullable()` will be rendered differently - ZodAny - ZodArray - `minItems`/`maxItems` mapping for `.length()`, `.min()`, `.max()` +- ZodBigInt + - `integer` `type` and `int64` `format` mapping` - ZodBoolean - ZodBranded - ZodCatch diff --git a/src/create/schema/index.test.ts b/src/create/schema/index.test.ts index 7dcd7f4..df23ec2 100644 --- a/src/create/schema/index.test.ts +++ b/src/create/schema/index.test.ts @@ -293,6 +293,12 @@ const expectedZodReadonly: Schema['schema'] = { type: 'string', }; +const zodBigInt = z.bigint(); +const expectedZodBigInt: Schema['schema'] = { + type: 'integer', + format: 'int64', +}; + it('creates an output schema for zodType', () => { expect( createSchema(zodLazyComplex, createOutputState(), ['previous']), @@ -332,6 +338,7 @@ describe('createSchema', () => { ${'ZodBranded'} | ${zodBranded} | ${expectedZodBranded} ${'ZodSet'} | ${zodSet} | ${expectedZodSet} ${'ZodReadonly'} | ${zodReadonly} | ${expectedZodReadonly} + ${'ZodBigInt'} | ${zodBigInt} | ${expectedZodBigInt} `('creates an output schema for $zodType', ({ schema, expected }) => { expect(createSchema(schema, createOutputState(), ['previous'])).toEqual( expected, @@ -370,6 +377,7 @@ describe('createSchema', () => { ${'ZodBranded'} | ${zodBranded} | ${expectedZodBranded} ${'ZodSet'} | ${zodSet} | ${expectedZodSet} ${'ZodReadonly'} | ${zodReadonly} | ${expectedZodReadonly} + ${'ZodBigInt'} | ${zodBigInt} | ${expectedZodBigInt} `('creates an input schema for $zodType', ({ schema, expected }) => { expect(createSchema(schema, createInputState(), ['previous'])).toEqual( expected, diff --git a/src/create/schema/parsers/bigint.test.ts b/src/create/schema/parsers/bigint.test.ts new file mode 100644 index 0000000..7cbd38a --- /dev/null +++ b/src/create/schema/parsers/bigint.test.ts @@ -0,0 +1,23 @@ +import '../../../entries/extend'; +import { z } from 'zod'; + +import type { Schema } from '..'; + +import { createBigIntSchema } from './bigint'; + +describe('createBigIntSchema', () => { + it('creates a int64 schema', () => { + const expected: Schema = { + type: 'schema', + schema: { + type: 'integer', + format: 'int64', + }, + }; + const schema = z.bigint(); + + const result = createBigIntSchema(schema); + + expect(result).toEqual(expected); + }); +}); diff --git a/src/create/schema/parsers/bigint.ts b/src/create/schema/parsers/bigint.ts new file mode 100644 index 0000000..4e68b12 --- /dev/null +++ b/src/create/schema/parsers/bigint.ts @@ -0,0 +1,11 @@ +import type { ZodBigInt } from 'zod'; + +import type { Schema } from '..'; + +export const createBigIntSchema = (_zodBigInt: ZodBigInt): Schema => ({ + type: 'schema', + schema: { + type: 'integer', + format: 'int64', + }, +}); diff --git a/src/create/schema/parsers/index.ts b/src/create/schema/parsers/index.ts index 4715eb6..cd95f85 100644 --- a/src/create/schema/parsers/index.ts +++ b/src/create/schema/parsers/index.ts @@ -4,6 +4,7 @@ import { isZodType } from '../../../zodType'; import type { RefObject, Schema, SchemaState } from '../../schema'; import { createArraySchema } from './array'; +import { createBigIntSchema } from './bigint'; import { createBooleanSchema } from './boolean'; import { createBrandedSchema } from './brand'; import { createCatchSchema } from './catch'; @@ -171,5 +172,9 @@ export const createSchemaSwitch = < return createSetSchema(zodSchema, state); } + if (isZodType(zodSchema, 'ZodBigInt')) { + return createBigIntSchema(zodSchema); + } + return createManualTypeSchema(zodSchema, state); };