Repository: samchungy/zod-openapi
Pull request #366 (2024-11-17 01:32:17)

Add support for bigint
I have implemented support for bigint type 😄
The schema is output as integer(int64).

If I am missing any considerations, please point them out🙇🏻‍♂️

samchungy: Could you update the readme otherwise lgtm
mdoi2: Thanks! I had overlooked that.
And I realized that each validation process should be supported like ZodNumber.
I will try to implement it, so please wait.
mdoi2: Oh... I was wondering why only BIgInt is not supported, maybe because metadevpro/openapi3-ts does not support bigint and you dare not support it?
samchungy: > Thanks! I had overlooked that. And I realized that each validation process should be supported like ZodNumber. I will try to implement it, so please wait.

I'm not entirely sure what you mean by that to be honest. The contents of your PR seem fine to me
mdoi2: Thanks for the reply, samchungy.

maximum/minimum/multipleOf in SchemaObject is number type, so bigint cannot be set.
I have come up with the following 2 patterns to deal with this issue without changing the type.

- Exclude maximum/minimum/multipleOf from support and explain it in the README.
- Supports maximum/minimum/multipleOf only in the range of number; if the number exceeds the range, it will be excluded from the output and display warning using console.warn.

Because of the wide range of number, I think the latter will suffice for most of my use cases.
Please give me your opinion on this.
mdoi2: Thanks for your advice🙇🏻‍♂️ certainly agree with your thoughts.
If you want to use min/max, you can take the strategy of using `number` instead of `bigint`.
Fortunately, the range of `number` is wider than that of int32.

I have appended it to README.md.
Could you please check if everything is OK?


Patch:
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/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);
 };


Test patch:
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);
+  });
+});