Skip to content

Commit

Permalink
fix: correct use of regex
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Mar 8, 2024
1 parent df5da5a commit 405e6f1
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-plants-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kubb/swagger-faker": patch
---

correct use of regex for faker
5 changes: 4 additions & 1 deletion examples/faker/petStore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ components:
description: Description
- enum:
- "NW"
- "NE"
- "NE"
- "SW"
- "SE"
description: Wind direction
Expand Down Expand Up @@ -719,6 +719,9 @@ components:
description: User Status
format: int32
example: 1
nationalityCode:
type: string
pattern: "^[A-Z]{2}$"
xml:
name: user
Tag:
Expand Down
1 change: 1 addition & 0 deletions examples/faker/src/gen/customMocks/createUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function createUser(override: NonNullable<Partial<User>> = {}): NonNullab
'password': faker.internet.password(),
'phone': faker.phone.number(),
'userStatus': faker.number.float({}),
'nationalityCode': faker.helpers.fromRegExp(/^[A-Z]{2}$/),
},
...override,
}
Expand Down
2 changes: 1 addition & 1 deletion examples/faker/src/gen/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './customMocks/index'
export * from './models/index'
export * from './customMocks/index'
4 changes: 4 additions & 0 deletions examples/faker/src/gen/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ export type User = {
* @example 1
*/
userStatus?: number
/**
* @type string | undefined
*/
nationalityCode?: string
}
7 changes: 7 additions & 0 deletions packages/swagger-faker/src/fakerParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
12 changes: 12 additions & 0 deletions packages/swagger-faker/src/fakerParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ export function parseFakerMeta(
return value
}

if (isKeyword(item, fakerKeywords.matches)) {
const options = (item as FakerMetaBase<unknown>).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<unknown>).args)
return `${value}(${options ?? ''})`
Expand Down

0 comments on commit 405e6f1

Please sign in to comment.