@schmock/faker 2.3.0 → 2.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.
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-name-matcher.d.ts +19 -0
  6. package/dist/field-name-matcher.d.ts.map +1 -1
  7. package/dist/index.d.ts +3 -0
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +4 -1
  10. package/dist/jsf-config.d.ts +19 -4
  11. package/dist/jsf-config.d.ts.map +1 -1
  12. package/dist/output-limits.d.ts +3 -0
  13. package/dist/output-limits.d.ts.map +1 -0
  14. package/dist/overrides.d.ts +2 -1
  15. package/dist/overrides.d.ts.map +1 -1
  16. package/dist/schema-children.d.ts +14 -0
  17. package/dist/schema-children.d.ts.map +1 -0
  18. package/dist/schema-enhancement.d.ts +1 -0
  19. package/dist/schema-enhancement.d.ts.map +1 -1
  20. package/dist/validation.d.ts +24 -5
  21. package/dist/validation.d.ts.map +1 -1
  22. package/package.json +21 -7
  23. package/dist/constants.js +0 -8
  24. package/dist/field-mappings.js +0 -1114
  25. package/dist/field-name-matcher.js +0 -148
  26. package/dist/jsf-config.js +0 -117
  27. package/dist/overrides.js +0 -137
  28. package/dist/schema-enhancement.js +0 -104
  29. package/dist/test-utils.d.ts +0 -57
  30. package/dist/test-utils.d.ts.map +0 -1
  31. package/dist/test-utils.js +0 -284
  32. package/dist/validation.js +0 -300
  33. package/src/advanced-features.test.ts +0 -912
  34. package/src/audit-faker-args.test.ts +0 -97
  35. package/src/constants.d.ts.map +0 -1
  36. package/src/constants.ts +0 -9
  37. package/src/data-quality.test.ts +0 -415
  38. package/src/error-handling.test.ts +0 -504
  39. package/src/field-mappings.test.ts +0 -351
  40. package/src/field-mappings.ts +0 -1148
  41. package/src/field-name-matcher.test.ts +0 -292
  42. package/src/field-name-matcher.ts +0 -187
  43. package/src/index.d.ts.map +0 -1
  44. package/src/index.test.ts +0 -1320
  45. package/src/index.ts +0 -185
  46. package/src/integration.test.ts +0 -634
  47. package/src/jsf-config.d.ts.map +0 -1
  48. package/src/jsf-config.test.ts +0 -65
  49. package/src/jsf-config.ts +0 -156
  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 -179
  54. package/src/performance.test.ts +0 -441
  55. package/src/plugin-integration.test.ts +0 -575
  56. package/src/post-process.test.ts +0 -177
  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 -222
  60. package/src/schema-enhancement.ts +0 -144
  61. package/src/steps/deterministic-seeds.steps.ts +0 -112
  62. package/src/steps/faker-plugin.steps.ts +0 -301
  63. package/src/test-utils.ts +0 -385
  64. package/src/validation.d.ts.map +0 -1
  65. package/src/validation.ts +0 -417
package/src/index.ts DELETED
@@ -1,185 +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 {
12
- createSeededRandom,
13
- generateWithJsf,
14
- resolveGenerationSeed,
15
- } from "./jsf-config.js";
16
- import { applyOverrides, determineArrayCount } from "./overrides.js";
17
- import { enhanceSchemaWithSmartMapping } from "./schema-enhancement.js";
18
- import { isJSONSchema7, validateSchema } from "./validation.js";
19
-
20
- export type SchemaGenerationContext = Schmock.SchemaGenerationContext;
21
-
22
- export type FakerPluginOptions = Schmock.FakerPluginOptions;
23
-
24
- function isRecord(value: unknown): value is Record<string, unknown> {
25
- return typeof value === "object" && value !== null && !Array.isArray(value);
26
- }
27
-
28
- export function fakerPlugin(options: FakerPluginOptions): Schmock.Plugin {
29
- // Validate schema immediately when plugin is created (fail-fast)
30
- validateSchema(options.schema);
31
-
32
- return {
33
- name: "faker",
34
- version: packageVersion,
35
-
36
- async process(context: Schmock.PluginContext, response?: unknown) {
37
- // If response already exists, pass it through
38
- if (response !== undefined && response !== null) {
39
- return { context, response };
40
- }
41
-
42
- try {
43
- const generatedResponse = await generateFromSchema({
44
- schema: options.schema,
45
- count: options.count,
46
- overrides: options.overrides,
47
- params: context.params,
48
- state: context.routeState,
49
- query: context.query,
50
- seed: options.seed,
51
- });
52
-
53
- return {
54
- context,
55
- response: generatedResponse,
56
- };
57
- } catch (error) {
58
- // Re-throw schema-specific errors as-is
59
- if (
60
- error instanceof SchemaValidationError ||
61
- error instanceof ResourceLimitError
62
- ) {
63
- throw error;
64
- }
65
-
66
- // Wrap other errors
67
- throw new SchemaGenerationError(
68
- context.path,
69
- error instanceof Error ? error : new Error(String(error)),
70
- options.schema,
71
- );
72
- }
73
- },
74
- };
75
- }
76
-
77
- export async function generateFromSchema(
78
- options: SchemaGenerationContext,
79
- ): Promise<unknown> {
80
- const { schema, count, overrides, params, state, query, seed } = options;
81
-
82
- validateSchema(schema);
83
-
84
- const generationSeed = resolveGenerationSeed(seed);
85
- const random = createSeededRandom(generationSeed);
86
-
87
- let enhancedSchema = enhanceSchemaWithSmartMapping(schema);
88
-
89
- // Resolve the top-level array size once, then let JSF generate the complete
90
- // array so tuple positions, uniqueness, and a seeded sequence are preserved.
91
- if (schema.type === "array" && schema.items) {
92
- const itemCount = determineArrayCount(schema, count, random);
93
-
94
- if (itemCount > MAX_ARRAY_SIZE) {
95
- throw new ResourceLimitError("array_size", MAX_ARRAY_SIZE, itemCount);
96
- }
97
-
98
- enhancedSchema = {
99
- ...enhancedSchema,
100
- minItems: itemCount,
101
- maxItems: itemCount,
102
- };
103
- }
104
-
105
- let generated: unknown = await generateWithJsf(
106
- enhancedSchema,
107
- generationSeed,
108
- );
109
- generated = postProcessGenerated(generated, enhancedSchema, random);
110
-
111
- if (Array.isArray(generated)) {
112
- return generated.map((item) =>
113
- applyOverrides(item, overrides, params, state, query),
114
- );
115
- }
116
-
117
- generated = applyOverrides(generated, overrides, params, state, query);
118
- return generated;
119
- }
120
-
121
- /**
122
- * Post-process generated data to apply nullable probability and boolean weighting.
123
- * Walks the schema and generated data in parallel, applying:
124
- * - schmockNullable: ~5% chance of null
125
- * - schmockTrueProbability: weighted boolean generation
126
- */
127
- function postProcessGenerated(
128
- data: unknown,
129
- schema: JSONSchema7,
130
- random: () => number,
131
- ): unknown {
132
- if (
133
- data === null ||
134
- data === undefined ||
135
- !schema ||
136
- typeof schema !== "object"
137
- ) {
138
- return data;
139
- }
140
-
141
- // Apply nullable probability at this level
142
- if ("schmockNullable" in schema && schema.schmockNullable === true) {
143
- if (random() < NULLABLE_NULL_PROBABILITY) {
144
- return null;
145
- }
146
- }
147
-
148
- // Apply boolean weighting at this level
149
- if (
150
- schema.type === "boolean" &&
151
- "schmockTrueProbability" in schema &&
152
- typeof schema.schmockTrueProbability === "number"
153
- ) {
154
- return random() < schema.schmockTrueProbability;
155
- }
156
-
157
- // Recurse into object properties
158
- if (isRecord(data) && schema.properties) {
159
- for (const [key, propSchema] of Object.entries(schema.properties)) {
160
- if (key in data && isJSONSchema7(propSchema)) {
161
- data[key] = postProcessGenerated(data[key], propSchema, random);
162
- }
163
- }
164
- }
165
-
166
- // Recurse into array items
167
- if (Array.isArray(data) && Array.isArray(schema.items)) {
168
- for (let index = 0; index < data.length; index++) {
169
- const itemSchema = schema.items[index];
170
- if (itemSchema && isJSONSchema7(itemSchema)) {
171
- data[index] = postProcessGenerated(data[index], itemSchema, random);
172
- }
173
- }
174
- } else if (
175
- Array.isArray(data) &&
176
- schema.items &&
177
- isJSONSchema7(schema.items)
178
- ) {
179
- for (let index = 0; index < data.length; index++) {
180
- data[index] = postProcessGenerated(data[index], schema.items, random);
181
- }
182
- }
183
-
184
- return data;
185
- }