@@ -145,9 +145,6 @@ export function transformJSONSchemaToFakerCode(jsonSchema?: OpenAPIV3.SchemaObje
145145
146146 switch ( jsonSchema . type ) {
147147 case 'string' :
148- if ( jsonSchema . pattern && isValidRegExp ( jsonSchema . pattern ) ) {
149- return `faker.helpers.fromRegExp(${ jsonSchema . pattern } )` ;
150- }
151148 return transformStringBasedOnFormat ( jsonSchema , key ) ;
152149 case 'number' :
153150 case 'integer' :
@@ -185,7 +182,7 @@ export function transformJSONSchemaToFakerCode(jsonSchema?: OpenAPIV3.SchemaObje
185182 * See https://json-schema.org/understanding-json-schema/reference/string.html#built-in-formats
186183 */
187184function transformStringBasedOnFormat ( schema : OpenAPIV3 . NonArraySchemaObject , key ?: string ) {
188- const { format, minLength, maxLength } = schema ;
185+ const { format, minLength, maxLength, pattern } = schema ;
189186 if ( format === 'date-time' || key ?. toLowerCase ( ) . endsWith ( '_at' ) ) {
190187 return `faker.date.past()` ;
191188 } else if ( format === 'time' ) {
@@ -228,7 +225,11 @@ function transformStringBasedOnFormat(schema: OpenAPIV3.NonArraySchemaObject, ke
228225 return `faker.string.alpha({ length: { min: ${ minLength } , max: MAX_STRING_LENGTH }})` ;
229226 } else if ( maxLength ) {
230227 return `faker.string.alpha({ length: { min: 0, max: ${ maxLength } }})` ;
231- } else {
232- return `faker.lorem.words()` ;
233228 }
229+
230+ if ( pattern && isValidRegExp ( pattern ) ) {
231+ return `faker.helpers.fromRegExp(${ pattern } )` ;
232+ }
233+
234+ return `faker.lorem.words()` ;
234235}
0 commit comments