Skip to content

Commit

Permalink
make BigInt result in a string
Browse files Browse the repository at this point in the history
  • Loading branch information
AGalabov committed Apr 5, 2024
1 parent 86905f0 commit 700b86c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 97 deletions.
84 changes: 1 addition & 83 deletions spec/types/bigint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,89 +4,7 @@ import { expectSchema } from '../lib/helpers';
describe('bigint', () => {
it('generates OpenAPI schema for a simple bigint type', () => {
expectSchema([z.bigint().openapi('SimpleBigInt')], {
SimpleBigInt: { type: 'integer', format: 'int64' },
SimpleBigInt: { type: 'string', pattern: `^\d+$` },
});
});

it('supports minimum in open api 3.0.0', () => {
expectSchema([z.bigint().gte(BigInt(0)).openapi('SimpleBigInteger')], {
SimpleBigInteger: { type: 'integer', format: 'int64', minimum: 0 },
});
});

it('supports exclusive minimum in open api 3.0.0', () => {
expectSchema([z.bigint().gt(BigInt(0)).openapi('SimpleBigInteger')], {
SimpleBigInteger: {
type: 'integer',
format: 'int64',
minimum: 0,
exclusiveMinimum: true,
},
});
});

it('supports maximum in open api 3.0.0', () => {
expectSchema([z.bigint().lte(BigInt(0)).openapi('SimpleBigInteger')], {
SimpleBigInteger: { type: 'integer', format: 'int64', maximum: 0 },
});
});

it('supports exclusive maximum in open api 3.0.0', () => {
expectSchema([z.bigint().lt(BigInt(0)).openapi('SimpleBigInteger')], {
SimpleBigInteger: {
type: 'integer',
format: 'int64',
maximum: 0,
exclusiveMaximum: true,
},
});
});

it('supports minimum in open api 3.1.0', () => {
expectSchema(
[z.bigint().gte(BigInt(0)).openapi('SimpleBigInteger')],
{
SimpleBigInteger: { type: 'integer', format: 'int64', minimum: 0 },
},
'3.1.0'
);
});

it('supports exclusive minimum in open api 3.1.0', () => {
expectSchema(
[z.bigint().gt(BigInt(0)).openapi('SimpleBigInteger')],
{
SimpleBigInteger: {
type: 'integer',
format: 'int64',
exclusiveMinimum: 0,
} as never,
},
'3.1.0'
);
});

it('supports maximum in open api 3.1.0', () => {
expectSchema(
[z.bigint().lte(BigInt(0)).openapi('SimpleBigInteger')],
{
SimpleBigInteger: { type: 'integer', format: 'int64', maximum: 0 },
},
'3.1.0'
);
});

it('supports exclusive maximum in open api 3.1.0', () => {
expectSchema(
[z.bigint().lt(BigInt(0)).openapi('SimpleBigInteger')],
{
SimpleBigInteger: {
type: 'integer',
format: 'int64',
exclusiveMaximum: 0,
} as never,
},
'3.1.0'
);
});
});
14 changes: 4 additions & 10 deletions src/transformers/big-int.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { ZodBigInt } from 'zod';
import { GetNumberChecks, MapNullableType } from '../types';
import { MapNullableType } from '../types';

export class BigIntTransformer {
transform(
zodSchema: ZodBigInt,
mapNullableType: MapNullableType,
getNumberChecks: GetNumberChecks
) {
transform(mapNullableType: MapNullableType) {
return {
...mapNullableType('integer'),
...getNumberChecks(zodSchema._def.checks),
format: 'int64',
...mapNullableType('string'),
pattern: `^\d+$`,
};
}
}
6 changes: 2 additions & 4 deletions src/transformers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ export class OpenApiTransformer {
}

if (isZodType(zodSchema, 'ZodBigInt')) {
return this.bigIntTransformer.transform(
zodSchema,
schema => this.versionSpecifics.mapNullableType(schema, isNullable),
_ => this.versionSpecifics.getNumberChecks(_)
return this.bigIntTransformer.transform(schema =>
this.versionSpecifics.mapNullableType(schema, isNullable)
);
}

Expand Down

0 comments on commit 700b86c

Please sign in to comment.