@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 +10 -0
- package/dist/core/context.d.ts +1 -0
- package/dist/generate.js +1 -0
- package/dist/generators/record.d.ts +2 -0
- package/dist/generators/record.js +16 -0
- package/dist/types.d.ts +1 -0
- package/dist/zod/parseZod.js +9 -2
- package/package.json +1 -1
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
|
```
|
package/dist/core/context.d.ts
CHANGED
package/dist/generate.js
CHANGED
|
@@ -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
package/dist/zod/parseZod.js
CHANGED
|
@@ -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 "
|
|
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
|
-
|
|
60
|
+
if (ctx.strict) {
|
|
61
|
+
throw new Error(`Unsupported Zod type: ${schema._def.typeName}`);
|
|
62
|
+
}
|
|
63
|
+
return undefined;
|
|
57
64
|
}
|
|
58
65
|
}
|