@schmock/faker 2.2.3 → 2.3.1

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.
Files changed (65) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +45 -0
  3. package/dist/constants.d.ts +82 -1
  4. package/dist/constants.d.ts.map +1 -1
  5. package/dist/field-mappings.d.ts.map +1 -1
  6. package/dist/field-name-matcher.d.ts +19 -0
  7. package/dist/field-name-matcher.d.ts.map +1 -1
  8. package/dist/index.d.ts +3 -0
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +4 -1
  11. package/dist/jsf-config.d.ts +27 -2
  12. package/dist/jsf-config.d.ts.map +1 -1
  13. package/dist/output-limits.d.ts +3 -0
  14. package/dist/output-limits.d.ts.map +1 -0
  15. package/dist/overrides.d.ts +2 -1
  16. package/dist/overrides.d.ts.map +1 -1
  17. package/dist/schema-children.d.ts +14 -0
  18. package/dist/schema-children.d.ts.map +1 -0
  19. package/dist/schema-enhancement.d.ts +1 -0
  20. package/dist/schema-enhancement.d.ts.map +1 -1
  21. package/dist/validation.d.ts +24 -5
  22. package/dist/validation.d.ts.map +1 -1
  23. package/package.json +19 -9
  24. package/dist/constants.js +0 -8
  25. package/dist/field-mappings.js +0 -1107
  26. package/dist/field-name-matcher.js +0 -149
  27. package/dist/jsf-config.js +0 -35
  28. package/dist/overrides.js +0 -131
  29. package/dist/schema-enhancement.js +0 -89
  30. package/dist/test-utils.d.ts +0 -57
  31. package/dist/test-utils.d.ts.map +0 -1
  32. package/dist/test-utils.js +0 -271
  33. package/dist/validation.js +0 -300
  34. package/src/advanced-features.test.ts +0 -912
  35. package/src/audit-faker-args.test.ts +0 -97
  36. package/src/constants.d.ts.map +0 -1
  37. package/src/constants.ts +0 -9
  38. package/src/data-quality.test.ts +0 -415
  39. package/src/error-handling.test.ts +0 -504
  40. package/src/field-mappings.test.ts +0 -351
  41. package/src/field-mappings.ts +0 -1141
  42. package/src/field-name-matcher.test.ts +0 -292
  43. package/src/field-name-matcher.ts +0 -188
  44. package/src/index.d.ts.map +0 -1
  45. package/src/index.test.ts +0 -1218
  46. package/src/index.ts +0 -168
  47. package/src/integration.test.ts +0 -634
  48. package/src/jsf-config.d.ts.map +0 -1
  49. package/src/jsf-config.ts +0 -48
  50. package/src/overrides.d.ts.map +0 -1
  51. package/src/overrides.property.test.ts +0 -172
  52. package/src/overrides.test.ts +0 -38
  53. package/src/overrides.ts +0 -176
  54. package/src/performance.test.ts +0 -494
  55. package/src/plugin-integration.test.ts +0 -575
  56. package/src/post-process.test.ts +0 -151
  57. package/src/real-world.test.ts +0 -636
  58. package/src/schema-enhancement.d.ts.map +0 -1
  59. package/src/schema-enhancement.test.ts +0 -216
  60. package/src/schema-enhancement.ts +0 -128
  61. package/src/steps/deterministic-seeds.steps.ts +0 -35
  62. package/src/steps/faker-plugin.steps.ts +0 -109
  63. package/src/test-utils.ts +0 -365
  64. package/src/validation.d.ts.map +0 -1
  65. package/src/validation.ts +0 -417
@@ -1,504 +0,0 @@
1
- import type { JSONSchema7 } from "json-schema";
2
- import { describe, expect, it } from "vitest";
3
- import { fakerPlugin, generateFromSchema } from "./index";
4
- import { schemas } from "./test-utils";
5
-
6
- describe("Schema Error Handling", () => {
7
- describe("Validation Error Messages", () => {
8
- it("provides clear error for empty schemas", async () => {
9
- try {
10
- await generateFromSchema({ schema: {} as any });
11
- expect.fail("Should have thrown");
12
- } catch (error: any) {
13
- expect(error.name).toBe("SchemaValidationError");
14
- expect(error.message).toContain("Schema cannot be empty");
15
- expect(error.code).toBe("SCHEMA_VALIDATION_ERROR");
16
- }
17
- });
18
-
19
- it("provides clear error for invalid types", async () => {
20
- try {
21
- await generateFromSchema({ schema: { type: "invalid" as any } });
22
- expect.fail("Should have thrown");
23
- } catch (error: any) {
24
- expect(error.name).toBe("SchemaValidationError");
25
- expect(error.message).toContain("Invalid schema type");
26
- expect(error.message).toContain("invalid");
27
- expect(error.message).toContain("Supported types are");
28
- }
29
- });
30
-
31
- it("provides helpful suggestions for common mistakes", async () => {
32
- try {
33
- await generateFromSchema({
34
- schema: {
35
- type: "object",
36
- properties: "should be object" as any,
37
- },
38
- });
39
- expect.fail("Should have thrown");
40
- } catch (error: any) {
41
- expect(error.message).toContain("Properties must be an object");
42
- expect(error.message).toContain(
43
- 'Use { "propertyName": { "type": "string" } } format',
44
- );
45
- }
46
- });
47
-
48
- it("includes schema path in error messages", async () => {
49
- try {
50
- await generateFromSchema({
51
- schema: {
52
- type: "object",
53
- properties: {
54
- user: {
55
- type: "object",
56
- properties: {
57
- email: {
58
- type: "invalid" as any,
59
- },
60
- },
61
- },
62
- },
63
- },
64
- });
65
- expect.fail("Should have thrown");
66
- } catch (error: any) {
67
- expect(error.context.schemaPath).toContain("user");
68
- expect(error.context.schemaPath).toContain("email");
69
- }
70
- });
71
-
72
- it("validates faker method namespaces with helpful errors", async () => {
73
- try {
74
- await generateFromSchema({
75
- schema: {
76
- type: "object",
77
- properties: {
78
- field: {
79
- type: "string",
80
- faker: "badnamespace.method" as any,
81
- },
82
- },
83
- },
84
- });
85
- expect.fail("Should have thrown");
86
- } catch (error: any) {
87
- expect(error.message).toContain("Invalid faker method");
88
- expect(error.message).toContain("badnamespace.method");
89
- }
90
- });
91
-
92
- it("validates array schemas must have items", async () => {
93
- try {
94
- await generateFromSchema({
95
- schema: {
96
- type: "array",
97
- items: null as any,
98
- },
99
- });
100
- expect.fail("Should have thrown");
101
- } catch (error: any) {
102
- expect(error.message).toContain(
103
- "Array schema must have valid items definition",
104
- );
105
- expect(error.message).toContain("Define items as a schema object");
106
- }
107
- });
108
- });
109
-
110
- describe("Resource Limit Errors", () => {
111
- it("provides clear error for array size limits", async () => {
112
- try {
113
- await generateFromSchema({
114
- schema: {
115
- type: "array",
116
- items: { type: "string" },
117
- maxItems: 50000,
118
- },
119
- });
120
- expect.fail("Should have thrown");
121
- } catch (error: any) {
122
- // Should throw some kind of error for resource limits
123
- expect(error).toBeDefined();
124
- expect(error.message).toContain("array_max_items");
125
- }
126
- });
127
-
128
- it("provides clear error for nesting depth", async () => {
129
- try {
130
- const deepSchema = schemas.nested.deep(15);
131
- await generateFromSchema({ schema: deepSchema });
132
- expect.fail("Should have thrown");
133
- } catch (error: any) {
134
- // Should throw some kind of error for nesting depth
135
- expect(error).toBeDefined();
136
- expect(error.message).toContain("schema_nesting_depth");
137
- }
138
- });
139
-
140
- it("detects memory risks from nested arrays", async () => {
141
- try {
142
- await generateFromSchema({
143
- schema: {
144
- type: "object",
145
- properties: {
146
- level1: {
147
- type: "array",
148
- items: {
149
- type: "array",
150
- items: {
151
- type: "array",
152
- items: {
153
- type: "object",
154
- properties: {
155
- data: { type: "string" },
156
- },
157
- },
158
- maxItems: 200,
159
- },
160
- maxItems: 200,
161
- },
162
- maxItems: 200,
163
- },
164
- },
165
- },
166
- });
167
- expect.fail("Should have thrown");
168
- } catch (error: any) {
169
- expect(error.name).toBe("ResourceLimitError");
170
- expect(error.message).toContain("memory");
171
- }
172
- });
173
-
174
- it("provides actionable error messages for limits", async () => {
175
- try {
176
- await generateFromSchema({
177
- schema: {
178
- type: "array",
179
- items: { type: "string" },
180
- minItems: 20000,
181
- },
182
- });
183
- expect.fail("Should have thrown");
184
- } catch (error: any) {
185
- expect(error.message).toContain("Resource limit exceeded");
186
- expect(error.message).toContain("array");
187
- // Message should indicate what limit was hit
188
- }
189
- });
190
- });
191
-
192
- describe("Schema Generation Errors", () => {
193
- it("wraps json-schema-faker errors appropriately", async () => {
194
- try {
195
- await generateFromSchema({
196
- schema: {
197
- type: "string",
198
- pattern: "[", // Invalid regex
199
- },
200
- });
201
- expect.fail("Should have thrown");
202
- } catch (error: any) {
203
- // Should throw some kind of error for invalid pattern
204
- expect(error).toBeDefined();
205
- expect(error.name).toContain("Error");
206
- }
207
- });
208
-
209
- it("includes context in generation errors", async () => {
210
- const plugin = fakerPlugin({
211
- schema: {
212
- type: "string",
213
- pattern: "[",
214
- },
215
- });
216
-
217
- const context = {
218
- method: "GET",
219
- path: "/test/123",
220
- params: { id: "123" },
221
- query: {},
222
- state: {},
223
- headers: {},
224
- body: null,
225
- route: {},
226
- };
227
-
228
- try {
229
- await plugin.process(context);
230
- expect.fail("Should have thrown");
231
- } catch (error: any) {
232
- // Should throw some kind of error
233
- expect(error).toBeDefined();
234
- }
235
- });
236
-
237
- it("handles circular reference errors", async () => {
238
- try {
239
- await generateFromSchema({
240
- schema: {
241
- type: "object",
242
- properties: {
243
- self: { $ref: "#" },
244
- },
245
- },
246
- });
247
- expect.fail("Should have thrown");
248
- } catch (error: any) {
249
- expect(error.message).toContain("circular");
250
- expect(error.name).toBe("SchemaValidationError");
251
- }
252
- });
253
-
254
- it("handles missing reference errors", async () => {
255
- try {
256
- await generateFromSchema({
257
- schema: {
258
- type: "object",
259
- properties: {
260
- ref: { $ref: "#/definitions/nonexistent" },
261
- },
262
- },
263
- });
264
- expect.fail("Should have thrown");
265
- } catch (error: any) {
266
- // json-schema-faker throws its own error
267
- expect(error.message).toContain("Unresolved $ref");
268
- }
269
- });
270
- });
271
-
272
- describe("Plugin Error Handling", () => {
273
- it("validates schema at plugin creation", () => {
274
- try {
275
- fakerPlugin({
276
- schema: null as any,
277
- });
278
- expect.fail("Should have thrown");
279
- } catch (error: any) {
280
- expect(error.name).toBe("SchemaValidationError");
281
- // Error happens at plugin creation, not processing
282
- }
283
- });
284
-
285
- it("handles null context gracefully", async () => {
286
- const plugin = fakerPlugin({
287
- schema: schemas.simple.object({ id: schemas.simple.number() }),
288
- });
289
-
290
- // Should not crash with null params
291
- const result = await plugin.process({
292
- method: "GET",
293
- path: "/test",
294
- params: null as any,
295
- query: null as any,
296
- state: null as any,
297
- headers: {},
298
- body: null,
299
- route: {},
300
- });
301
-
302
- expect(result.response).toHaveProperty("id");
303
- });
304
-
305
- it("preserves original error stack traces", async () => {
306
- try {
307
- await generateFromSchema({
308
- schema: {
309
- type: "string",
310
- pattern: "[",
311
- },
312
- });
313
- expect.fail("Should have thrown");
314
- } catch (error: any) {
315
- // Should preserve error info
316
- expect(error).toBeDefined();
317
- expect(error.stack).toBeDefined();
318
- }
319
- });
320
- });
321
-
322
- describe("Error Recovery", () => {
323
- it("can generate after validation errors", async () => {
324
- // First attempt with invalid schema
325
- try {
326
- await generateFromSchema({ schema: { type: "invalid" as any } });
327
- } catch (_error) {
328
- // Expected
329
- }
330
-
331
- // Should be able to generate with valid schema
332
- const result = await generateFromSchema({
333
- schema: schemas.simple.object({ id: schemas.simple.number() }),
334
- });
335
-
336
- expect(result).toHaveProperty("id");
337
- });
338
-
339
- it("plugin continues to work after errors", async () => {
340
- const plugin = fakerPlugin({
341
- schema: schemas.simple.object({ id: schemas.simple.number() }),
342
- });
343
-
344
- const context = {
345
- method: "GET",
346
- path: "/test",
347
- params: {},
348
- query: {},
349
- state: {},
350
- headers: {},
351
- body: null,
352
- route: {},
353
- };
354
-
355
- // Multiple calls should work
356
- const result1 = await plugin.process(context);
357
- const result2 = await plugin.process(context);
358
-
359
- expect(result1.response).toHaveProperty("id");
360
- expect(result2.response).toHaveProperty("id");
361
- });
362
- });
363
-
364
- describe("Edge Case Error Handling", () => {
365
- it("handles deeply nested validation errors", async () => {
366
- try {
367
- await generateFromSchema({
368
- schema: {
369
- type: "object",
370
- properties: {
371
- a: {
372
- type: "object",
373
- properties: {
374
- b: {
375
- type: "object",
376
- properties: {
377
- c: {
378
- type: "object",
379
- properties: {
380
- d: {
381
- type: "array",
382
- items: null as any,
383
- },
384
- },
385
- },
386
- },
387
- },
388
- },
389
- },
390
- },
391
- },
392
- });
393
- expect.fail("Should have thrown");
394
- } catch (error: any) {
395
- expect(error.context.schemaPath).toContain("a");
396
- expect(error.context.schemaPath).toContain("b");
397
- expect(error.context.schemaPath).toContain("c");
398
- expect(error.context.schemaPath).toContain("d");
399
- }
400
- });
401
-
402
- it("handles multiple validation errors (reports first)", async () => {
403
- try {
404
- await generateFromSchema({
405
- schema: {
406
- type: "invalid" as any,
407
- properties: "also invalid" as any,
408
- items: null as any,
409
- },
410
- });
411
- expect.fail("Should have thrown");
412
- } catch (error: any) {
413
- // Should report the first error encountered
414
- expect(error.message).toContain("Invalid schema type");
415
- }
416
- });
417
-
418
- it("handles non-Error objects in catch blocks", async () => {
419
- // This is more about the implementation being defensive
420
- const plugin = fakerPlugin({
421
- schema: schemas.simple.object({ id: schemas.simple.number() }),
422
- });
423
-
424
- // Even with weird inputs, should handle gracefully
425
- await expect(plugin.process({} as any)).resolves.not.toThrow();
426
- });
427
-
428
- it("handles schemas that generate invalid JSON", async () => {
429
- // Some edge cases might generate circular structures
430
- const schema: JSONSchema7 = {
431
- type: "object",
432
- properties: {
433
- normal: { type: "string" },
434
- },
435
- };
436
-
437
- const result = await generateFromSchema({ schema });
438
-
439
- // Should be serializable
440
- expect(() => JSON.stringify(result)).not.toThrow();
441
- });
442
- });
443
-
444
- describe("Error Message Quality", () => {
445
- it("uses consistent error message format", async () => {
446
- const errors: any[] = [];
447
-
448
- // Collect various errors
449
- try {
450
- await generateFromSchema({ schema: {} as any });
451
- } catch (e) {
452
- errors.push(e);
453
- }
454
-
455
- try {
456
- await generateFromSchema({ schema: { type: "invalid" as any } });
457
- } catch (e) {
458
- errors.push(e);
459
- }
460
-
461
- // All should have consistent structure
462
- errors.forEach((error) => {
463
- expect(error).toHaveProperty("name");
464
- expect(error).toHaveProperty("message");
465
- expect(error).toHaveProperty("code");
466
- expect(error).toHaveProperty("context");
467
- });
468
- });
469
-
470
- it("avoids exposing internal implementation details", async () => {
471
- try {
472
- await generateFromSchema({
473
- schema: {
474
- type: "string",
475
- pattern: "[",
476
- },
477
- });
478
- expect.fail("Should have thrown");
479
- } catch (error: any) {
480
- // Should not expose internal file paths or function names
481
- expect(error.message).not.toContain("node_modules");
482
- expect(error.message).not.toContain("dist/");
483
- // Should have some useful error info
484
- expect(error.message.length).toBeGreaterThan(5);
485
- }
486
- });
487
-
488
- it("provides actionable error messages", async () => {
489
- try {
490
- await generateFromSchema({
491
- schema: {
492
- type: "object",
493
- properties: [] as any, // Wrong type
494
- },
495
- });
496
- expect.fail("Should have thrown");
497
- } catch (error: any) {
498
- // Should tell user what to do
499
- expect(error.message).toContain("must be an object");
500
- expect(error.context.suggestion).toBeDefined();
501
- }
502
- });
503
- });
504
- });