@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/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: any[] = [];
124
+ const mappedSamples: unknown[] = [];
121
125
  for (let i = 0; i < 10; i++) {
122
- const result = (await generateFromSchema({
126
+ const result = await generateFromSchema({
123
127
  schema: mappedSchema,
124
- })) as Record<string, unknown>;
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: any[] = [];
137
+ const unmappedSamples: unknown[] = [];
129
138
  for (let i = 0; i < 10; i++) {
130
- const result = (await generateFromSchema({
139
+ const result = await generateFromSchema({
131
140
  schema: unmappedSchema,
132
- })) as Record<string, unknown>;
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: any[]): number => {
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: any[], validator: (sample: any) => boolean): boolean => {
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: () => any,
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 <T>(
268
+ samples: async (
255
269
  schema: JSONSchema7,
256
270
  count = 10,
257
- options?: any,
258
- ): Promise<T[]> => {
259
- const results: T[] = [];
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((await generateFromSchema({ schema, ...options })) as T);
275
+ results.push(await generateFromSchema({ schema, ...options }));
262
276
  }
263
277
  return results;
264
278
  },
265
279
 
266
- withSeed: async (schema: JSONSchema7, _seed?: number): Promise<any> => {
267
- // Note: faker.js doesn't support seeding in the same way,
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: any[]): Map<any, number> => {
276
- const dist = new Map<any, number>();
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: any[]): number => {
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: any,
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: any,
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: any) {
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 (error.context?.schemaPath) {
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: any[]): string {
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 (type !== "string") return type;
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 as Record<string, unknown>)[part];
401
+ current = Reflect.get(current, part);
402
402
  } else {
403
403
  throw new SchemaValidationError(
404
404
  "$.faker",