Skip to content

Commit c47aec7

Browse files
committed
improve name rules
1 parent 248e883 commit c47aec7

File tree

6 files changed

+30
-3
lines changed

6 files changed

+30
-3
lines changed

packages/openapi-ts-tests/faker/v9/__snapshots__/3.1.x/faker-locale/@faker-js/faker.gen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ export const fakePersonProfile = (options?: Options) => {
369369
last_name: f.person.lastName(),
370370
email: f.internet.email(),
371371
phone: f.phone.number(),
372+
homePhone: f.phone.number(),
372373
age: f.number.int({ min: 1, max: 120 }),
373374
city: f.location.city(),
374375
postalCode: f.location.zipCode(),

packages/openapi-ts-tests/faker/v9/__snapshots__/3.1.x/faker-typed/@faker-js/faker.gen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ export const fakePersonProfile = (options?: Options): PersonProfile => {
370370
last_name: f.person.lastName(),
371371
email: f.internet.email(),
372372
phone: f.phone.number(),
373+
homePhone: f.phone.number(),
373374
age: f.number.int({ min: 1, max: 120 }),
374375
city: f.location.city(),
375376
postalCode: f.location.zipCode(),

packages/openapi-ts-tests/faker/v9/__snapshots__/3.1.x/faker-typed/types.gen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ export type PersonProfile = {
172172
last_name: string;
173173
email: string;
174174
phone: string;
175+
homePhone: string;
175176
age: number;
176177
city: string;
177178
postalCode: string;

packages/openapi-ts-tests/faker/v9/__snapshots__/3.1.x/faker/@faker-js/faker.gen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ export const fakePersonProfile = (options?: Options) => {
368368
last_name: f.person.lastName(),
369369
email: f.internet.email(),
370370
phone: f.phone.number(),
371+
homePhone: f.phone.number(),
371372
age: f.number.int({ min: 1, max: 120 }),
372373
city: f.location.city(),
373374
postalCode: f.location.zipCode(),

packages/openapi-ts/src/plugins/@faker-js/faker/v10/toAst/nameRules.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ const STRING_COMPOUND_RULES: Record<string, NameRule> = {
121121
'user.name': { fakerPath: ['person', 'fullName'] },
122122
};
123123

124+
// ---------------------------------------------------------------------------
125+
// String suffix rules — checked after exact/compound matches.
126+
// Order matters: first match wins. Suffixes are matched against normalized names.
127+
// ---------------------------------------------------------------------------
128+
const STRING_SUFFIX_RULES: Array<{ rule: NameRule; suffix: string }> = [
129+
{ rule: { fakerPath: ['string', 'uuid'] }, suffix: 'id' },
130+
{ rule: { fakerPath: ['internet', 'email'] }, suffix: 'email' },
131+
{ rule: { fakerPath: ['internet', 'url'] }, suffix: 'url' },
132+
{ rule: { fakerPath: ['internet', 'password'] }, suffix: 'password' },
133+
{ rule: { fakerPath: ['internet', 'jwt'] }, suffix: 'token' },
134+
{ rule: { fakerPath: ['phone', 'number'] }, suffix: 'phone' },
135+
];
136+
124137
// ---------------------------------------------------------------------------
125138
// Integer / number name rules (standalone).
126139
// ---------------------------------------------------------------------------
@@ -228,10 +241,17 @@ export function stringNameToExpression(
228241
nameInfo: PropertyNameInfo,
229242
): Expression | undefined {
230243
const rule = lookupRule(nameInfo, STRING_NAME_RULES, STRING_COMPOUND_RULES);
231-
if (!rule) {
232-
return undefined;
244+
if (rule) {
245+
return buildFromRule(ctx, rule);
233246
}
234-
return buildFromRule(ctx, rule);
247+
248+
const normalized = normalizeName(nameInfo.name);
249+
const suffixRule = STRING_SUFFIX_RULES.find((r) => normalized.endsWith(r.suffix));
250+
if (suffixRule) {
251+
return buildFromRule(ctx, suffixRule.rule);
252+
}
253+
254+
return undefined;
235255
}
236256

237257
/**

specs/3.1.x/faker.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ components:
508508
type: string
509509
phone:
510510
type: string
511+
homePhone:
512+
type: string
511513
age:
512514
type: integer
513515
city:
@@ -537,6 +539,7 @@ components:
537539
- last_name
538540
- email
539541
- phone
542+
- homePhone
540543
- age
541544
- city
542545
- postalCode

0 commit comments

Comments
 (0)