@@ -119,6 +119,8 @@ function parseString(
119
119
const regexCheck = checks . find ( ( check ) => check . kind === 'regex' ) ;
120
120
if ( regexCheck && 'regex' in regexCheck ) {
121
121
const generator = new randExp ( regexCheck . regex ) ;
122
+ generator . randInt = ( min : number , max : number ) =>
123
+ fakerInstance . datatype . number ( { min, max } ) ;
122
124
const max = checks . find ( ( check ) => check . kind === 'max' ) ;
123
125
if ( max && 'value' in max && typeof max . value === 'number' ) {
124
126
generator . max = max . value ;
@@ -319,30 +321,34 @@ function parseMap(zodRef: z.ZodMap<never>, options?: GenerateMockOptions) {
319
321
return results ;
320
322
}
321
323
322
- function parseEnum ( zodRef : z . ZodEnum < never > | z . ZodNativeEnum < never > ) {
324
+ function parseEnum (
325
+ zodRef : z . ZodEnum < never > | z . ZodNativeEnum < never > ,
326
+ options ?: GenerateMockOptions
327
+ ) {
328
+ const fakerInstance = options ?. faker || faker ;
323
329
const values = zodRef . _def . values as Array < z . infer < typeof zodRef > > ;
324
- const pick = Math . floor ( Math . random ( ) * values . length ) ;
325
- return values [ pick ] ;
330
+ return fakerInstance . helpers . arrayElement ( values ) ;
326
331
}
327
332
328
333
function parseDiscriminatedUnion (
329
334
zodRef : z . ZodDiscriminatedUnion < never , any > ,
330
335
options ?: GenerateMockOptions
331
336
) {
337
+ const fakerInstance = options ?. faker || faker ;
332
338
// Map the options to various possible union cases
333
339
const potentialCases = [ ...zodRef . _def . options . values ( ) ] ;
334
- const pick = Math . floor ( Math . random ( ) * potentialCases . length ) ;
335
-
336
- const mocked = potentialCases [ pick ] ;
337
-
340
+ const mocked = fakerInstance . helpers . arrayElement ( potentialCases ) ;
338
341
return generateMock ( mocked , options ) ;
339
342
}
340
343
341
- function parseNativeEnum ( zodRef : z . ZodNativeEnum < never > ) {
344
+ function parseNativeEnum (
345
+ zodRef : z . ZodNativeEnum < never > ,
346
+ options ?: GenerateMockOptions
347
+ ) {
348
+ const fakerInstance = options ?. faker || faker ;
342
349
const { values } = zodRef . _def ;
343
- const pick = Math . floor ( Math . random ( ) * Object . values ( values ) . length ) ;
344
- const key = Array . from ( Object . keys ( values ) ) [ pick ] ;
345
- return values [ values [ key ] ] ;
350
+ const value = fakerInstance . helpers . arrayElement ( values ) ;
351
+ return values [ value ] ;
346
352
}
347
353
348
354
function parseLiteral ( zodRef : z . ZodLiteral < any > ) {
@@ -369,10 +375,9 @@ function parseUnion(
369
375
) {
370
376
const fakerInstance = options ?. faker || faker ;
371
377
// Map the options to various possible mock values
372
- const mockOptions = zodRef . _def . options . map ( ( option ) =>
373
- generateMock ( option , options )
374
- ) ;
375
- return fakerInstance . helpers . arrayElement ( mockOptions ) ;
378
+ const potentialCases = [ ...zodRef . _def . options . values ( ) ] ;
379
+ const mocked = fakerInstance . helpers . arrayElement ( potentialCases ) ;
380
+ return generateMock ( mocked , options ) ;
376
381
}
377
382
378
383
function parseZodIntersection (
@@ -509,7 +514,7 @@ export interface GenerateMockOptions {
509
514
throwOnUnknownType ?: boolean ;
510
515
511
516
/**
512
- * Set a seed for random generation within the mock library
517
+ * Set a seed for random generation
513
518
*/
514
519
seed ?: number | number [ ] ;
515
520
0 commit comments