@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
package/src/index.ts DELETED
@@ -1,168 +0,0 @@
1
- /// <reference path="../../core/schmock.d.ts" />
2
-
3
- import {
4
- ResourceLimitError,
5
- SchemaGenerationError,
6
- SchemaValidationError,
7
- } from "@schmock/core";
8
- import type { JSONSchema7 } from "json-schema";
9
- import { version as packageVersion } from "../package.json";
10
- import { MAX_ARRAY_SIZE, NULLABLE_NULL_PROBABILITY } from "./constants.js";
11
- import { generateWithJsf } from "./jsf-config.js";
12
- import { applyOverrides, determineArrayCount } from "./overrides.js";
13
- import { enhanceSchemaWithSmartMapping } from "./schema-enhancement.js";
14
- import { isJSONSchema7, validateSchema } from "./validation.js";
15
-
16
- export type SchemaGenerationContext = Schmock.SchemaGenerationContext;
17
-
18
- export type FakerPluginOptions = Schmock.FakerPluginOptions;
19
-
20
- export function fakerPlugin(options: FakerPluginOptions): Schmock.Plugin {
21
- // Validate schema immediately when plugin is created (fail-fast)
22
- validateSchema(options.schema);
23
-
24
- return {
25
- name: "faker",
26
- version: packageVersion,
27
-
28
- async process(context: Schmock.PluginContext, response?: unknown) {
29
- // If response already exists, pass it through
30
- if (response !== undefined && response !== null) {
31
- return { context, response };
32
- }
33
-
34
- try {
35
- const generatedResponse = await generateFromSchema({
36
- schema: options.schema,
37
- count: options.count,
38
- overrides: options.overrides,
39
- params: context.params,
40
- state: context.routeState,
41
- query: context.query,
42
- seed: options.seed,
43
- });
44
-
45
- return {
46
- context,
47
- response: generatedResponse,
48
- };
49
- } catch (error) {
50
- // Re-throw schema-specific errors as-is
51
- if (
52
- error instanceof SchemaValidationError ||
53
- error instanceof ResourceLimitError
54
- ) {
55
- throw error;
56
- }
57
-
58
- // Wrap other errors
59
- throw new SchemaGenerationError(
60
- context.path,
61
- error instanceof Error ? error : new Error(String(error)),
62
- options.schema,
63
- );
64
- }
65
- },
66
- };
67
- }
68
-
69
- export async function generateFromSchema(
70
- options: SchemaGenerationContext,
71
- ): Promise<unknown> {
72
- const { schema, count, overrides, params, state, query, seed } = options;
73
-
74
- validateSchema(schema);
75
-
76
- // Handle array schemas with count
77
- if (schema.type === "array" && schema.items) {
78
- const itemCount = determineArrayCount(schema, count);
79
-
80
- // Check for resource limits
81
- if (itemCount > MAX_ARRAY_SIZE) {
82
- throw new ResourceLimitError("array_size", MAX_ARRAY_SIZE, itemCount);
83
- }
84
-
85
- const rawItemSchema = Array.isArray(schema.items)
86
- ? schema.items[0]
87
- : schema.items;
88
-
89
- if (!rawItemSchema || typeof rawItemSchema === "boolean") {
90
- throw new SchemaValidationError(
91
- "$.items",
92
- "Array schema must have valid items definition",
93
- );
94
- }
95
-
96
- const itemSchema = rawItemSchema;
97
- const enhancedItemSchema = enhanceSchemaWithSmartMapping(itemSchema);
98
-
99
- const items: unknown[] = [];
100
- for (let i = 0; i < itemCount; i++) {
101
- let item: unknown = await generateWithJsf(enhancedItemSchema, seed);
102
- item = postProcessGenerated(item, enhancedItemSchema);
103
- item = applyOverrides(item, overrides, params, state, query);
104
- items.push(item);
105
- }
106
- return items;
107
- }
108
-
109
- // Handle object schemas
110
- const enhancedSchema = enhanceSchemaWithSmartMapping(schema);
111
- let generated: unknown = await generateWithJsf(enhancedSchema, seed);
112
- generated = postProcessGenerated(generated, enhancedSchema);
113
- generated = applyOverrides(generated, overrides, params, state, query);
114
- return generated;
115
- }
116
-
117
- /**
118
- * Post-process generated data to apply nullable probability and boolean weighting.
119
- * Walks the schema and generated data in parallel, applying:
120
- * - schmockNullable: ~5% chance of null
121
- * - schmockTrueProbability: weighted boolean generation
122
- */
123
- function postProcessGenerated(data: unknown, schema: JSONSchema7): unknown {
124
- if (
125
- data === null ||
126
- data === undefined ||
127
- !schema ||
128
- typeof schema !== "object"
129
- ) {
130
- return data;
131
- }
132
-
133
- const schemaExt = schema as Record<string, unknown>;
134
-
135
- // Apply nullable probability at this level
136
- if (schemaExt.schmockNullable === true) {
137
- if (Math.random() < NULLABLE_NULL_PROBABILITY) {
138
- return null;
139
- }
140
- }
141
-
142
- // Apply boolean weighting at this level
143
- if (
144
- schema.type === "boolean" &&
145
- typeof schemaExt.schmockTrueProbability === "number"
146
- ) {
147
- return Math.random() < schemaExt.schmockTrueProbability;
148
- }
149
-
150
- // Recurse into object properties
151
- if (typeof data === "object" && !Array.isArray(data) && schema.properties) {
152
- const record = data as Record<string, unknown>;
153
- for (const [key, propSchema] of Object.entries(schema.properties)) {
154
- if (key in record && isJSONSchema7(propSchema)) {
155
- record[key] = postProcessGenerated(record[key], propSchema);
156
- }
157
- }
158
- }
159
-
160
- // Recurse into array items
161
- if (Array.isArray(data) && schema.items && isJSONSchema7(schema.items)) {
162
- for (let i = 0; i < data.length; i++) {
163
- data[i] = postProcessGenerated(data[i], schema.items);
164
- }
165
- }
166
-
167
- return data;
168
- }