@schmock/faker 1.4.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.
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +161 -0
- package/dist/test-utils.d.ts +57 -0
- package/dist/test-utils.d.ts.map +1 -0
- package/dist/test-utils.js +255 -0
- package/package.json +45 -0
- package/src/advanced-features.test.ts +911 -0
- package/src/data-quality.test.ts +415 -0
- package/src/error-handling.test.ts +506 -0
- package/src/index.test.ts +1207 -0
- package/src/index.ts +868 -0
- package/src/integration.test.ts +632 -0
- package/src/performance.test.ts +483 -0
- package/src/plugin-integration.test.ts +574 -0
- package/src/real-world.test.ts +636 -0
- package/src/steps/deterministic-seeds.steps.ts +53 -0
- package/src/steps/faker-plugin.steps.ts +160 -0
- package/src/test-utils.ts +345 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,868 @@
|
|
|
1
|
+
import { en, Faker } from "@faker-js/faker";
|
|
2
|
+
import type { Plugin, PluginContext } from "@schmock/core";
|
|
3
|
+
import {
|
|
4
|
+
ResourceLimitError,
|
|
5
|
+
SchemaGenerationError,
|
|
6
|
+
SchemaValidationError,
|
|
7
|
+
} from "@schmock/core";
|
|
8
|
+
import type { JSONSchema7 } from "json-schema";
|
|
9
|
+
import jsf from "json-schema-faker";
|
|
10
|
+
|
|
11
|
+
function isJSONSchema7(value: unknown): value is JSONSchema7 {
|
|
12
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** JSONSchema7 extended with json-schema-faker's `faker` property */
|
|
16
|
+
interface FakerSchema extends JSONSchema7 {
|
|
17
|
+
faker?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Create isolated faker instance to avoid race conditions
|
|
22
|
+
* Each generation gets its own faker instance to ensure thread-safety
|
|
23
|
+
* @returns Fresh Faker instance with English locale
|
|
24
|
+
*/
|
|
25
|
+
function createFakerInstance(seed?: number) {
|
|
26
|
+
const faker = new Faker({ locale: [en] });
|
|
27
|
+
if (seed !== undefined) {
|
|
28
|
+
faker.seed(seed);
|
|
29
|
+
}
|
|
30
|
+
return faker;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let jsfConfigured = false;
|
|
34
|
+
let currentSeed: number | undefined;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Create a seeded PRNG using the mulberry32 algorithm.
|
|
38
|
+
* Returns a function that produces deterministic values in [0, 1).
|
|
39
|
+
*/
|
|
40
|
+
function createSeededRandom(seed: number): () => number {
|
|
41
|
+
let state = seed | 0;
|
|
42
|
+
return () => {
|
|
43
|
+
state = (state + 0x6d2b79f5) | 0;
|
|
44
|
+
let t = state;
|
|
45
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
46
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
47
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getJsf(seed?: number) {
|
|
52
|
+
const seedChanged = seed !== currentSeed;
|
|
53
|
+
if (!jsfConfigured || seedChanged) {
|
|
54
|
+
currentSeed = seed;
|
|
55
|
+
jsf.extend("faker", () => createFakerInstance(seed));
|
|
56
|
+
jsf.option({
|
|
57
|
+
requiredOnly: false,
|
|
58
|
+
alwaysFakeOptionals: true,
|
|
59
|
+
useDefaultValue: true,
|
|
60
|
+
ignoreMissingRefs: true,
|
|
61
|
+
failOnInvalidTypes: false,
|
|
62
|
+
failOnInvalidFormat: false,
|
|
63
|
+
});
|
|
64
|
+
jsfConfigured = true;
|
|
65
|
+
}
|
|
66
|
+
// Always reset PRNG for deterministic output per call
|
|
67
|
+
if (seed !== undefined) {
|
|
68
|
+
jsf.option({ random: createSeededRandom(seed) });
|
|
69
|
+
jsf.extend("faker", () => createFakerInstance(seed));
|
|
70
|
+
}
|
|
71
|
+
return jsf;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Resource limits for safety
|
|
75
|
+
const MAX_ARRAY_SIZE = 10000;
|
|
76
|
+
const MAX_NESTING_DEPTH = 10; // Reasonable limit for schema nesting
|
|
77
|
+
const DEFAULT_ARRAY_COUNT = 3; // Default items to generate when not specified
|
|
78
|
+
const DEEP_NESTING_THRESHOLD = 3; // Depth at which to check for memory risks
|
|
79
|
+
const LARGE_ARRAY_THRESHOLD = 100; // Array size considered "large"
|
|
80
|
+
|
|
81
|
+
export interface SchemaGenerationContext {
|
|
82
|
+
schema: JSONSchema7;
|
|
83
|
+
count?: number;
|
|
84
|
+
overrides?: Record<string, any>;
|
|
85
|
+
params?: Record<string, string>;
|
|
86
|
+
state?: any;
|
|
87
|
+
query?: Record<string, string>;
|
|
88
|
+
seed?: number;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface FakerPluginOptions {
|
|
92
|
+
schema: JSONSchema7;
|
|
93
|
+
count?: number;
|
|
94
|
+
overrides?: Record<string, any>;
|
|
95
|
+
seed?: number;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function fakerPlugin(options: FakerPluginOptions): Plugin {
|
|
99
|
+
// Validate schema immediately when plugin is created
|
|
100
|
+
validateSchema(options.schema);
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
name: "faker",
|
|
104
|
+
version: "1.0.1",
|
|
105
|
+
|
|
106
|
+
process(context: PluginContext, response?: any) {
|
|
107
|
+
// If response already exists, pass it through
|
|
108
|
+
if (response !== undefined && response !== null) {
|
|
109
|
+
return { context, response };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
const generatedResponse = generateFromSchema({
|
|
114
|
+
schema: options.schema,
|
|
115
|
+
count: options.count,
|
|
116
|
+
overrides: options.overrides,
|
|
117
|
+
params: context.params,
|
|
118
|
+
state: context.routeState,
|
|
119
|
+
query: context.query,
|
|
120
|
+
seed: options.seed,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
context,
|
|
125
|
+
response: generatedResponse,
|
|
126
|
+
};
|
|
127
|
+
} catch (error) {
|
|
128
|
+
// Re-throw schema-specific errors as-is
|
|
129
|
+
if (
|
|
130
|
+
error instanceof SchemaValidationError ||
|
|
131
|
+
error instanceof ResourceLimitError
|
|
132
|
+
) {
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Wrap other errors
|
|
137
|
+
throw new SchemaGenerationError(
|
|
138
|
+
context.path,
|
|
139
|
+
error instanceof Error ? error : new Error(String(error)),
|
|
140
|
+
options.schema,
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function generateFromSchema(options: SchemaGenerationContext): any {
|
|
148
|
+
const { schema, count, overrides, params, state, query, seed } = options;
|
|
149
|
+
|
|
150
|
+
// Validate schema
|
|
151
|
+
validateSchema(schema);
|
|
152
|
+
|
|
153
|
+
let generated: any;
|
|
154
|
+
|
|
155
|
+
// Handle array schemas with count
|
|
156
|
+
if (schema.type === "array" && schema.items) {
|
|
157
|
+
const itemCount = determineArrayCount(schema, count);
|
|
158
|
+
|
|
159
|
+
// Check for resource limits
|
|
160
|
+
if (itemCount > MAX_ARRAY_SIZE) {
|
|
161
|
+
throw new ResourceLimitError("array_size", MAX_ARRAY_SIZE, itemCount);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const rawItemSchema = Array.isArray(schema.items)
|
|
165
|
+
? schema.items[0]
|
|
166
|
+
: schema.items;
|
|
167
|
+
|
|
168
|
+
if (!rawItemSchema || typeof rawItemSchema === "boolean") {
|
|
169
|
+
throw new SchemaValidationError(
|
|
170
|
+
"$.items",
|
|
171
|
+
"Array schema must have valid items definition",
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const itemSchema = rawItemSchema;
|
|
176
|
+
|
|
177
|
+
generated = [];
|
|
178
|
+
for (let i = 0; i < itemCount; i++) {
|
|
179
|
+
let item = getJsf(seed).generate(
|
|
180
|
+
enhanceSchemaWithSmartMapping(itemSchema),
|
|
181
|
+
);
|
|
182
|
+
item = applyOverrides(item, overrides, params, state, query);
|
|
183
|
+
generated.push(item);
|
|
184
|
+
}
|
|
185
|
+
} else {
|
|
186
|
+
// Handle object schemas
|
|
187
|
+
const enhancedSchema = enhanceSchemaWithSmartMapping(schema);
|
|
188
|
+
generated = getJsf(seed).generate(enhancedSchema);
|
|
189
|
+
generated = applyOverrides(generated, overrides, params, state, query);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return generated;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Validate JSON Schema structure and enforce resource limits
|
|
197
|
+
* Checks for malformed schemas, circular references, excessive nesting,
|
|
198
|
+
* and dangerous patterns that could cause memory issues
|
|
199
|
+
* @param schema - JSON Schema to validate
|
|
200
|
+
* @param path - Current path in schema tree (for error messages)
|
|
201
|
+
* @throws {SchemaValidationError} When schema structure is invalid
|
|
202
|
+
* @throws {ResourceLimitError} When schema exceeds safety limits
|
|
203
|
+
*/
|
|
204
|
+
function validateSchema(schema: JSONSchema7, path = "$"): void {
|
|
205
|
+
if (!schema || typeof schema !== "object") {
|
|
206
|
+
throw new SchemaValidationError(
|
|
207
|
+
path,
|
|
208
|
+
"Schema must be a valid JSON Schema object",
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (Object.keys(schema).length === 0) {
|
|
213
|
+
throw new SchemaValidationError(path, "Schema cannot be empty");
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Check for invalid schema types
|
|
217
|
+
const validTypes = [
|
|
218
|
+
"object",
|
|
219
|
+
"array",
|
|
220
|
+
"string",
|
|
221
|
+
"number",
|
|
222
|
+
"integer",
|
|
223
|
+
"boolean",
|
|
224
|
+
"null",
|
|
225
|
+
];
|
|
226
|
+
if (
|
|
227
|
+
schema.type &&
|
|
228
|
+
typeof schema.type === "string" &&
|
|
229
|
+
!validTypes.includes(schema.type)
|
|
230
|
+
) {
|
|
231
|
+
throw new SchemaValidationError(
|
|
232
|
+
path,
|
|
233
|
+
`Invalid schema type: "${schema.type}"`,
|
|
234
|
+
"Supported types are: object, array, string, number, integer, boolean, null",
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Check for malformed properties (must be object, not string)
|
|
239
|
+
if (schema.type === "object" && schema.properties) {
|
|
240
|
+
if (
|
|
241
|
+
typeof schema.properties !== "object" ||
|
|
242
|
+
Array.isArray(schema.properties)
|
|
243
|
+
) {
|
|
244
|
+
throw new SchemaValidationError(
|
|
245
|
+
`${path}.properties`,
|
|
246
|
+
"Properties must be an object mapping property names to schemas",
|
|
247
|
+
'Use { "propertyName": { "type": "string" } } format',
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Validate each property recursively
|
|
252
|
+
for (const [propName, propSchema] of Object.entries(schema.properties)) {
|
|
253
|
+
if (typeof propSchema === "object" && propSchema !== null) {
|
|
254
|
+
// Check for invalid faker methods in property schemas
|
|
255
|
+
const fakerProp =
|
|
256
|
+
"faker" in propSchema ? String(propSchema.faker) : undefined;
|
|
257
|
+
if (fakerProp) {
|
|
258
|
+
try {
|
|
259
|
+
validateFakerMethod(fakerProp);
|
|
260
|
+
} catch (error: unknown) {
|
|
261
|
+
// Re-throw with proper path context
|
|
262
|
+
if (error instanceof SchemaValidationError) {
|
|
263
|
+
const ctx = error.context;
|
|
264
|
+
let issue = "Invalid faker method";
|
|
265
|
+
let suggestion: string | undefined;
|
|
266
|
+
if (ctx && typeof ctx === "object") {
|
|
267
|
+
if ("issue" in ctx && typeof ctx.issue === "string")
|
|
268
|
+
issue = ctx.issue;
|
|
269
|
+
if ("suggestion" in ctx && typeof ctx.suggestion === "string")
|
|
270
|
+
suggestion = ctx.suggestion;
|
|
271
|
+
}
|
|
272
|
+
throw new SchemaValidationError(
|
|
273
|
+
`${path}.properties.${propName}.faker`,
|
|
274
|
+
issue,
|
|
275
|
+
suggestion,
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
if (error instanceof Error) throw error;
|
|
279
|
+
throw new Error(String(error));
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
validateSchema(propSchema, `${path}.properties.${propName}`);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// Check for invalid array items
|
|
288
|
+
if (schema.type === "array") {
|
|
289
|
+
// Array must have items defined and non-null
|
|
290
|
+
if (schema.items === null || schema.items === undefined) {
|
|
291
|
+
throw new SchemaValidationError(
|
|
292
|
+
`${path}.items`,
|
|
293
|
+
"Array schema must have valid items definition",
|
|
294
|
+
"Define items as a schema object or array of schemas",
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (Array.isArray(schema.items)) {
|
|
299
|
+
if (schema.items.length === 0) {
|
|
300
|
+
throw new SchemaValidationError(
|
|
301
|
+
`${path}.items`,
|
|
302
|
+
"Array items cannot be empty array",
|
|
303
|
+
"Provide at least one item schema",
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
schema.items.forEach((item, index) => {
|
|
307
|
+
if (typeof item === "object" && item !== null) {
|
|
308
|
+
validateSchema(item, `${path}.items[${index}]`);
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
} else if (typeof schema.items === "object" && schema.items !== null) {
|
|
312
|
+
validateSchema(schema.items, `${path}.items`);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Check for circular references
|
|
317
|
+
if (hasCircularReference(schema)) {
|
|
318
|
+
throw new SchemaValidationError(
|
|
319
|
+
path,
|
|
320
|
+
"Schema contains circular references which are not supported",
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// Check nesting depth
|
|
325
|
+
const depth = calculateNestingDepth(schema);
|
|
326
|
+
if (depth > MAX_NESTING_DEPTH) {
|
|
327
|
+
throw new ResourceLimitError(
|
|
328
|
+
"schema_nesting_depth",
|
|
329
|
+
MAX_NESTING_DEPTH,
|
|
330
|
+
depth,
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Check for dangerous combination of deep nesting + large arrays
|
|
335
|
+
if (depth >= 4) {
|
|
336
|
+
checkForDeepNestingWithArrays(schema, path);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// Check for potentially dangerous array sizes in schema definition
|
|
340
|
+
checkArraySizeLimits(schema, path);
|
|
341
|
+
|
|
342
|
+
// Check for forbidden features
|
|
343
|
+
if (schema.$ref === "#") {
|
|
344
|
+
throw new SchemaValidationError(
|
|
345
|
+
path,
|
|
346
|
+
"Self-referencing schemas are not supported",
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Detect circular references in JSON Schema using path-based traversal
|
|
353
|
+
* Uses backtracking to distinguish between cycles and legitimate schema reuse
|
|
354
|
+
* @param schema - Schema to check for cycles
|
|
355
|
+
* @param currentPath - Set of schemas currently in traversal path
|
|
356
|
+
* @returns true if circular reference detected, false otherwise
|
|
357
|
+
* @example
|
|
358
|
+
* // Detects: schema A -> B -> A (cycle)
|
|
359
|
+
* // Allows: schema A -> B, A -> C (reuse of A)
|
|
360
|
+
*/
|
|
361
|
+
function hasCircularReference(
|
|
362
|
+
schema: JSONSchema7,
|
|
363
|
+
currentPath = new Set(),
|
|
364
|
+
): boolean {
|
|
365
|
+
// Check if this schema is currently being traversed (cycle detected)
|
|
366
|
+
if (currentPath.has(schema)) {
|
|
367
|
+
return true;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (schema.$ref === "#") {
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// Add to current path for this traversal branch
|
|
375
|
+
currentPath.add(schema);
|
|
376
|
+
|
|
377
|
+
if (schema.type === "object" && schema.properties) {
|
|
378
|
+
for (const prop of Object.values(schema.properties)) {
|
|
379
|
+
if (isJSONSchema7(prop)) {
|
|
380
|
+
if (hasCircularReference(prop, currentPath)) {
|
|
381
|
+
return true;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
if (schema.type === "array" && schema.items) {
|
|
388
|
+
const items = Array.isArray(schema.items) ? schema.items : [schema.items];
|
|
389
|
+
for (const item of items) {
|
|
390
|
+
if (isJSONSchema7(item)) {
|
|
391
|
+
if (hasCircularReference(item, currentPath)) {
|
|
392
|
+
return true;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// Remove from current path after checking all children (backtrack)
|
|
399
|
+
currentPath.delete(schema);
|
|
400
|
+
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Calculate maximum nesting depth of a JSON Schema
|
|
406
|
+
* Recursively traverses object properties and array items
|
|
407
|
+
* @param schema - Schema to measure
|
|
408
|
+
* @param depth - Current depth (internal recursion parameter)
|
|
409
|
+
* @returns Maximum nesting depth found
|
|
410
|
+
*/
|
|
411
|
+
function calculateNestingDepth(schema: JSONSchema7, depth = 0): number {
|
|
412
|
+
if (depth > MAX_NESTING_DEPTH) {
|
|
413
|
+
return depth;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
let maxDepth = depth;
|
|
417
|
+
|
|
418
|
+
if (schema.type === "object" && schema.properties) {
|
|
419
|
+
for (const prop of Object.values(schema.properties)) {
|
|
420
|
+
if (isJSONSchema7(prop)) {
|
|
421
|
+
maxDepth = Math.max(maxDepth, calculateNestingDepth(prop, depth + 1));
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (schema.type === "array" && schema.items) {
|
|
427
|
+
const items = Array.isArray(schema.items) ? schema.items : [schema.items];
|
|
428
|
+
for (const item of items) {
|
|
429
|
+
if (isJSONSchema7(item)) {
|
|
430
|
+
maxDepth = Math.max(maxDepth, calculateNestingDepth(item, depth + 1));
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
return maxDepth;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Check for dangerous patterns of deep nesting combined with large arrays
|
|
440
|
+
* Prevents memory issues from schemas like: depth 3+ with 100+ item arrays
|
|
441
|
+
* @param schema - Schema to check
|
|
442
|
+
* @param _path - Path in schema (unused but kept for signature consistency)
|
|
443
|
+
* @throws {ResourceLimitError} When dangerous nesting pattern detected
|
|
444
|
+
*/
|
|
445
|
+
function checkForDeepNestingWithArrays(
|
|
446
|
+
schema: JSONSchema7,
|
|
447
|
+
_path: string,
|
|
448
|
+
): void {
|
|
449
|
+
// Look for arrays in deeply nested structures that could cause memory issues
|
|
450
|
+
function findArraysInDeepNesting(
|
|
451
|
+
node: JSONSchema7,
|
|
452
|
+
currentDepth: number,
|
|
453
|
+
): boolean {
|
|
454
|
+
const schemaType = node.type;
|
|
455
|
+
const isArray = Array.isArray(schemaType)
|
|
456
|
+
? schemaType.includes("array")
|
|
457
|
+
: schemaType === "array";
|
|
458
|
+
|
|
459
|
+
if (isArray) {
|
|
460
|
+
const maxItems = node.maxItems || DEFAULT_ARRAY_COUNT;
|
|
461
|
+
// Be more aggressive about deep nesting detection
|
|
462
|
+
if (
|
|
463
|
+
currentDepth >= DEEP_NESTING_THRESHOLD &&
|
|
464
|
+
maxItems >= LARGE_ARRAY_THRESHOLD
|
|
465
|
+
) {
|
|
466
|
+
throw new ResourceLimitError(
|
|
467
|
+
"deep_nesting_memory_risk",
|
|
468
|
+
DEEP_NESTING_THRESHOLD * LARGE_ARRAY_THRESHOLD,
|
|
469
|
+
currentDepth * maxItems,
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// Check items if they exist
|
|
474
|
+
if (node.items) {
|
|
475
|
+
const items = Array.isArray(node.items) ? node.items : [node.items];
|
|
476
|
+
for (const item of items) {
|
|
477
|
+
if (isJSONSchema7(item)) {
|
|
478
|
+
if (findArraysInDeepNesting(item, currentDepth + 1)) {
|
|
479
|
+
return true;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
return true;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if (schemaType === "object" && node.properties) {
|
|
489
|
+
for (const prop of Object.values(node.properties)) {
|
|
490
|
+
if (isJSONSchema7(prop)) {
|
|
491
|
+
if (findArraysInDeepNesting(prop, currentDepth + 1)) {
|
|
492
|
+
return true;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
return false;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
findArraysInDeepNesting(schema, 0);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function checkArraySizeLimits(schema: JSONSchema7, path: string): void {
|
|
505
|
+
// Recursively check all array constraints in the schema
|
|
506
|
+
if (schema.type === "array") {
|
|
507
|
+
// Check for dangerously large maxItems
|
|
508
|
+
if (schema.maxItems && schema.maxItems > MAX_ARRAY_SIZE) {
|
|
509
|
+
throw new ResourceLimitError(
|
|
510
|
+
"array_max_items",
|
|
511
|
+
MAX_ARRAY_SIZE,
|
|
512
|
+
schema.maxItems,
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// Check for combination of deep nesting and large arrays
|
|
517
|
+
const depth = calculateNestingDepth(schema);
|
|
518
|
+
const estimatedSize =
|
|
519
|
+
schema.maxItems || schema.minItems || DEFAULT_ARRAY_COUNT;
|
|
520
|
+
|
|
521
|
+
// If we have deep nesting and large arrays, it could cause memory issues
|
|
522
|
+
if (
|
|
523
|
+
depth > DEEP_NESTING_THRESHOLD &&
|
|
524
|
+
estimatedSize > LARGE_ARRAY_THRESHOLD
|
|
525
|
+
) {
|
|
526
|
+
throw new ResourceLimitError(
|
|
527
|
+
"memory_estimation",
|
|
528
|
+
DEEP_NESTING_THRESHOLD * LARGE_ARRAY_THRESHOLD,
|
|
529
|
+
depth * estimatedSize,
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// Recursively check nested schemas
|
|
535
|
+
if (schema.type === "object" && schema.properties) {
|
|
536
|
+
for (const [propName, propSchema] of Object.entries(schema.properties)) {
|
|
537
|
+
if (isJSONSchema7(propSchema)) {
|
|
538
|
+
checkArraySizeLimits(propSchema, `${path}.properties.${propName}`);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
if (schema.type === "array" && schema.items) {
|
|
544
|
+
if (Array.isArray(schema.items)) {
|
|
545
|
+
schema.items.forEach((item, index) => {
|
|
546
|
+
if (isJSONSchema7(item)) {
|
|
547
|
+
checkArraySizeLimits(item, `${path}.items[${index}]`);
|
|
548
|
+
}
|
|
549
|
+
});
|
|
550
|
+
} else if (isJSONSchema7(schema.items)) {
|
|
551
|
+
checkArraySizeLimits(schema.items, `${path}.items`);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* Determine number of items to generate for array schema
|
|
558
|
+
* Prefers explicit count, then schema minItems/maxItems, with sane defaults
|
|
559
|
+
* @param schema - Array schema with optional minItems/maxItems
|
|
560
|
+
* @param explicitCount - Explicit count override from plugin options
|
|
561
|
+
* @returns Number of array items to generate
|
|
562
|
+
*/
|
|
563
|
+
function determineArrayCount(
|
|
564
|
+
schema: JSONSchema7,
|
|
565
|
+
explicitCount?: number,
|
|
566
|
+
): number {
|
|
567
|
+
if (explicitCount !== undefined) {
|
|
568
|
+
// Handle negative or invalid counts
|
|
569
|
+
if (explicitCount < 0) {
|
|
570
|
+
return 0;
|
|
571
|
+
}
|
|
572
|
+
return explicitCount;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
if (schema.minItems !== undefined && schema.maxItems !== undefined) {
|
|
576
|
+
return (
|
|
577
|
+
Math.floor(Math.random() * (schema.maxItems - schema.minItems + 1)) +
|
|
578
|
+
schema.minItems
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
if (schema.minItems !== undefined) {
|
|
583
|
+
return Math.max(schema.minItems, DEFAULT_ARRAY_COUNT);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
if (schema.maxItems !== undefined) {
|
|
587
|
+
return Math.min(schema.maxItems, DEFAULT_ARRAY_COUNT);
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
return DEFAULT_ARRAY_COUNT;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* Apply overrides to generated data with support for templates
|
|
595
|
+
* Supports nested paths (dot notation), templates with {{params.id}}, and state access
|
|
596
|
+
* @param data - Generated data to apply overrides to
|
|
597
|
+
* @param overrides - Override values (can use templates)
|
|
598
|
+
* @param params - Route parameters for template expansion
|
|
599
|
+
* @param state - Plugin state for template expansion
|
|
600
|
+
* @param query - Query parameters for template expansion
|
|
601
|
+
* @returns Data with overrides applied
|
|
602
|
+
*/
|
|
603
|
+
function applyOverrides(
|
|
604
|
+
data: any,
|
|
605
|
+
overrides?: Record<string, any>,
|
|
606
|
+
params?: Record<string, string>,
|
|
607
|
+
state?: any,
|
|
608
|
+
query?: Record<string, string>,
|
|
609
|
+
): any {
|
|
610
|
+
if (!overrides) return data;
|
|
611
|
+
|
|
612
|
+
const result = structuredClone(data);
|
|
613
|
+
|
|
614
|
+
for (const [key, value] of Object.entries(overrides)) {
|
|
615
|
+
// Handle nested paths like "data.id" or "pagination.page"
|
|
616
|
+
if (key.includes(".")) {
|
|
617
|
+
setNestedProperty(result, key, value, { params, state, query });
|
|
618
|
+
} else {
|
|
619
|
+
// Handle flat keys and nested objects
|
|
620
|
+
if (
|
|
621
|
+
typeof value === "object" &&
|
|
622
|
+
value !== null &&
|
|
623
|
+
!Array.isArray(value)
|
|
624
|
+
) {
|
|
625
|
+
// Recursively apply nested overrides
|
|
626
|
+
if (result[key] && typeof result[key] === "object") {
|
|
627
|
+
result[key] = applyOverrides(
|
|
628
|
+
result[key],
|
|
629
|
+
value,
|
|
630
|
+
params,
|
|
631
|
+
state,
|
|
632
|
+
query,
|
|
633
|
+
);
|
|
634
|
+
} else {
|
|
635
|
+
result[key] = applyOverrides({}, value, params, state, query);
|
|
636
|
+
}
|
|
637
|
+
} else if (typeof value === "string" && value.includes("{{")) {
|
|
638
|
+
// Template processing
|
|
639
|
+
result[key] = processTemplate(value, { params, state, query });
|
|
640
|
+
} else {
|
|
641
|
+
result[key] = value;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
return result;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
function setNestedProperty(
|
|
650
|
+
obj: any,
|
|
651
|
+
path: string,
|
|
652
|
+
value: any,
|
|
653
|
+
context: {
|
|
654
|
+
params?: Record<string, string>;
|
|
655
|
+
state?: any;
|
|
656
|
+
query?: Record<string, string>;
|
|
657
|
+
},
|
|
658
|
+
): void {
|
|
659
|
+
const parts = path.split(".");
|
|
660
|
+
let current = obj;
|
|
661
|
+
|
|
662
|
+
// Navigate to the parent of the target property
|
|
663
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
664
|
+
const part = parts[i];
|
|
665
|
+
if (
|
|
666
|
+
!(part in current) ||
|
|
667
|
+
typeof current[part] !== "object" ||
|
|
668
|
+
current[part] === null
|
|
669
|
+
) {
|
|
670
|
+
current[part] = {};
|
|
671
|
+
}
|
|
672
|
+
current = current[part];
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// Set the final property
|
|
676
|
+
const finalKey = parts[parts.length - 1];
|
|
677
|
+
if (typeof value === "string" && value.includes("{{")) {
|
|
678
|
+
current[finalKey] = processTemplate(value, context);
|
|
679
|
+
} else {
|
|
680
|
+
current[finalKey] = value;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
function processTemplate(
|
|
685
|
+
template: string,
|
|
686
|
+
context: {
|
|
687
|
+
params?: Record<string, string>;
|
|
688
|
+
state?: any;
|
|
689
|
+
query?: Record<string, string>;
|
|
690
|
+
},
|
|
691
|
+
): any {
|
|
692
|
+
// Check if the template is just a single template expression
|
|
693
|
+
const singleTemplateMatch = template.match(/^\{\{\s*([^}]+)\s*\}\}$/);
|
|
694
|
+
if (singleTemplateMatch) {
|
|
695
|
+
// For single templates, return the actual value without string conversion
|
|
696
|
+
const expression = singleTemplateMatch[1];
|
|
697
|
+
const parts = expression.trim().split(".");
|
|
698
|
+
let result: any = context;
|
|
699
|
+
|
|
700
|
+
for (const part of parts) {
|
|
701
|
+
if (result && typeof result === "object") {
|
|
702
|
+
result = result[part];
|
|
703
|
+
} else {
|
|
704
|
+
return template; // Return original if can't resolve
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
return result !== undefined ? result : template;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
// For templates mixed with other text, do string replacement
|
|
712
|
+
const processed = template.replace(
|
|
713
|
+
/\{\{\s*([^}]+)\s*\}\}/g,
|
|
714
|
+
(match, expression) => {
|
|
715
|
+
const parts = expression.trim().split(".");
|
|
716
|
+
let result: any = context;
|
|
717
|
+
|
|
718
|
+
for (const part of parts) {
|
|
719
|
+
if (result && typeof result === "object") {
|
|
720
|
+
result = result[part];
|
|
721
|
+
} else {
|
|
722
|
+
return match; // Return original if can't resolve
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
return result !== undefined ? String(result) : match;
|
|
727
|
+
},
|
|
728
|
+
);
|
|
729
|
+
|
|
730
|
+
return processed;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* Validate that faker method string references a valid Faker.js API
|
|
735
|
+
* Checks format (namespace.method) and validates against known namespaces
|
|
736
|
+
* @param fakerMethod - Faker method string (e.g., "person.fullName")
|
|
737
|
+
* @throws {SchemaValidationError} When faker method format or namespace is invalid
|
|
738
|
+
*/
|
|
739
|
+
function validateFakerMethod(fakerMethod: string): void {
|
|
740
|
+
// Check if faker method follows valid format (namespace.method)
|
|
741
|
+
const parts = fakerMethod.split(".");
|
|
742
|
+
if (parts.length < 2) {
|
|
743
|
+
throw new SchemaValidationError(
|
|
744
|
+
"$.faker",
|
|
745
|
+
`Invalid faker method format: "${fakerMethod}"`,
|
|
746
|
+
"Use format like 'person.firstName' or 'internet.email'",
|
|
747
|
+
);
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
// Validate by resolving the method path on a real faker instance
|
|
751
|
+
const faker = createFakerInstance();
|
|
752
|
+
let current: any = faker;
|
|
753
|
+
for (const part of parts) {
|
|
754
|
+
if (current && typeof current === "object" && part in current) {
|
|
755
|
+
current = current[part];
|
|
756
|
+
} else {
|
|
757
|
+
throw new SchemaValidationError(
|
|
758
|
+
"$.faker",
|
|
759
|
+
`Invalid faker method: "${fakerMethod}"`,
|
|
760
|
+
"Check faker.js documentation for valid methods",
|
|
761
|
+
);
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
if (typeof current !== "function") {
|
|
765
|
+
throw new SchemaValidationError(
|
|
766
|
+
"$.faker",
|
|
767
|
+
`Invalid faker method: "${fakerMethod}" is not a function`,
|
|
768
|
+
"Check faker.js documentation for valid methods",
|
|
769
|
+
);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
function enhanceSchemaWithSmartMapping(schema: JSONSchema7): JSONSchema7 {
|
|
774
|
+
if (!schema || typeof schema !== "object") {
|
|
775
|
+
return schema;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
const enhanced = { ...schema };
|
|
779
|
+
|
|
780
|
+
// Handle object properties
|
|
781
|
+
if (enhanced.type === "object" && enhanced.properties) {
|
|
782
|
+
enhanced.properties = { ...enhanced.properties };
|
|
783
|
+
|
|
784
|
+
for (const [fieldName, fieldSchema] of Object.entries(
|
|
785
|
+
enhanced.properties,
|
|
786
|
+
)) {
|
|
787
|
+
if (isJSONSchema7(fieldSchema)) {
|
|
788
|
+
enhanced.properties[fieldName] = enhanceFieldSchema(
|
|
789
|
+
fieldName,
|
|
790
|
+
fieldSchema,
|
|
791
|
+
);
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
return enhanced;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
function enhanceFieldSchema(
|
|
800
|
+
fieldName: string,
|
|
801
|
+
fieldSchema: JSONSchema7,
|
|
802
|
+
): FakerSchema {
|
|
803
|
+
const enhanced: FakerSchema = { ...fieldSchema };
|
|
804
|
+
|
|
805
|
+
// If already has faker extension, validate it and don't override
|
|
806
|
+
if (enhanced.faker) {
|
|
807
|
+
validateFakerMethod(enhanced.faker);
|
|
808
|
+
return enhanced;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
// Apply smart field name mapping
|
|
812
|
+
const lowerFieldName = fieldName.toLowerCase();
|
|
813
|
+
|
|
814
|
+
// Email fields
|
|
815
|
+
if (lowerFieldName.includes("email")) {
|
|
816
|
+
enhanced.format = "email";
|
|
817
|
+
enhanced.faker = "internet.email";
|
|
818
|
+
}
|
|
819
|
+
// Name fields
|
|
820
|
+
else if (lowerFieldName === "firstname" || lowerFieldName === "first_name") {
|
|
821
|
+
enhanced.faker = "person.firstName";
|
|
822
|
+
} else if (lowerFieldName === "lastname" || lowerFieldName === "last_name") {
|
|
823
|
+
enhanced.faker = "person.lastName";
|
|
824
|
+
} else if (lowerFieldName === "name" || lowerFieldName === "fullname") {
|
|
825
|
+
enhanced.faker = "person.fullName";
|
|
826
|
+
}
|
|
827
|
+
// Phone fields
|
|
828
|
+
else if (lowerFieldName.includes("phone") || lowerFieldName === "mobile") {
|
|
829
|
+
enhanced.faker = "phone.number";
|
|
830
|
+
}
|
|
831
|
+
// Address fields
|
|
832
|
+
else if (lowerFieldName === "street" || lowerFieldName === "address") {
|
|
833
|
+
enhanced.faker = "location.streetAddress";
|
|
834
|
+
} else if (lowerFieldName === "city") {
|
|
835
|
+
enhanced.faker = "location.city";
|
|
836
|
+
} else if (lowerFieldName === "zipcode" || lowerFieldName === "zip") {
|
|
837
|
+
enhanced.faker = "location.zipCode";
|
|
838
|
+
}
|
|
839
|
+
// UUID fields
|
|
840
|
+
else if (
|
|
841
|
+
lowerFieldName === "uuid" ||
|
|
842
|
+
(lowerFieldName === "id" && enhanced.format === "uuid")
|
|
843
|
+
) {
|
|
844
|
+
enhanced.faker = "string.uuid";
|
|
845
|
+
}
|
|
846
|
+
// Date fields
|
|
847
|
+
else if (
|
|
848
|
+
lowerFieldName.includes("createdat") ||
|
|
849
|
+
lowerFieldName.includes("created_at") ||
|
|
850
|
+
lowerFieldName.includes("updatedat") ||
|
|
851
|
+
lowerFieldName.includes("updated_at")
|
|
852
|
+
) {
|
|
853
|
+
enhanced.format = "date-time";
|
|
854
|
+
enhanced.faker = "date.recent";
|
|
855
|
+
}
|
|
856
|
+
// Company fields
|
|
857
|
+
else if (lowerFieldName.includes("company")) {
|
|
858
|
+
enhanced.faker = "company.name";
|
|
859
|
+
} else if (lowerFieldName === "position" || lowerFieldName === "jobtitle") {
|
|
860
|
+
enhanced.faker = "person.jobTitle";
|
|
861
|
+
}
|
|
862
|
+
// Price/money fields
|
|
863
|
+
else if (lowerFieldName === "price" || lowerFieldName === "amount") {
|
|
864
|
+
enhanced.faker = "commerce.price";
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
return enhanced;
|
|
868
|
+
}
|