@@ -8,6 +8,12 @@ import type { StringCase, StringName } from '../../types/case';
8
8
import { numberRegExp } from '../../utils/regexp' ;
9
9
import { createSchemaComment } from '../shared/utils/schema' ;
10
10
import { identifiers , valibotId } from './constants' ;
11
+ import {
12
+ INTEGER_FORMATS ,
13
+ isIntegerFormat ,
14
+ needsBigIntForFormat ,
15
+ numberParameter ,
16
+ } from './number-helpers' ;
11
17
import { operationToValibotSchema } from './operation' ;
12
18
import type { ValibotPlugin } from './types' ;
13
19
@@ -253,99 +259,6 @@ const nullTypeToValibotSchema = (_props: {
253
259
return expression ;
254
260
} ;
255
261
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
-
349
262
const numberTypeToValibotSchema = ( {
350
263
schema,
351
264
} : {
0 commit comments