@ramiyohay/schema-faker 0.4.0 → 0.6.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/README.md CHANGED
@@ -31,6 +31,7 @@ Most fake data libraries generate random values field-by-field.
31
31
  - ✅ union
32
32
  - ✅ literal
33
33
  - ✅ date
34
+ - ✅ record
34
35
 
35
36
 
36
37
  ## Install
@@ -78,4 +79,13 @@ const user = generate(UserSchema, {
78
79
  });
79
80
 
80
81
  console.log(user);
82
+ ```
83
+
84
+ ### Strict mode
85
+
86
+ By default, unsupported Zod types are ignored.
87
+ Enable strict mode to throw an error instead:
88
+
89
+ ```ts
90
+ generate(schema, { strict: true });
81
91
  ```
@@ -1,4 +1,5 @@
1
1
  import { Random } from "./random";
2
2
  export type Context = {
3
3
  random: Random;
4
+ strict: boolean;
4
5
  };
package/dist/generate.js CHANGED
@@ -7,6 +7,7 @@ const deepMerge_1 = require("./core/deepMerge");
7
7
  function generate(schema, options = {}) {
8
8
  const ctx = {
9
9
  random: new random_1.Random(options.seed),
10
+ strict: options.strict || false,
10
11
  };
11
12
  // Function to generate a single value
12
13
  const makeOne = () => {
@@ -0,0 +1,2 @@
1
+ import { Context } from "../core/context";
2
+ export declare function generateRecord(schema: any, ctx: Context): Record<string, any>;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateRecord = generateRecord;
4
+ const parseZod_1 = require("../zod/parseZod");
5
+ function generateRecord(schema, ctx) {
6
+ const result = {};
7
+ const keySchema = schema._def.keyType;
8
+ const valueSchema = schema._def.valueType;
9
+ const count = ctx.random.int(1, 5); // Generate between 1 and 5 entries
10
+ for (let i = 0; i < count; i++) {
11
+ const key = (0, parseZod_1.parseZodSchema)(keySchema, ctx);
12
+ const value = (0, parseZod_1.parseZodSchema)(valueSchema, ctx);
13
+ result[String(key)] = value;
14
+ }
15
+ return result;
16
+ }
package/dist/types.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export type GenerateOptions = {
2
2
  seed?: number;
3
3
  count?: number;
4
+ strict?: boolean;
4
5
  overrides?: Record<string, any>;
5
6
  };
@@ -7,6 +7,7 @@ const boolean_1 = require("../generators/boolean");
7
7
  const array_1 = require("../generators/array");
8
8
  const enum_1 = require("../generators/enum");
9
9
  const date_1 = require("../generators/date");
10
+ const record_1 = require("../generators/record");
10
11
  function parseZodSchema(schema, ctx) {
11
12
  switch (schema._def.typeName) {
12
13
  case "ZodString": // String type
@@ -46,13 +47,19 @@ function parseZodSchema(schema, ctx) {
46
47
  const index = ctx.random.int(0, options.length - 1);
47
48
  return parseZodSchema(options[index], ctx);
48
49
  }
49
- case "ZodTuple": { // Tuple type , create array with fixed length and types
50
+ case "ZodRecord": // Record type
51
+ return (0, record_1.generateRecord)(schema, ctx);
52
+ case "ZodTuple": {
53
+ // Tuple type , create array with fixed length and types
50
54
  const items = schema._def.items;
51
55
  return items.map((itemSchema) => parseZodSchema(itemSchema, ctx));
52
56
  }
53
57
  case "ZodDate": // Date type
54
58
  return (0, date_1.generateDate)(schema, ctx);
55
59
  default:
56
- return null;
60
+ if (ctx.strict) {
61
+ throw new Error(`Unsupported Zod type: ${schema._def.typeName}`);
62
+ }
63
+ return undefined;
57
64
  }
58
65
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ramiyohay/schema-faker",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Generate fake data from Zod schemas in a deterministic way",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",