@@ -141,29 +141,13 @@ export const getMockScalar = ({
141
141
) ;
142
142
let numberImports : GeneratorImport [ ] = [ ] ;
143
143
if ( item . enum ) {
144
- // By default the value isn't a reference, so we don't have the object explicitly defined.
145
- // So we have to create an array with the enum values and force them to be a const.
146
- const joinedEnumValues = item . enum . filter ( Boolean ) . join ( ',' ) ;
147
-
148
- let enumValue = `[${ joinedEnumValues } ] as const` ;
149
-
150
- // But if the value is a reference, we can use the object directly via the imports and using Object.values.
151
- if ( item . isRef ) {
152
- enumValue = `Object.values(${ item . name } )` ;
153
- numberImports = [
154
- {
155
- name : item . name ,
156
- values : true ,
157
- ...( ! isRootKey ( context . specKey , context . target )
158
- ? { specKey : context . specKey }
159
- : { } ) ,
160
- } ,
161
- ] ;
162
- }
163
-
164
- value = item . path ?. endsWith ( '[]' )
165
- ? `faker.helpers.arrayElements(${ enumValue } )`
166
- : `faker.helpers.arrayElement(${ enumValue } )` ;
144
+ value = getEnum (
145
+ item ,
146
+ numberImports ,
147
+ context ,
148
+ existingReferencedProperties ,
149
+ 'number' ,
150
+ ) ;
167
151
} else if ( 'const' in item ) {
168
152
value = '' + ( item as SchemaObject31 ) . const ;
169
153
}
@@ -205,7 +189,6 @@ export const getMockScalar = ({
205
189
value,
206
190
enums,
207
191
imports : resolvedImports ,
208
- name,
209
192
} = resolveMockValue ( {
210
193
schema : {
211
194
...item . items ,
@@ -223,32 +206,9 @@ export const getMockScalar = ({
223
206
} ) ;
224
207
225
208
if ( enums ) {
226
- if ( ! isReference ( item . items ) ) {
227
- return {
228
- value,
229
- imports : resolvedImports ,
230
- name : item . name ,
231
- } ;
232
- }
233
-
234
- const enumImp = imports . find (
235
- ( imp ) => name . replace ( '[]' , '' ) === imp . name ,
236
- ) ;
237
- const enumValue = enumImp ?. name || name ;
238
209
return {
239
- value : `faker.helpers.arrayElements(Object.values(${ enumValue } ))` ,
240
- imports : enumImp
241
- ? [
242
- ...resolvedImports ,
243
- {
244
- ...enumImp ,
245
- values : true ,
246
- ...( ! isRootKey ( context . specKey , context . target )
247
- ? { specKey : context . specKey }
248
- : { } ) ,
249
- } ,
250
- ]
251
- : resolvedImports ,
210
+ value,
211
+ imports : resolvedImports ,
252
212
name : item . name ,
253
213
} ;
254
214
}
@@ -272,35 +232,16 @@ export const getMockScalar = ({
272
232
273
233
case 'string' : {
274
234
let value = 'faker.string.alpha(20)' ;
275
- let imports : GeneratorImport [ ] = [ ] ;
235
+ let stringImports : GeneratorImport [ ] = [ ] ;
276
236
277
237
if ( item . enum ) {
278
- // By default the value isn't a reference, so we don't have the object explicitly defined.
279
- // So we have to create an array with the enum values and force them to be a const.
280
- const joindEnumValues = item . enum
281
- . filter ( Boolean )
282
- . map ( ( e ) => escape ( e ) )
283
- . join ( "','" ) ;
284
-
285
- let enumValue = `['${ joindEnumValues } '] as const` ;
286
-
287
- // But if the value is a reference, we can use the object directly via the imports and using Object.values.
288
- if ( item . isRef ) {
289
- enumValue = `Object.values(${ item . name } )` ;
290
- imports = [
291
- {
292
- name : item . name ,
293
- values : true ,
294
- ...( ! isRootKey ( context . specKey , context . target )
295
- ? { specKey : context . specKey }
296
- : { } ) ,
297
- } ,
298
- ] ;
299
- }
300
-
301
- value = item . path ?. endsWith ( '[]' )
302
- ? `faker.helpers.arrayElements(${ enumValue } )`
303
- : `faker.helpers.arrayElement(${ enumValue } )` ;
238
+ value = getEnum (
239
+ item ,
240
+ stringImports ,
241
+ context ,
242
+ existingReferencedProperties ,
243
+ 'string' ,
244
+ ) ;
304
245
} else if ( item . pattern ) {
305
246
value = `faker.helpers.fromRegExp('${ item . pattern } ')` ;
306
247
} else if ( 'const' in item ) {
@@ -311,7 +252,7 @@ export const getMockScalar = ({
311
252
value : getNullable ( value , item . nullable ) ,
312
253
enums : item . enum ,
313
254
name : item . name ,
314
- imports,
255
+ imports : stringImports ,
315
256
} ;
316
257
}
317
258
@@ -355,3 +296,62 @@ function getItemType(item: MockSchemaObject) {
355
296
if ( ! type ) return ;
356
297
return [ 'string' , 'number' ] . includes ( type ) ? type : undefined ;
357
298
}
299
+
300
+ const getEnum = (
301
+ item : MockSchemaObject ,
302
+ imports : GeneratorImport [ ] ,
303
+ context : ContextSpecs ,
304
+ existingReferencedProperties : string [ ] ,
305
+ type : 'string' | 'number' ,
306
+ ) => {
307
+ if ( ! item . enum ) return '' ;
308
+ const joindEnumValues =
309
+ type === 'string'
310
+ ? `'${ item . enum
311
+ . filter ( ( e ) => e !== null )
312
+ . map ( ( e ) => escape ( e ) )
313
+ . join ( "','" ) } '`
314
+ : item . enum . filter ( ( e ) => e !== null ) . join ( ',' ) ;
315
+
316
+ let enumValue = `[${ joindEnumValues } ]` ;
317
+ if ( context . output . override . useNativeEnums ) {
318
+ if ( item . isRef || existingReferencedProperties . length === 0 ) {
319
+ enumValue += ` as ${ item . name } ${ item . name . endsWith ( '[]' ) ? '' : '[]' } ` ;
320
+ imports . push ( {
321
+ name : item . name ,
322
+ ...( ! isRootKey ( context . specKey , context . target )
323
+ ? { specKey : context . specKey }
324
+ : { } ) ,
325
+ } ) ;
326
+ } else {
327
+ enumValue += ` as ${ existingReferencedProperties [ existingReferencedProperties . length - 1 ] } ['${ item . name } ']` ;
328
+ if ( ! item . path ?. endsWith ( '[]' ) ) enumValue += '[]' ;
329
+ imports . push ( {
330
+ name : existingReferencedProperties [
331
+ existingReferencedProperties . length - 1
332
+ ] ,
333
+ ...( ! isRootKey ( context . specKey , context . target )
334
+ ? { specKey : context . specKey }
335
+ : { } ) ,
336
+ } ) ;
337
+ }
338
+ } else {
339
+ enumValue += ' as const' ;
340
+ }
341
+
342
+ // But if the value is a reference, we can use the object directly via the imports and using Object.values.
343
+ if ( item . isRef && type === 'string' ) {
344
+ enumValue = `Object.values(${ item . name } )` ;
345
+ imports . push ( {
346
+ name : item . name ,
347
+ values : true ,
348
+ ...( ! isRootKey ( context . specKey , context . target )
349
+ ? { specKey : context . specKey }
350
+ : { } ) ,
351
+ } ) ;
352
+ }
353
+
354
+ return item . path ?. endsWith ( '[]' )
355
+ ? `faker.helpers.arrayElements(${ enumValue } )`
356
+ : `faker.helpers.arrayElement(${ enumValue } )` ;
357
+ } ;
0 commit comments