@schmock/schema 1.0.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.
@@ -0,0 +1,357 @@
1
+ import type { JSONSchema7 } from "json-schema";
2
+ import { expect } from "vitest";
3
+ import { generateFromSchema } from "./index";
4
+
5
+ // Schema Factory Functions
6
+ export const schemas = {
7
+ simple: {
8
+ string: (): JSONSchema7 => ({
9
+ type: "string",
10
+ }),
11
+
12
+ number: (): JSONSchema7 => ({
13
+ type: "number",
14
+ }),
15
+
16
+ object: (properties: Record<string, JSONSchema7> = {}): JSONSchema7 => ({
17
+ type: "object",
18
+ properties,
19
+ }),
20
+
21
+ array: (
22
+ items: JSONSchema7,
23
+ constraints?: { minItems?: number; maxItems?: number },
24
+ ): JSONSchema7 => ({
25
+ type: "array",
26
+ items,
27
+ ...constraints,
28
+ }),
29
+ },
30
+
31
+ withFaker: (type: JSONSchema7["type"], fakerMethod: string): JSONSchema7 =>
32
+ ({
33
+ type: type as any,
34
+ faker: fakerMethod,
35
+ }) as any,
36
+
37
+ nested: {
38
+ deep: (
39
+ depth: number,
40
+ leafSchema: JSONSchema7 = schemas.simple.string(),
41
+ ): JSONSchema7 => {
42
+ if (depth <= 0) return leafSchema;
43
+ return {
44
+ type: "object",
45
+ properties: {
46
+ nested: schemas.nested.deep(depth - 1, leafSchema),
47
+ },
48
+ };
49
+ },
50
+
51
+ wide: (
52
+ width: number,
53
+ propertySchema: JSONSchema7 = schemas.simple.string(),
54
+ ): JSONSchema7 => ({
55
+ type: "object",
56
+ properties: Object.fromEntries(
57
+ Array.from({ length: width }, (_, i) => [`prop${i}`, propertySchema]),
58
+ ),
59
+ }),
60
+ },
61
+
62
+ complex: {
63
+ user: (): JSONSchema7 => ({
64
+ type: "object",
65
+ properties: {
66
+ id: { type: "string", format: "uuid" },
67
+ email: { type: "string" },
68
+ firstName: { type: "string" },
69
+ lastName: { type: "string" },
70
+ createdAt: { type: "string" },
71
+ },
72
+ required: ["id", "email"],
73
+ }),
74
+
75
+ apiResponse: (): JSONSchema7 => ({
76
+ type: "object",
77
+ properties: {
78
+ success: { type: "boolean" },
79
+ data: {
80
+ type: "array",
81
+ items: schemas.complex.user(),
82
+ },
83
+ meta: {
84
+ type: "object",
85
+ properties: {
86
+ page: { type: "number" },
87
+ total: { type: "number" },
88
+ },
89
+ },
90
+ },
91
+ }),
92
+ },
93
+ };
94
+
95
+ // Validation Helpers
96
+ export const validators = {
97
+ // Check if a field was mapped to a faker method by comparing with unmapped behavior
98
+ isFieldMapped: async (
99
+ fieldName: string,
100
+ fieldType: JSONSchema7["type"] = "string",
101
+ ): Promise<boolean> => {
102
+ const mappedSchema = {
103
+ type: "object" as const,
104
+ properties: {
105
+ [fieldName]: { type: fieldType as any },
106
+ },
107
+ };
108
+
109
+ const unmappedSchema = {
110
+ type: "object" as const,
111
+ properties: {
112
+ unmappedRandomField12345: { type: fieldType as any },
113
+ },
114
+ };
115
+
116
+ // Generate multiple samples to check for patterns
117
+ const mappedSamples = Array.from(
118
+ { length: 10 },
119
+ () => generateFromSchema({ schema: mappedSchema })[fieldName],
120
+ );
121
+
122
+ const unmappedSamples = Array.from(
123
+ { length: 10 },
124
+ () =>
125
+ generateFromSchema({ schema: unmappedSchema }).unmappedRandomField12345,
126
+ );
127
+
128
+ // If field is mapped to a specific faker method, it should have different characteristics
129
+ // than the generic unmapped field
130
+ return (
131
+ analyzeDataCharacteristics(mappedSamples) !==
132
+ analyzeDataCharacteristics(unmappedSamples)
133
+ );
134
+ },
135
+
136
+ // Analyze uniqueness of generated data
137
+ uniquenessRatio: (samples: any[]): number => {
138
+ const unique = new Set(samples);
139
+ return unique.size / samples.length;
140
+ },
141
+
142
+ // Check if all samples match a basic pattern without being too specific
143
+ allMatch: (samples: any[], validator: (sample: any) => boolean): boolean => {
144
+ return samples.every(validator);
145
+ },
146
+
147
+ // Check if data appears to be from a specific faker category
148
+ appearsToBeFromCategory: (
149
+ samples: string[],
150
+ category: "email" | "name" | "phone" | "address" | "uuid" | "date",
151
+ ): boolean => {
152
+ switch (category) {
153
+ case "email":
154
+ return validators.allMatch(
155
+ samples,
156
+ (s) => typeof s === "string" && s.includes("@") && s.includes("."),
157
+ );
158
+ case "name":
159
+ return validators.allMatch(
160
+ samples,
161
+ (s) =>
162
+ typeof s === "string" &&
163
+ s.length > 1 &&
164
+ s.length < 50 &&
165
+ /^[A-Z]/.test(s),
166
+ );
167
+ case "phone":
168
+ return validators.allMatch(
169
+ samples,
170
+ (s) => typeof s === "string" && /\d/.test(s) && s.length > 10,
171
+ );
172
+ case "address":
173
+ return validators.allMatch(
174
+ samples,
175
+ (s) =>
176
+ typeof s === "string" &&
177
+ s.length > 10 &&
178
+ /\d/.test(s) &&
179
+ /[A-Z]/.test(s),
180
+ );
181
+ case "uuid":
182
+ return validators.allMatch(
183
+ samples,
184
+ (s) =>
185
+ typeof s === "string" &&
186
+ /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(
187
+ s,
188
+ ),
189
+ );
190
+ case "date":
191
+ return validators.allMatch(
192
+ samples,
193
+ (s) => typeof s === "string" && !Number.isNaN(Date.parse(s)),
194
+ );
195
+ default:
196
+ return false;
197
+ }
198
+ },
199
+ };
200
+
201
+ // Performance Testing Utilities
202
+ export const performance = {
203
+ measure: async <T>(
204
+ fn: () => T | Promise<T>,
205
+ ): Promise<{ result: T; duration: number }> => {
206
+ const start = Date.now();
207
+ const result = await fn();
208
+ const duration = Date.now() - start;
209
+ return { result, duration };
210
+ },
211
+
212
+ measureMemory: (fn: () => void): number => {
213
+ if (globalThis.gc) {
214
+ globalThis.gc();
215
+ }
216
+ const before = process.memoryUsage().heapUsed;
217
+ fn();
218
+ const after = process.memoryUsage().heapUsed;
219
+ return after - before;
220
+ },
221
+
222
+ benchmark: async (
223
+ _name: string,
224
+ fn: () => any,
225
+ iterations = 100,
226
+ ): Promise<{ mean: number; min: number; max: number }> => {
227
+ const times: number[] = [];
228
+
229
+ for (let i = 0; i < iterations; i++) {
230
+ const start = Date.now();
231
+ await fn();
232
+ const duration = Date.now() - start;
233
+ times.push(duration);
234
+ }
235
+
236
+ return {
237
+ mean: times.reduce((a, b) => a + b, 0) / times.length,
238
+ min: Math.min(...times),
239
+ max: Math.max(...times),
240
+ };
241
+ },
242
+ };
243
+
244
+ // Test Data Generators
245
+ export const generate = {
246
+ samples: <T>(schema: JSONSchema7, count = 10, options?: any): T[] => {
247
+ return Array.from({ length: count }, () =>
248
+ generateFromSchema({ schema, ...options }),
249
+ );
250
+ },
251
+
252
+ withSeed: (schema: JSONSchema7, _seed?: number): any => {
253
+ // Note: faker.js doesn't support seeding in the same way,
254
+ // but we can at least ensure consistent test behavior
255
+ return generateFromSchema({ schema });
256
+ },
257
+ };
258
+
259
+ // Statistical Analysis
260
+ export const stats = {
261
+ distribution: (samples: any[]): Map<any, number> => {
262
+ const dist = new Map<any, number>();
263
+ for (const sample of samples) {
264
+ const key = JSON.stringify(sample);
265
+ dist.set(key, (dist.get(key) || 0) + 1);
266
+ }
267
+ return dist;
268
+ },
269
+
270
+ entropy: (samples: any[]): number => {
271
+ const dist = stats.distribution(samples);
272
+ const total = samples.length;
273
+ let entropy = 0;
274
+
275
+ for (const count of dist.values()) {
276
+ const p = count / total;
277
+ if (p > 0) {
278
+ entropy -= p * Math.log2(p);
279
+ }
280
+ }
281
+
282
+ return entropy;
283
+ },
284
+ };
285
+
286
+ // Schema Validation Test Helpers
287
+ export const schemaTests = {
288
+ expectValid: (schema: JSONSchema7): void => {
289
+ expect(() => generateFromSchema({ schema })).not.toThrow();
290
+ },
291
+
292
+ expectInvalid: (schema: any, errorMessage?: string | RegExp): void => {
293
+ if (errorMessage) {
294
+ expect(() => generateFromSchema({ schema })).toThrow(errorMessage);
295
+ } else {
296
+ expect(() => generateFromSchema({ schema })).toThrow();
297
+ }
298
+ },
299
+
300
+ expectSchemaError: (schema: any, path: string, issue?: string): void => {
301
+ try {
302
+ generateFromSchema({ schema });
303
+ throw new Error("Expected schema validation to fail");
304
+ } catch (error: any) {
305
+ expect(error.name).toBe("SchemaValidationError");
306
+ // The schemaPath is in the context
307
+ if (error.context?.schemaPath) {
308
+ expect(error.context.schemaPath).toBe(path);
309
+ }
310
+ if (issue) {
311
+ expect(error.message).toContain(issue);
312
+ }
313
+ }
314
+ },
315
+ };
316
+
317
+ // Helper to analyze data characteristics without hardcoding patterns
318
+ function analyzeDataCharacteristics(samples: any[]): string {
319
+ if (samples.length === 0) return "empty";
320
+
321
+ const first = samples[0];
322
+ const type = typeof first;
323
+
324
+ if (type !== "string") return type;
325
+
326
+ // Analyze string characteristics
327
+ const characteristics: string[] = [type];
328
+
329
+ // Check common patterns without being too specific
330
+ if (samples.every((s) => s.includes("@"))) characteristics.push("has-at");
331
+ if (samples.every((s) => /^\d+$/.test(s))) characteristics.push("numeric");
332
+ if (samples.every((s) => /^[0-9a-f-]+$/i.test(s)))
333
+ characteristics.push("hex-like");
334
+ if (samples.every((s) => s.length > 50)) characteristics.push("long");
335
+ if (samples.every((s) => s.length < 10)) characteristics.push("short");
336
+ if (validators.uniquenessRatio(samples) > 0.8)
337
+ characteristics.push("high-entropy");
338
+ if (validators.uniquenessRatio(samples) < 0.2)
339
+ characteristics.push("low-entropy");
340
+
341
+ return characteristics.join("-");
342
+ }
343
+
344
+ // Mock/Spy utilities for testing faker integration
345
+ export const mocks = {
346
+ trackFakerCalls: () => {
347
+ const calls: string[] = [];
348
+ // This would need actual implementation with faker.js internals
349
+ // For now, it's a placeholder for the concept
350
+ return {
351
+ calls,
352
+ reset: () => {
353
+ calls.length = 0;
354
+ },
355
+ };
356
+ },
357
+ };