diff --git a/.changeset/tricky-plants-rush.md b/.changeset/tricky-plants-rush.md new file mode 100644 index 000000000..516aac23d --- /dev/null +++ b/.changeset/tricky-plants-rush.md @@ -0,0 +1,5 @@ +--- +"@kubb/swagger-faker": patch +--- + +correct use of regex for faker diff --git a/examples/faker/petStore.yaml b/examples/faker/petStore.yaml index 9bce401b4..e81d04f4c 100644 --- a/examples/faker/petStore.yaml +++ b/examples/faker/petStore.yaml @@ -671,7 +671,7 @@ components: description: Description - enum: - "NW" - - "NE" + - "NE" - "SW" - "SE" description: Wind direction @@ -719,6 +719,9 @@ components: description: User Status format: int32 example: 1 + nationalityCode: + type: string + pattern: "^[A-Z]{2}$" xml: name: user Tag: diff --git a/examples/faker/src/gen/customMocks/createUser.ts b/examples/faker/src/gen/customMocks/createUser.ts index 202f67349..297dd91d7 100644 --- a/examples/faker/src/gen/customMocks/createUser.ts +++ b/examples/faker/src/gen/customMocks/createUser.ts @@ -12,6 +12,7 @@ export function createUser(override: NonNullable> = {}): NonNullab 'password': faker.internet.password(), 'phone': faker.phone.number(), 'userStatus': faker.number.float({}), + 'nationalityCode': faker.helpers.fromRegExp(/^[A-Z]{2}$/), }, ...override, } diff --git a/examples/faker/src/gen/index.ts b/examples/faker/src/gen/index.ts index b2ee2d10d..bc16ba481 100644 --- a/examples/faker/src/gen/index.ts +++ b/examples/faker/src/gen/index.ts @@ -1,2 +1,2 @@ -export * from './customMocks/index' export * from './models/index' +export * from './customMocks/index' diff --git a/examples/faker/src/gen/models/User.ts b/examples/faker/src/gen/models/User.ts index 7db228615..546481aaf 100644 --- a/examples/faker/src/gen/models/User.ts +++ b/examples/faker/src/gen/models/User.ts @@ -40,4 +40,8 @@ export type User = { * @example 1 */ userStatus?: number + /** + * @type string | undefined + */ + nationalityCode?: string } diff --git a/packages/swagger-faker/src/fakerParser.test.ts b/packages/swagger-faker/src/fakerParser.test.ts index a3606a4fd..5855175ac 100644 --- a/packages/swagger-faker/src/fakerParser.test.ts +++ b/packages/swagger-faker/src/fakerParser.test.ts @@ -45,6 +45,13 @@ const input = [ }), expected: 'faker.helpers.fromRegExp("*")', }, + { + input: parseFakerMeta({ + keyword: 'matches', + args: '^[A-Z]{2}$', + }), + expected: 'faker.helpers.fromRegExp(/^[A-Z]{2}$/)', + }, { input: parseFakerMeta({ keyword: 'ref', diff --git a/packages/swagger-faker/src/fakerParser.ts b/packages/swagger-faker/src/fakerParser.ts index bd2f57f90..e1715a1c7 100644 --- a/packages/swagger-faker/src/fakerParser.ts +++ b/packages/swagger-faker/src/fakerParser.ts @@ -192,6 +192,18 @@ export function parseFakerMeta( return value } + if (isKeyword(item, fakerKeywords.matches)) { + const options = (item as FakerMetaBase).args as string + let regex + try { + regex = new RegExp(options) + } catch (_e) { + regex = JSON.stringify(options) + } + + return `${value}(${regex ?? ''})` + } + if (item.keyword in mapper) { const options = JSON.stringify((item as FakerMetaBase).args) return `${value}(${options ?? ''})`