@schmock/faker 1.9.5 → 1.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,252 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import {
3
+ findBestMapping,
4
+ scoreMatch,
5
+ tokenizeFieldName,
6
+ } from "./field-name-matcher";
7
+
8
+ describe("tokenizeFieldName", () => {
9
+ it("splits camelCase", () => {
10
+ expect(tokenizeFieldName("userFirstName")).toEqual([
11
+ "user",
12
+ "first",
13
+ "name",
14
+ ]);
15
+ });
16
+
17
+ it("splits snake_case", () => {
18
+ expect(tokenizeFieldName("created_at")).toEqual(["created", "at"]);
19
+ });
20
+
21
+ it("splits kebab-case", () => {
22
+ expect(tokenizeFieldName("user-name")).toEqual(["user", "name"]);
23
+ });
24
+
25
+ it("handles consecutive uppercase (HTMLParser)", () => {
26
+ expect(tokenizeFieldName("HTMLParser")).toEqual(["html", "parser"]);
27
+ });
28
+
29
+ it("handles is_active", () => {
30
+ expect(tokenizeFieldName("is_active")).toEqual(["is", "active"]);
31
+ });
32
+
33
+ it("handles single word", () => {
34
+ expect(tokenizeFieldName("email")).toEqual(["email"]);
35
+ });
36
+
37
+ it("handles uppercase single word", () => {
38
+ expect(tokenizeFieldName("UUID")).toEqual(["uuid"]);
39
+ });
40
+
41
+ it("handles mixed formats", () => {
42
+ expect(tokenizeFieldName("userEmail_address")).toEqual([
43
+ "user",
44
+ "email",
45
+ "address",
46
+ ]);
47
+ });
48
+ });
49
+
50
+ describe("scoreMatch", () => {
51
+ it("returns 1.0 for exact match", () => {
52
+ expect(scoreMatch(["email"], ["email"])).toBe(1.0);
53
+ });
54
+
55
+ it("returns 1.0 for exact multi-token match", () => {
56
+ expect(scoreMatch(["first", "name"], ["first_name"])).toBe(1.0);
57
+ });
58
+
59
+ it("returns 0.7 for substring match with low keyword coverage", () => {
60
+ // "email" is 1/3 tokens → low coverage (0.65) but substring match (0.7) wins
61
+ expect(scoreMatch(["user", "email", "address"], ["email"])).toBe(0.7);
62
+ });
63
+
64
+ it("returns 0.9 when all keyword tokens found with high coverage", () => {
65
+ // ["created", "at"] are both found in ["user", "created", "at"] → 2/3 coverage > 50%
66
+ expect(scoreMatch(["user", "created", "at"], ["created_at"])).toBe(0.9);
67
+ });
68
+
69
+ it("returns 0.8 when field ends with keyword", () => {
70
+ // ["name"] is at the end of ["display", "name"] — ends-with score = 0.8
71
+ // coverage is 1/2 = 0.5, not > 0.5, so "all found" gives 0.65
72
+ // ends-with wins at 0.8
73
+ expect(scoreMatch(["display", "name"], ["name"])).toBe(0.8);
74
+ });
75
+
76
+ it("returns 0.7 for substring match", () => {
77
+ expect(scoreMatch(["myemailfield"], ["email"])).toBe(0.7);
78
+ });
79
+
80
+ it("returns 0 for no match", () => {
81
+ expect(scoreMatch(["foo", "bar"], ["email"])).toBe(0);
82
+ });
83
+ });
84
+
85
+ describe("findBestMapping", () => {
86
+ it("maps email field", () => {
87
+ const result = findBestMapping("email", { type: "string" });
88
+ expect(result).toBeDefined();
89
+ expect(result?.mapping.fakerMethod).toBe("internet.email");
90
+ });
91
+
92
+ it("maps userEmail field", () => {
93
+ const result = findBestMapping("userEmail", { type: "string" });
94
+ expect(result).toBeDefined();
95
+ expect(result?.mapping.fakerMethod).toBe("internet.email");
96
+ });
97
+
98
+ it("maps firstName field", () => {
99
+ const result = findBestMapping("firstName", { type: "string" });
100
+ expect(result).toBeDefined();
101
+ expect(result?.mapping.fakerMethod).toBe("person.firstName");
102
+ });
103
+
104
+ it("maps first_name field", () => {
105
+ const result = findBestMapping("first_name", { type: "string" });
106
+ expect(result).toBeDefined();
107
+ expect(result?.mapping.fakerMethod).toBe("person.firstName");
108
+ });
109
+
110
+ it("maps createdAt to date.recent", () => {
111
+ const result = findBestMapping("createdAt", { type: "string" });
112
+ expect(result).toBeDefined();
113
+ expect(result?.mapping.fakerMethod).toBe("date.recent");
114
+ expect(result?.mapping.format).toBe("date-time");
115
+ });
116
+
117
+ it("maps city field", () => {
118
+ const result = findBestMapping("city", { type: "string" });
119
+ expect(result).toBeDefined();
120
+ expect(result?.mapping.fakerMethod).toBe("location.city");
121
+ });
122
+
123
+ it("maps url field", () => {
124
+ const result = findBestMapping("url", { type: "string" });
125
+ expect(result).toBeDefined();
126
+ expect(result?.mapping.fakerMethod).toBe("internet.url");
127
+ });
128
+
129
+ it("maps avatar field", () => {
130
+ const result = findBestMapping("avatar", { type: "string" });
131
+ expect(result).toBeDefined();
132
+ expect(result?.mapping.fakerMethod).toBe("image.avatar");
133
+ });
134
+
135
+ it("maps latitude field to number", () => {
136
+ const result = findBestMapping("latitude", { type: "number" });
137
+ expect(result).toBeDefined();
138
+ expect(result?.mapping.fakerMethod).toBe("location.latitude");
139
+ });
140
+
141
+ it("maps description field", () => {
142
+ const result = findBestMapping("description", { type: "string" });
143
+ expect(result).toBeDefined();
144
+ expect(result?.mapping.fakerMethod).toBe("lorem.paragraph");
145
+ });
146
+
147
+ it("maps title field", () => {
148
+ const result = findBestMapping("title", { type: "string" });
149
+ expect(result).toBeDefined();
150
+ expect(result?.mapping.fakerMethod).toBe("lorem.sentence");
151
+ });
152
+
153
+ it("maps country field", () => {
154
+ const result = findBestMapping("country", { type: "string" });
155
+ expect(result).toBeDefined();
156
+ expect(result?.mapping.fakerMethod).toBe("location.country");
157
+ });
158
+
159
+ it("maps isActive boolean with probability", () => {
160
+ const result = findBestMapping("isActive", { type: "boolean" });
161
+ expect(result).toBeDefined();
162
+ expect(result?.mapping.trueProbability).toBe(0.9);
163
+ });
164
+
165
+ it("maps is_deleted boolean with low probability", () => {
166
+ const result = findBestMapping("is_deleted", { type: "boolean" });
167
+ expect(result).toBeDefined();
168
+ expect(result?.mapping.trueProbability).toBe(0.05);
169
+ });
170
+
171
+ describe("ID suffix detection", () => {
172
+ it("maps userId to UUID", () => {
173
+ const result = findBestMapping("userId", { type: "string" });
174
+ expect(result).toBeDefined();
175
+ expect(result?.mapping.fakerMethod).toBe("string.uuid");
176
+ });
177
+
178
+ it("maps order_id to UUID", () => {
179
+ const result = findBestMapping("order_id", { type: "string" });
180
+ expect(result).toBeDefined();
181
+ expect(result?.mapping.fakerMethod).toBe("string.uuid");
182
+ });
183
+
184
+ it("maps parent_id to UUID", () => {
185
+ const result = findBestMapping("parent_id", { type: "string" });
186
+ expect(result).toBeDefined();
187
+ expect(result?.mapping.fakerMethod).toBe("string.uuid");
188
+ });
189
+
190
+ it("does not map single id without format:uuid", () => {
191
+ const result = findBestMapping("id", { type: "string" });
192
+ // 'id' alone shouldn't trigger UUID — it's a single token so suffix rule doesn't apply
193
+ // But it could still match something else. Let's just check it doesn't falsely match
194
+ if (result) {
195
+ // Could match 'id' in some mapping, that's OK
196
+ expect(result.score).toBeGreaterThan(0);
197
+ }
198
+ });
199
+
200
+ it("does not map Id suffix on number fields", () => {
201
+ const result = findBestMapping("userId", { type: "number" });
202
+ // Number type should not get UUID mapping
203
+ if (result) {
204
+ expect(result.mapping.fakerMethod).not.toBe("string.uuid");
205
+ }
206
+ });
207
+ });
208
+
209
+ describe("skip conditions", () => {
210
+ it("skips when schema has pattern", () => {
211
+ const result = findBestMapping("email", {
212
+ type: "string",
213
+ pattern: "^[a-z]+$",
214
+ });
215
+ expect(result).toBeUndefined();
216
+ });
217
+
218
+ it("skips when schema has enum", () => {
219
+ const result = findBestMapping("email", {
220
+ type: "string",
221
+ enum: ["a@b.com", "c@d.com"],
222
+ });
223
+ expect(result).toBeUndefined();
224
+ });
225
+
226
+ it("skips when schema already has faker", () => {
227
+ const schema = { type: "string" as const, faker: "lorem.word" } as any;
228
+ const result = findBestMapping("email", schema);
229
+ expect(result).toBeUndefined();
230
+ });
231
+ });
232
+
233
+ it("does not map unrecognized fields", () => {
234
+ const result = findBestMapping("randomFieldXYZ123", { type: "string" });
235
+ expect(result).toBeUndefined();
236
+ });
237
+
238
+ it("respects type constraints", () => {
239
+ // latitude mapping requires number type
240
+ const result = findBestMapping("latitude", { type: "string" });
241
+ expect(result).toBeUndefined();
242
+ });
243
+
244
+ it("maps format:uuid even without name match", () => {
245
+ const result = findBestMapping("someField", {
246
+ type: "string",
247
+ format: "uuid",
248
+ });
249
+ expect(result).toBeDefined();
250
+ expect(result?.mapping.fakerMethod).toBe("string.uuid");
251
+ });
252
+ });
@@ -0,0 +1,172 @@
1
+ import type { JSONSchema7 } from "json-schema";
2
+ import type { FieldMapping } from "./field-mappings.js";
3
+ import { ALL_FIELD_MAPPINGS } from "./field-mappings.js";
4
+
5
+ /**
6
+ * Split a field name (camelCase, snake_case, kebab-case) into lowercase tokens.
7
+ * "userFirstName" → ["user", "first", "name"]
8
+ * "created_at" → ["created", "at"]
9
+ * "HTMLParser" → ["html", "parser"]
10
+ * "is_active" → ["is", "active"]
11
+ */
12
+ export function tokenizeFieldName(name: string): string[] {
13
+ // Split on _ and -
14
+ const parts = name.split(/[_-]/);
15
+ const tokens: string[] = [];
16
+
17
+ for (const part of parts) {
18
+ if (!part) continue;
19
+ // Split camelCase and consecutive uppercase (e.g., HTMLParser → HTML, Parser)
20
+ const camelTokens = part
21
+ .replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2")
22
+ .replace(/([a-z0-9])([A-Z])/g, "$1_$2")
23
+ .split("_");
24
+
25
+ for (const t of camelTokens) {
26
+ if (t) tokens.push(t.toLowerCase());
27
+ }
28
+ }
29
+
30
+ return tokens;
31
+ }
32
+
33
+ /**
34
+ * Score how well a set of keyword tokens matches field name tokens.
35
+ * Returns 0-1:
36
+ * 1.0 — exact full match (joined tokens equal joined keywords)
37
+ * 0.9 — all keyword tokens found in field tokens with high coverage (>50%)
38
+ * 0.8 — field ends with keyword tokens
39
+ * 0.7 — substring match (keyword appears in joined field name)
40
+ * 0.65 — all keyword tokens found but low coverage (<=50%)
41
+ */
42
+ export function scoreMatch(fieldTokens: string[], keywords: string[]): number {
43
+ const fieldJoined = fieldTokens.join("");
44
+
45
+ let bestScore = 0;
46
+
47
+ // Try each keyword variant, keep the highest score
48
+ for (const keyword of keywords) {
49
+ const kwTokens = tokenizeFieldName(keyword);
50
+ const kwJoined = kwTokens.join("");
51
+
52
+ // Exact match: joined tokens are identical
53
+ if (fieldJoined === kwJoined) return 1.0;
54
+
55
+ // All keyword tokens present in field tokens with high coverage
56
+ if (
57
+ kwTokens.length > 0 &&
58
+ kwTokens.every((kt) => fieldTokens.includes(kt))
59
+ ) {
60
+ const coverage = kwTokens.length / fieldTokens.length;
61
+ const score = coverage > 0.5 ? 0.9 : 0.65;
62
+ bestScore = Math.max(bestScore, score);
63
+ }
64
+
65
+ // Field ends with keyword tokens
66
+ if (kwTokens.length > 0 && kwTokens.length <= fieldTokens.length) {
67
+ const tail = fieldTokens.slice(-kwTokens.length);
68
+ if (tail.every((t, i) => t === kwTokens[i])) {
69
+ bestScore = Math.max(bestScore, 0.8);
70
+ }
71
+ }
72
+
73
+ // Substring match: keyword joined appears in field joined
74
+ if (kwJoined.length >= 3 && fieldJoined.includes(kwJoined)) {
75
+ bestScore = Math.max(bestScore, 0.7);
76
+ }
77
+ }
78
+
79
+ return bestScore;
80
+ }
81
+
82
+ interface MatchResult {
83
+ mapping: FieldMapping;
84
+ score: number;
85
+ }
86
+
87
+ /**
88
+ * Find the best field mapping for a given field name and schema.
89
+ * Returns the highest-scoring match above its threshold, or undefined.
90
+ */
91
+ export function findBestMapping(
92
+ fieldName: string,
93
+ schema: JSONSchema7,
94
+ mappings: FieldMapping[] = ALL_FIELD_MAPPINGS,
95
+ ): MatchResult | undefined {
96
+ const schemaAny = schema as Record<string, unknown>;
97
+ const schemaType = typeof schema.type === "string" ? schema.type : undefined;
98
+
99
+ // Priority: format:uuid always maps to string.uuid
100
+ if (schemaType === "string" && schema.format === "uuid") {
101
+ return {
102
+ mapping: {
103
+ keywords: ["uuid"],
104
+ fakerMethod: "string.uuid",
105
+ schemaType: "string",
106
+ minScore: 0.5,
107
+ },
108
+ score: 1.0,
109
+ };
110
+ }
111
+
112
+ // Skip if schema already has pattern, enum, or faker constraint
113
+ if (schemaAny.pattern || schemaAny.enum || schemaAny.faker) {
114
+ return undefined;
115
+ }
116
+
117
+ // Skip numeric faker mapping when schema already constrains the range
118
+ const hasNumericConstraints =
119
+ schema.minimum !== undefined ||
120
+ schema.maximum !== undefined ||
121
+ schema.exclusiveMinimum !== undefined ||
122
+ schema.exclusiveMaximum !== undefined;
123
+
124
+ const tokens = tokenizeFieldName(fieldName);
125
+
126
+ let best: MatchResult | undefined;
127
+
128
+ for (const mapping of mappings) {
129
+ // Skip numeric faker mappings when schema has explicit constraints
130
+ if (
131
+ hasNumericConstraints &&
132
+ (mapping.schemaType === "number" || mapping.schemaType === "integer")
133
+ ) {
134
+ continue;
135
+ }
136
+
137
+ // Check schema type constraint
138
+ if (mapping.schemaType && schemaType && mapping.schemaType !== schemaType) {
139
+ const isNumeric =
140
+ (mapping.schemaType === "number" && schemaType === "integer") ||
141
+ (mapping.schemaType === "integer" && schemaType === "number");
142
+ if (!isNumeric) continue;
143
+ }
144
+
145
+ const score = scoreMatch(tokens, mapping.keywords);
146
+ if (score >= mapping.minScore && (!best || score > best.score)) {
147
+ best = { mapping, score };
148
+ }
149
+ }
150
+
151
+ // ID suffix detection: fields ending in Id/_id with string type → UUID
152
+ // Only if no better match was found or the existing match is weak
153
+ if (schemaType === "string") {
154
+ const lastToken = tokens[tokens.length - 1];
155
+ if (lastToken === "id" && tokens.length > 1) {
156
+ const idScore = 0.8;
157
+ if (!best || best.score < idScore) {
158
+ best = {
159
+ mapping: {
160
+ keywords: ["id"],
161
+ fakerMethod: "string.uuid",
162
+ schemaType: "string",
163
+ minScore: 0.5,
164
+ },
165
+ score: idScore,
166
+ };
167
+ }
168
+ }
169
+ }
170
+
171
+ return best;
172
+ }
package/src/index.ts CHANGED
@@ -6,11 +6,11 @@ import {
6
6
  SchemaValidationError,
7
7
  } from "@schmock/core";
8
8
  import type { JSONSchema7 } from "json-schema";
9
- import { MAX_ARRAY_SIZE } from "./constants.js";
9
+ import { MAX_ARRAY_SIZE, NULLABLE_NULL_PROBABILITY } from "./constants.js";
10
10
  import { getJsf } from "./jsf-config.js";
11
11
  import { applyOverrides, determineArrayCount } from "./overrides.js";
12
12
  import { enhanceSchemaWithSmartMapping } from "./schema-enhancement.js";
13
- import { validateSchema } from "./validation.js";
13
+ import { isJSONSchema7, validateSchema } from "./validation.js";
14
14
 
15
15
  export interface SchemaGenerationContext {
16
16
  schema: JSONSchema7;
@@ -106,12 +106,12 @@ export function generateFromSchema(options: SchemaGenerationContext): any {
106
106
  }
107
107
 
108
108
  const itemSchema = rawItemSchema;
109
+ const enhancedItemSchema = enhanceSchemaWithSmartMapping(itemSchema);
109
110
 
110
111
  generated = [];
111
112
  for (let i = 0; i < itemCount; i++) {
112
- let item = getJsf(seed).generate(
113
- enhanceSchemaWithSmartMapping(itemSchema),
114
- );
113
+ let item = getJsf(seed).generate(enhancedItemSchema);
114
+ item = postProcessGenerated(item, enhancedItemSchema);
115
115
  item = applyOverrides(item, overrides, params, state, query);
116
116
  generated.push(item);
117
117
  }
@@ -119,8 +119,61 @@ export function generateFromSchema(options: SchemaGenerationContext): any {
119
119
  // Handle object schemas
120
120
  const enhancedSchema = enhanceSchemaWithSmartMapping(schema);
121
121
  generated = getJsf(seed).generate(enhancedSchema);
122
+ generated = postProcessGenerated(generated, enhancedSchema);
122
123
  generated = applyOverrides(generated, overrides, params, state, query);
123
124
  }
124
125
 
125
126
  return generated;
126
127
  }
128
+
129
+ /**
130
+ * Post-process generated data to apply nullable probability and boolean weighting.
131
+ * Walks the schema and generated data in parallel, applying:
132
+ * - schmockNullable: ~5% chance of null
133
+ * - schmockTrueProbability: weighted boolean generation
134
+ */
135
+ function postProcessGenerated(data: any, schema: JSONSchema7): any {
136
+ if (
137
+ data === null ||
138
+ data === undefined ||
139
+ !schema ||
140
+ typeof schema !== "object"
141
+ ) {
142
+ return data;
143
+ }
144
+
145
+ const schemaAny = schema as Record<string, unknown>;
146
+
147
+ // Apply nullable probability at this level
148
+ if (schemaAny.schmockNullable === true) {
149
+ if (Math.random() < NULLABLE_NULL_PROBABILITY) {
150
+ return null;
151
+ }
152
+ }
153
+
154
+ // Apply boolean weighting at this level
155
+ if (
156
+ schema.type === "boolean" &&
157
+ typeof schemaAny.schmockTrueProbability === "number"
158
+ ) {
159
+ return Math.random() < schemaAny.schmockTrueProbability;
160
+ }
161
+
162
+ // Recurse into object properties
163
+ if (typeof data === "object" && !Array.isArray(data) && schema.properties) {
164
+ for (const [key, propSchema] of Object.entries(schema.properties)) {
165
+ if (key in data && isJSONSchema7(propSchema)) {
166
+ data[key] = postProcessGenerated(data[key], propSchema);
167
+ }
168
+ }
169
+ }
170
+
171
+ // Recurse into array items
172
+ if (Array.isArray(data) && schema.items && isJSONSchema7(schema.items)) {
173
+ for (let i = 0; i < data.length; i++) {
174
+ data[i] = postProcessGenerated(data[i], schema.items);
175
+ }
176
+ }
177
+
178
+ return data;
179
+ }
package/src/jsf-config.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { en, Faker } from "@faker-js/faker";
1
+ import { base, en, Faker } from "@faker-js/faker";
2
2
  import jsf from "json-schema-faker";
3
3
 
4
4
  /**
@@ -7,7 +7,7 @@ import jsf from "json-schema-faker";
7
7
  * @returns Fresh Faker instance with English locale
8
8
  */
9
9
  export function createFakerInstance(seed?: number) {
10
- const faker = new Faker({ locale: [en] });
10
+ const faker = new Faker({ locale: [en, base] });
11
11
  if (seed !== undefined) {
12
12
  faker.seed(seed);
13
13
  }
@@ -1,9 +1,12 @@
1
1
  import type { JSONSchema7 } from "json-schema";
2
+ import { findBestMapping } from "./field-name-matcher.js";
2
3
  import { isJSONSchema7, validateFakerMethod } from "./validation.js";
3
4
 
4
- /** JSONSchema7 extended with json-schema-faker's `faker` property */
5
+ /** JSONSchema7 extended with json-schema-faker's `faker` property and schmock markers */
5
6
  interface FakerSchema extends JSONSchema7 {
6
7
  faker?: string;
8
+ schmockNullable?: boolean;
9
+ schmockTrueProbability?: number;
7
10
  }
8
11
 
9
12
  export function enhanceSchemaWithSmartMapping(
@@ -13,10 +16,10 @@ export function enhanceSchemaWithSmartMapping(
13
16
  return schema;
14
17
  }
15
18
 
16
- const enhanced = { ...schema };
19
+ const enhanced = { ...schema } as FakerSchema;
17
20
 
18
21
  // Handle object properties
19
- if (enhanced.type === "object" && enhanced.properties) {
22
+ if (enhanced.properties) {
20
23
  enhanced.properties = { ...enhanced.properties };
21
24
 
22
25
  for (const [fieldName, fieldSchema] of Object.entries(
@@ -31,6 +34,38 @@ export function enhanceSchemaWithSmartMapping(
31
34
  }
32
35
  }
33
36
 
37
+ // Recurse into composition keywords
38
+ for (const keyword of ["allOf", "anyOf", "oneOf"] as const) {
39
+ const branches = enhanced[keyword];
40
+ if (Array.isArray(branches)) {
41
+ (enhanced as Record<string, unknown>)[keyword] = branches.map((branch) =>
42
+ isJSONSchema7(branch) ? enhanceSchemaWithSmartMapping(branch) : branch,
43
+ );
44
+ }
45
+ }
46
+
47
+ // Recurse into array items
48
+ if (enhanced.items) {
49
+ if (Array.isArray(enhanced.items)) {
50
+ enhanced.items = enhanced.items.map((item) =>
51
+ isJSONSchema7(item) ? enhanceSchemaWithSmartMapping(item) : item,
52
+ );
53
+ } else if (isJSONSchema7(enhanced.items)) {
54
+ enhanced.items = enhanceSchemaWithSmartMapping(enhanced.items);
55
+ }
56
+ }
57
+
58
+ // Recurse into additionalProperties
59
+ if (
60
+ enhanced.additionalProperties &&
61
+ typeof enhanced.additionalProperties === "object" &&
62
+ !Array.isArray(enhanced.additionalProperties)
63
+ ) {
64
+ enhanced.additionalProperties = enhanceSchemaWithSmartMapping(
65
+ enhanced.additionalProperties as JSONSchema7,
66
+ );
67
+ }
68
+
34
69
  return enhanced;
35
70
  }
36
71
 
@@ -46,60 +81,28 @@ function enhanceFieldSchema(
46
81
  return enhanced;
47
82
  }
48
83
 
49
- // Apply smart field name mapping
50
- const lowerFieldName = fieldName.toLowerCase();
51
-
52
- // Email fields
53
- if (lowerFieldName.includes("email")) {
54
- enhanced.format = "email";
55
- enhanced.faker = "internet.email";
56
- }
57
- // Name fields
58
- else if (lowerFieldName === "firstname" || lowerFieldName === "first_name") {
59
- enhanced.faker = "person.firstName";
60
- } else if (lowerFieldName === "lastname" || lowerFieldName === "last_name") {
61
- enhanced.faker = "person.lastName";
62
- } else if (lowerFieldName === "name" || lowerFieldName === "fullname") {
63
- enhanced.faker = "person.fullName";
84
+ // Recursively enhance nested schemas first
85
+ const hasComposition = enhanced.allOf || enhanced.anyOf || enhanced.oneOf;
86
+ if (enhanced.properties || hasComposition || enhanced.items) {
87
+ const recursed = enhanceSchemaWithSmartMapping(enhanced);
88
+ Object.assign(enhanced, recursed);
64
89
  }
65
- // Phone fields
66
- else if (lowerFieldName.includes("phone") || lowerFieldName === "mobile") {
67
- enhanced.faker = "phone.number";
68
- }
69
- // Address fields
70
- else if (lowerFieldName === "street" || lowerFieldName === "address") {
71
- enhanced.faker = "location.streetAddress";
72
- } else if (lowerFieldName === "city") {
73
- enhanced.faker = "location.city";
74
- } else if (lowerFieldName === "zipcode" || lowerFieldName === "zip") {
75
- enhanced.faker = "location.zipCode";
76
- }
77
- // UUID fields
78
- else if (
79
- lowerFieldName === "uuid" ||
80
- (lowerFieldName === "id" && enhanced.format === "uuid")
81
- ) {
82
- enhanced.faker = "string.uuid";
83
- }
84
- // Date fields
85
- else if (
86
- lowerFieldName.includes("createdat") ||
87
- lowerFieldName.includes("created_at") ||
88
- lowerFieldName.includes("updatedat") ||
89
- lowerFieldName.includes("updated_at")
90
- ) {
91
- enhanced.format = "date-time";
92
- enhanced.faker = "date.recent";
93
- }
94
- // Company fields
95
- else if (lowerFieldName.includes("company")) {
96
- enhanced.faker = "company.name";
97
- } else if (lowerFieldName === "position" || lowerFieldName === "jobtitle") {
98
- enhanced.faker = "person.jobTitle";
90
+
91
+ // Don't apply field-level faker mapping to composition schemas — the branches define their own types
92
+ if (hasComposition) {
93
+ return enhanced;
99
94
  }
100
- // Price/money fields
101
- else if (lowerFieldName === "price" || lowerFieldName === "amount") {
102
- enhanced.faker = "commerce.price";
95
+
96
+ // Apply smart field name mapping via the scoring matcher
97
+ const match = findBestMapping(fieldName, enhanced);
98
+ if (match) {
99
+ enhanced.faker = match.mapping.fakerMethod;
100
+ if (match.mapping.format) {
101
+ enhanced.format = match.mapping.format;
102
+ }
103
+ if (match.mapping.trueProbability !== undefined) {
104
+ enhanced.schmockTrueProbability = match.mapping.trueProbability;
105
+ }
103
106
  }
104
107
 
105
108
  return enhanced;