@schmock/faker 2.2.2 → 2.3.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/field-mappings.d.ts.map +1 -1
- package/dist/field-mappings.js +8 -1
- package/dist/field-name-matcher.d.ts.map +1 -1
- package/dist/field-name-matcher.js +4 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/jsf-config.d.ts +11 -1
- package/dist/jsf-config.d.ts.map +1 -1
- package/dist/jsf-config.js +93 -11
- package/dist/overrides.d.ts.map +1 -1
- package/dist/overrides.js +20 -14
- package/dist/schema-enhancement.d.ts.map +1 -1
- package/dist/schema-enhancement.js +18 -3
- package/dist/test-utils.d.ts +9 -9
- package/dist/test-utils.d.ts.map +1 -1
- package/dist/test-utils.js +25 -12
- package/dist/validation.js +1 -1
- package/package.json +4 -4
- package/src/field-mappings.ts +8 -1
- package/src/field-name-matcher.ts +4 -5
- package/src/index.test.ts +102 -0
- package/src/index.ts +59 -42
- package/src/jsf-config.test.ts +65 -0
- package/src/jsf-config.ts +129 -21
- package/src/overrides.ts +23 -20
- package/src/performance.test.ts +274 -327
- package/src/post-process.test.ts +87 -61
- package/src/schema-enhancement.test.ts +8 -2
- package/src/schema-enhancement.ts +24 -8
- package/src/steps/deterministic-seeds.steps.ts +77 -0
- package/src/steps/faker-plugin.steps.ts +198 -6
- package/src/test-utils.ts +48 -28
- package/src/validation.ts +1 -1
package/src/test-utils.ts
CHANGED
|
@@ -6,6 +6,10 @@ interface FakerSchema extends JSONSchema7 {
|
|
|
6
6
|
faker?: string;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
10
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
11
|
+
}
|
|
12
|
+
|
|
9
13
|
// Schema Factory Functions
|
|
10
14
|
export const schemas = {
|
|
11
15
|
simple: {
|
|
@@ -117,19 +121,29 @@ export const validators = {
|
|
|
117
121
|
};
|
|
118
122
|
|
|
119
123
|
// Generate multiple samples to check for patterns
|
|
120
|
-
const mappedSamples:
|
|
124
|
+
const mappedSamples: unknown[] = [];
|
|
121
125
|
for (let i = 0; i < 10; i++) {
|
|
122
|
-
const result =
|
|
126
|
+
const result = await generateFromSchema({
|
|
123
127
|
schema: mappedSchema,
|
|
124
|
-
})
|
|
128
|
+
});
|
|
129
|
+
if (!isRecord(result)) {
|
|
130
|
+
throw new Error(
|
|
131
|
+
"Expected mapped schema generation to return an object",
|
|
132
|
+
);
|
|
133
|
+
}
|
|
125
134
|
mappedSamples.push(result[fieldName]);
|
|
126
135
|
}
|
|
127
136
|
|
|
128
|
-
const unmappedSamples:
|
|
137
|
+
const unmappedSamples: unknown[] = [];
|
|
129
138
|
for (let i = 0; i < 10; i++) {
|
|
130
|
-
const result =
|
|
139
|
+
const result = await generateFromSchema({
|
|
131
140
|
schema: unmappedSchema,
|
|
132
|
-
})
|
|
141
|
+
});
|
|
142
|
+
if (!isRecord(result)) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
"Expected unmapped schema generation to return an object",
|
|
145
|
+
);
|
|
146
|
+
}
|
|
133
147
|
unmappedSamples.push(result.unmappedRandomField12345);
|
|
134
148
|
}
|
|
135
149
|
|
|
@@ -142,13 +156,13 @@ export const validators = {
|
|
|
142
156
|
},
|
|
143
157
|
|
|
144
158
|
// Analyze uniqueness of generated data
|
|
145
|
-
uniquenessRatio: (samples:
|
|
159
|
+
uniquenessRatio: (samples: unknown[]): number => {
|
|
146
160
|
const unique = new Set(samples);
|
|
147
161
|
return unique.size / samples.length;
|
|
148
162
|
},
|
|
149
163
|
|
|
150
164
|
// Check if all samples match a basic pattern without being too specific
|
|
151
|
-
allMatch: (samples:
|
|
165
|
+
allMatch: <T>(samples: T[], validator: (sample: T) => boolean): boolean => {
|
|
152
166
|
return samples.every(validator);
|
|
153
167
|
},
|
|
154
168
|
|
|
@@ -229,7 +243,7 @@ export const performance = {
|
|
|
229
243
|
|
|
230
244
|
benchmark: async (
|
|
231
245
|
_name: string,
|
|
232
|
-
fn: () =>
|
|
246
|
+
fn: () => unknown | Promise<unknown>,
|
|
233
247
|
iterations = 100,
|
|
234
248
|
): Promise<{ mean: number; min: number; max: number }> => {
|
|
235
249
|
const times: number[] = [];
|
|
@@ -251,37 +265,35 @@ export const performance = {
|
|
|
251
265
|
|
|
252
266
|
// Test Data Generators
|
|
253
267
|
export const generate = {
|
|
254
|
-
samples: async
|
|
268
|
+
samples: async (
|
|
255
269
|
schema: JSONSchema7,
|
|
256
270
|
count = 10,
|
|
257
|
-
options?:
|
|
258
|
-
): Promise<
|
|
259
|
-
const results:
|
|
271
|
+
options?: Omit<Schmock.SchemaGenerationContext, "schema">,
|
|
272
|
+
): Promise<unknown[]> => {
|
|
273
|
+
const results: unknown[] = [];
|
|
260
274
|
for (let i = 0; i < count; i++) {
|
|
261
|
-
results.push(
|
|
275
|
+
results.push(await generateFromSchema({ schema, ...options }));
|
|
262
276
|
}
|
|
263
277
|
return results;
|
|
264
278
|
},
|
|
265
279
|
|
|
266
|
-
withSeed: async (schema: JSONSchema7,
|
|
267
|
-
|
|
268
|
-
// but we can at least ensure consistent test behavior
|
|
269
|
-
return await generateFromSchema({ schema });
|
|
280
|
+
withSeed: async (schema: JSONSchema7, seed?: number): Promise<unknown> => {
|
|
281
|
+
return await generateFromSchema({ schema, seed });
|
|
270
282
|
},
|
|
271
283
|
};
|
|
272
284
|
|
|
273
285
|
// Statistical Analysis
|
|
274
286
|
export const stats = {
|
|
275
|
-
distribution: (samples:
|
|
276
|
-
const dist = new Map<
|
|
287
|
+
distribution: (samples: unknown[]): Map<string, number> => {
|
|
288
|
+
const dist = new Map<string, number>();
|
|
277
289
|
for (const sample of samples) {
|
|
278
|
-
const key = JSON.stringify(sample);
|
|
290
|
+
const key = JSON.stringify(sample) ?? "undefined";
|
|
279
291
|
dist.set(key, (dist.get(key) || 0) + 1);
|
|
280
292
|
}
|
|
281
293
|
return dist;
|
|
282
294
|
},
|
|
283
295
|
|
|
284
|
-
entropy: (samples:
|
|
296
|
+
entropy: (samples: unknown[]): number => {
|
|
285
297
|
const dist = stats.distribution(samples);
|
|
286
298
|
const total = samples.length;
|
|
287
299
|
let entropy = 0;
|
|
@@ -304,7 +316,7 @@ export const schemaTests = {
|
|
|
304
316
|
},
|
|
305
317
|
|
|
306
318
|
expectInvalid: async (
|
|
307
|
-
schema:
|
|
319
|
+
schema: JSONSchema7,
|
|
308
320
|
errorMessage?: string | RegExp,
|
|
309
321
|
): Promise<void> => {
|
|
310
322
|
if (errorMessage) {
|
|
@@ -317,17 +329,25 @@ export const schemaTests = {
|
|
|
317
329
|
},
|
|
318
330
|
|
|
319
331
|
expectSchemaError: async (
|
|
320
|
-
schema:
|
|
332
|
+
schema: JSONSchema7,
|
|
321
333
|
path: string,
|
|
322
334
|
issue?: string,
|
|
323
335
|
): Promise<void> => {
|
|
324
336
|
try {
|
|
325
337
|
await generateFromSchema({ schema });
|
|
326
338
|
throw new Error("Expected schema validation to fail");
|
|
327
|
-
} catch (error:
|
|
339
|
+
} catch (error: unknown) {
|
|
340
|
+
expect(error).toBeInstanceOf(Error);
|
|
341
|
+
if (!(error instanceof Error)) {
|
|
342
|
+
throw new Error("Expected schema validation to throw an Error");
|
|
343
|
+
}
|
|
328
344
|
expect(error.name).toBe("SchemaValidationError");
|
|
329
345
|
// The schemaPath is in the context
|
|
330
|
-
if (
|
|
346
|
+
if (
|
|
347
|
+
"context" in error &&
|
|
348
|
+
isRecord(error.context) &&
|
|
349
|
+
error.context.schemaPath
|
|
350
|
+
) {
|
|
331
351
|
expect(error.context.schemaPath).toBe(path);
|
|
332
352
|
}
|
|
333
353
|
if (issue) {
|
|
@@ -338,13 +358,13 @@ export const schemaTests = {
|
|
|
338
358
|
};
|
|
339
359
|
|
|
340
360
|
// Helper to analyze data characteristics without hardcoding patterns
|
|
341
|
-
function analyzeDataCharacteristics(samples:
|
|
361
|
+
function analyzeDataCharacteristics(samples: unknown[]): string {
|
|
342
362
|
if (samples.length === 0) return "empty";
|
|
343
363
|
|
|
344
364
|
const first = samples[0];
|
|
345
365
|
const type = typeof first;
|
|
346
366
|
|
|
347
|
-
if (
|
|
367
|
+
if (!samples.every((sample) => typeof sample === "string")) return type;
|
|
348
368
|
|
|
349
369
|
// Analyze string characteristics
|
|
350
370
|
const characteristics: string[] = [type];
|
package/src/validation.ts
CHANGED
|
@@ -398,7 +398,7 @@ export function validateFakerMethod(fakerMethod: string): void {
|
|
|
398
398
|
let current: unknown = faker;
|
|
399
399
|
for (const part of parts) {
|
|
400
400
|
if (current && typeof current === "object" && part in current) {
|
|
401
|
-
current = (current
|
|
401
|
+
current = Reflect.get(current, part);
|
|
402
402
|
} else {
|
|
403
403
|
throw new SchemaValidationError(
|
|
404
404
|
"$.faker",
|