Skip to content

Commit d573e06

Browse files
committed
refactor(valibot): extract number helper functions to separate file
1 parent a7df1f2 commit d573e06

File tree

2 files changed

+101
-93
lines changed

2 files changed

+101
-93
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { compiler } from '../../compiler';
2+
3+
// Integer format ranges and properties
4+
export const INTEGER_FORMATS = {
5+
int16: {
6+
max: 32767,
7+
maxError: 'Invalid value: Expected int16 to be <= 2^15-1',
8+
min: -32768,
9+
minError: 'Invalid value: Expected int16 to be >= -2^15',
10+
needsBigInt: false,
11+
},
12+
int32: {
13+
max: 2147483647,
14+
maxError: 'Invalid value: Expected int32 to be <= 2^31-1',
15+
min: -2147483648,
16+
minError: 'Invalid value: Expected int32 to be >= -2^31',
17+
needsBigInt: false,
18+
},
19+
int64: {
20+
max: '9223372036854775807',
21+
maxError: 'Invalid value: Expected int64 to be <= 2^63-1',
22+
min: '-9223372036854775808',
23+
minError: 'Invalid value: Expected int64 to be >= -2^63',
24+
needsBigInt: true,
25+
},
26+
int8: {
27+
max: 127,
28+
maxError: 'Invalid value: Expected int8 to be <= 2^7-1',
29+
min: -128,
30+
minError: 'Invalid value: Expected int8 to be >= -2^7',
31+
needsBigInt: false,
32+
},
33+
uint16: {
34+
max: 65535,
35+
maxError: 'Invalid value: Expected uint16 to be <= 2^16-1',
36+
min: 0,
37+
minError: 'Invalid value: Expected uint16 to be >= 0',
38+
needsBigInt: false,
39+
},
40+
uint32: {
41+
max: 4294967295,
42+
maxError: 'Invalid value: Expected uint32 to be <= 2^32-1',
43+
min: 0,
44+
minError: 'Invalid value: Expected uint32 to be >= 0',
45+
needsBigInt: false,
46+
},
47+
uint64: {
48+
max: '18446744073709551615',
49+
maxError: 'Invalid value: Expected uint64 to be <= 2^64-1',
50+
min: '0',
51+
minError: 'Invalid value: Expected uint64 to be >= 0',
52+
needsBigInt: true,
53+
},
54+
uint8: {
55+
max: 255,
56+
maxError: 'Invalid value: Expected uint8 to be <= 2^8-1',
57+
min: 0,
58+
minError: 'Invalid value: Expected uint8 to be >= 0',
59+
needsBigInt: false,
60+
},
61+
} as const;
62+
63+
export type IntegerFormat = keyof typeof INTEGER_FORMATS;
64+
65+
export const isIntegerFormat = (
66+
format: string | undefined,
67+
): format is IntegerFormat => format !== undefined && format in INTEGER_FORMATS;
68+
69+
export const needsBigIntForFormat = (format: string | undefined): boolean =>
70+
isIntegerFormat(format) && INTEGER_FORMATS[format].needsBigInt;
71+
72+
export const numberParameter = ({
73+
isBigInt,
74+
value,
75+
}: {
76+
isBigInt: boolean;
77+
value: unknown;
78+
}) => {
79+
const expression = compiler.valueToExpression({ value });
80+
81+
if (
82+
isBigInt &&
83+
(typeof value === 'bigint' ||
84+
typeof value === 'number' ||
85+
typeof value === 'string' ||
86+
typeof value === 'boolean')
87+
) {
88+
return compiler.callExpression({
89+
functionName: 'BigInt',
90+
parameters: [expression],
91+
});
92+
}
93+
94+
return expression;
95+
};

packages/openapi-ts/src/plugins/valibot/plugin.ts

Lines changed: 6 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import type { StringCase, StringName } from '../../types/case';
88
import { numberRegExp } from '../../utils/regexp';
99
import { createSchemaComment } from '../shared/utils/schema';
1010
import { identifiers, valibotId } from './constants';
11+
import {
12+
INTEGER_FORMATS,
13+
isIntegerFormat,
14+
needsBigIntForFormat,
15+
numberParameter,
16+
} from './number-helpers';
1117
import { operationToValibotSchema } from './operation';
1218
import type { ValibotPlugin } from './types';
1319

@@ -253,99 +259,6 @@ const nullTypeToValibotSchema = (_props: {
253259
return expression;
254260
};
255261

256-
// Integer format ranges and properties
257-
const INTEGER_FORMATS = {
258-
int16: {
259-
max: 32767,
260-
maxError: 'Invalid value: Expected int16 to be <= 2^15-1',
261-
min: -32768,
262-
minError: 'Invalid value: Expected int16 to be >= -2^15',
263-
needsBigInt: false,
264-
},
265-
int32: {
266-
max: 2147483647,
267-
maxError: 'Invalid value: Expected int32 to be <= 2^31-1',
268-
min: -2147483648,
269-
minError: 'Invalid value: Expected int32 to be >= -2^31',
270-
needsBigInt: false,
271-
},
272-
int64: {
273-
max: '9223372036854775807',
274-
maxError: 'Invalid value: Expected int64 to be <= 2^63-1',
275-
min: '-9223372036854775808',
276-
minError: 'Invalid value: Expected int64 to be >= -2^63',
277-
needsBigInt: true,
278-
},
279-
int8: {
280-
max: 127,
281-
maxError: 'Invalid value: Expected int8 to be <= 2^7-1',
282-
min: -128,
283-
minError: 'Invalid value: Expected int8 to be >= -2^7',
284-
needsBigInt: false,
285-
},
286-
uint16: {
287-
max: 65535,
288-
maxError: 'Invalid value: Expected uint16 to be <= 2^16-1',
289-
min: 0,
290-
minError: 'Invalid value: Expected uint16 to be >= 0',
291-
needsBigInt: false,
292-
},
293-
uint32: {
294-
max: 4294967295,
295-
maxError: 'Invalid value: Expected uint32 to be <= 2^32-1',
296-
min: 0,
297-
minError: 'Invalid value: Expected uint32 to be >= 0',
298-
needsBigInt: false,
299-
},
300-
uint64: {
301-
max: '18446744073709551615',
302-
maxError: 'Invalid value: Expected uint64 to be <= 2^64-1',
303-
min: '0',
304-
minError: 'Invalid value: Expected uint64 to be >= 0',
305-
needsBigInt: true,
306-
},
307-
uint8: {
308-
max: 255,
309-
maxError: 'Invalid value: Expected uint8 to be <= 2^8-1',
310-
min: 0,
311-
minError: 'Invalid value: Expected uint8 to be >= 0',
312-
needsBigInt: false,
313-
},
314-
} as const;
315-
316-
type IntegerFormat = keyof typeof INTEGER_FORMATS;
317-
318-
const isIntegerFormat = (format: string | undefined): format is IntegerFormat =>
319-
format !== undefined && format in INTEGER_FORMATS;
320-
321-
const needsBigIntForFormat = (format: string | undefined): boolean =>
322-
isIntegerFormat(format) && INTEGER_FORMATS[format].needsBigInt;
323-
324-
const numberParameter = ({
325-
isBigInt,
326-
value,
327-
}: {
328-
isBigInt: boolean;
329-
value: unknown;
330-
}) => {
331-
const expression = compiler.valueToExpression({ value });
332-
333-
if (
334-
isBigInt &&
335-
(typeof value === 'bigint' ||
336-
typeof value === 'number' ||
337-
typeof value === 'string' ||
338-
typeof value === 'boolean')
339-
) {
340-
return compiler.callExpression({
341-
functionName: 'BigInt',
342-
parameters: [expression],
343-
});
344-
}
345-
346-
return expression;
347-
};
348-
349262
const numberTypeToValibotSchema = ({
350263
schema,
351264
}: {

0 commit comments

Comments
 (0)