Skip to content

Commit

Permalink
Add support for bigint (#366)
Browse files Browse the repository at this point in the history
Co-authored-by: Sam Chung <[email protected]>
  • Loading branch information
mdoi2 and samchungy authored Nov 19, 2024
1 parent aad157d commit 60cf31c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/create/schema/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions src/create/schema/parsers/bigint.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
11 changes: 11 additions & 0 deletions src/create/schema/parsers/bigint.ts
Original file line number Diff line number Diff line change
@@ -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',
},
});
5 changes: 5 additions & 0 deletions src/create/schema/parsers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -171,5 +172,9 @@ export const createSchemaSwitch = <
return createSetSchema(zodSchema, state);
}

if (isZodType(zodSchema, 'ZodBigInt')) {
return createBigIntSchema(zodSchema);
}

return createManualTypeSchema(zodSchema, state);
};

0 comments on commit 60cf31c

Please sign in to comment.