@mikemajesty/zod-mock-schema 1.0.13 → 1.0.14
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 +162 -57
- package/dist/__tests__/advanced-schemas.spec.d.ts +1 -0
- package/dist/__tests__/advanced-schemas.spec.js +151 -0
- package/dist/__tests__/advanced-schemas.spec.js.map +1 -0
- package/dist/__tests__/brazilian-formats.spec.d.ts +1 -0
- package/dist/__tests__/brazilian-formats.spec.js +207 -0
- package/dist/__tests__/brazilian-formats.spec.js.map +1 -0
- package/dist/__tests__/complex-intersections.spec.d.ts +1 -0
- package/dist/__tests__/complex-intersections.spec.js +178 -0
- package/dist/__tests__/complex-intersections.spec.js.map +1 -0
- package/dist/__tests__/edge-cases.spec.d.ts +1 -0
- package/dist/__tests__/edge-cases.spec.js +208 -0
- package/dist/__tests__/edge-cases.spec.js.map +1 -0
- package/dist/__tests__/faker-injection.spec.d.ts +1 -0
- package/dist/__tests__/faker-injection.spec.js +112 -0
- package/dist/__tests__/faker-injection.spec.js.map +1 -0
- package/dist/__tests__/overrides-all-types.spec.d.ts +1 -0
- package/dist/__tests__/overrides-all-types.spec.js +285 -0
- package/dist/__tests__/overrides-all-types.spec.js.map +1 -0
- package/dist/__tests__/overrides.spec.d.ts +1 -0
- package/dist/__tests__/overrides.spec.js +347 -0
- package/dist/__tests__/overrides.spec.js.map +1 -0
- package/dist/__tests__/unsupported-types.spec.d.ts +1 -0
- package/dist/__tests__/unsupported-types.spec.js +29 -0
- package/dist/__tests__/unsupported-types.spec.js.map +1 -0
- package/dist/__tests__/{zod-mock-schema.spec.d.ts → zod-types.spec.d.ts} +8 -8
- package/dist/__tests__/{zod-mock-schema.spec.js → zod-types.spec.js} +180 -5
- package/dist/__tests__/zod-types.spec.js.map +1 -0
- package/dist/constants.d.ts +46 -0
- package/dist/constants.js +87 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/types.d.ts +29 -15
- package/dist/zod-mock-schema.d.ts +16 -19
- package/dist/zod-mock-schema.js +297 -206
- package/dist/zod-mock-schema.js.map +1 -1
- package/package.json +12 -3
- package/dist/__tests__/zod-mock-schema.spec.js.map +0 -1
package/dist/zod-mock-schema.js
CHANGED
|
@@ -7,162 +7,156 @@ exports.ZodMockSchema = void 0;
|
|
|
7
7
|
const faker_1 = require("@faker-js/faker");
|
|
8
8
|
const randexp_1 = __importDefault(require("randexp"));
|
|
9
9
|
const zod_1 = __importDefault(require("zod"));
|
|
10
|
+
const constants_1 = require("./constants");
|
|
10
11
|
class ZodMockSchema {
|
|
11
12
|
constructor(schema) {
|
|
12
13
|
this.schema = schema;
|
|
13
|
-
this.faker = faker_1.
|
|
14
|
-
this.getRandomNumber = (max, sum = 1) => {
|
|
15
|
-
return Math.floor(Math.random() * (max + 1)) + sum;
|
|
16
|
-
};
|
|
17
|
-
this.validCPF = [
|
|
18
|
-
"12345678909",
|
|
19
|
-
"52998224725",
|
|
20
|
-
"11144477735",
|
|
21
|
-
"86473782905",
|
|
22
|
-
"40723177808"
|
|
23
|
-
];
|
|
24
|
-
this.validCNPJ = [
|
|
25
|
-
"11222333000181",
|
|
26
|
-
"60676960000106",
|
|
27
|
-
"06990590000123",
|
|
28
|
-
"33112511000130",
|
|
29
|
-
"00111222000189"
|
|
30
|
-
];
|
|
31
|
-
this.validRG = [
|
|
32
|
-
"238192611",
|
|
33
|
-
"110209394",
|
|
34
|
-
"202455518",
|
|
35
|
-
"437062053",
|
|
36
|
-
"207022689"
|
|
37
|
-
];
|
|
38
|
-
this.validPhoneBR = [
|
|
39
|
-
"11987654321",
|
|
40
|
-
"21998765432",
|
|
41
|
-
"31991234567",
|
|
42
|
-
"41992345678",
|
|
43
|
-
"51993456789"
|
|
44
|
-
];
|
|
45
|
-
this.validCEP = [
|
|
46
|
-
"01001000",
|
|
47
|
-
"20040010",
|
|
48
|
-
"30130010",
|
|
49
|
-
"80010000",
|
|
50
|
-
"90010000"
|
|
51
|
-
];
|
|
52
|
-
}
|
|
53
|
-
generate(overrides) {
|
|
54
|
-
const mockData = this.generateMockData(this.schema);
|
|
55
|
-
const merged = Object.assign(Object.assign({}, mockData), overrides === null || overrides === void 0 ? void 0 : overrides.overrides);
|
|
56
|
-
return this.schema.parse(merged);
|
|
14
|
+
this.faker = new faker_1.Faker({ locale: faker_1.en });
|
|
57
15
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
});
|
|
16
|
+
seed(seed) {
|
|
17
|
+
const seedArray = Array.isArray(seed) ? seed : [seed];
|
|
18
|
+
this.faker.seed(seedArray);
|
|
19
|
+
return this;
|
|
63
20
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return this.getBralizilianMockedData().cpf;
|
|
70
|
-
}
|
|
71
|
-
if ((meta === null || meta === void 0 ? void 0 : meta.format) === 'cnpj') {
|
|
72
|
-
return this.getBralizilianMockedData().cnpj;
|
|
21
|
+
generate(overrides) {
|
|
22
|
+
const originalFaker = this.faker;
|
|
23
|
+
try {
|
|
24
|
+
if (overrides === null || overrides === void 0 ? void 0 : overrides.faker) {
|
|
25
|
+
this.faker = overrides.faker;
|
|
73
26
|
}
|
|
74
|
-
|
|
75
|
-
|
|
27
|
+
const mockData = this.generateFromSchema(this.schema);
|
|
28
|
+
if (!this.hasCustomOverrides(overrides)) {
|
|
29
|
+
return this.schema.parse(mockData);
|
|
76
30
|
}
|
|
77
|
-
if ((
|
|
78
|
-
|
|
31
|
+
if (this.isPlainObject(overrides.overrides) && this.isPlainObject(mockData)) {
|
|
32
|
+
const merged = Object.assign(Object.assign({}, mockData), overrides.overrides);
|
|
33
|
+
return this.schema.parse(merged);
|
|
79
34
|
}
|
|
80
|
-
|
|
81
|
-
|
|
35
|
+
return this.schema.parse(overrides.overrides);
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
this.faker = originalFaker;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
generateMany(count, options = {}) {
|
|
42
|
+
return Array.from({ length: count }, () => this.generate(options));
|
|
43
|
+
}
|
|
44
|
+
generateFromSchema(schema) {
|
|
45
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
46
|
+
if (this.hasMetadata(schema)) {
|
|
47
|
+
const meta = schema.meta();
|
|
48
|
+
const brazilianFormats = {
|
|
49
|
+
[constants_1.Constants.FORMAT_CPF]: 'cpf',
|
|
50
|
+
[constants_1.Constants.FORMAT_CNPJ]: 'cnpj',
|
|
51
|
+
[constants_1.Constants.FORMAT_RG]: 'rg',
|
|
52
|
+
[constants_1.Constants.FORMAT_PHONE_BR]: 'phoneBR',
|
|
53
|
+
[constants_1.Constants.FORMAT_CEP]: 'cep'
|
|
54
|
+
};
|
|
55
|
+
const format = meta === null || meta === void 0 ? void 0 : meta.format;
|
|
56
|
+
if (format && brazilianFormats[format]) {
|
|
57
|
+
return this.getBrazilianMockValue(brazilianFormats[format]);
|
|
82
58
|
}
|
|
83
59
|
}
|
|
84
60
|
if (schema instanceof zod_1.default.ZodUnion) {
|
|
85
61
|
const options = schema.def.options;
|
|
86
|
-
const
|
|
87
|
-
return this.
|
|
62
|
+
const selectedOption = this.faker.helpers.arrayElement(options);
|
|
63
|
+
return this.generateFromSchema(selectedOption);
|
|
88
64
|
}
|
|
89
65
|
if (schema instanceof zod_1.default.ZodIntersection) {
|
|
90
|
-
const
|
|
91
|
-
const
|
|
92
|
-
return Object.assign(Object.assign({},
|
|
66
|
+
const leftSchema = this.generateFromSchema(schema.def.left);
|
|
67
|
+
const rightSchema = this.generateFromSchema(schema.def.right);
|
|
68
|
+
return Object.assign(Object.assign({}, leftSchema), rightSchema);
|
|
93
69
|
}
|
|
94
70
|
if (schema instanceof zod_1.default.ZodEmail)
|
|
95
|
-
return
|
|
71
|
+
return this.faker.internet.email();
|
|
96
72
|
if (schema instanceof zod_1.default.ZodURL)
|
|
97
|
-
return
|
|
73
|
+
return this.faker.internet.url();
|
|
98
74
|
if (schema instanceof zod_1.default.ZodUUID)
|
|
99
|
-
return
|
|
75
|
+
return this.faker.string.uuid();
|
|
100
76
|
if (schema instanceof zod_1.default.ZodString) {
|
|
101
|
-
return this.
|
|
77
|
+
return this.generateStringValue(schema);
|
|
102
78
|
}
|
|
103
79
|
;
|
|
104
80
|
if (schema instanceof zod_1.default.ZodNumber) {
|
|
105
|
-
return this.
|
|
81
|
+
return this.generateNumberValue(schema);
|
|
106
82
|
}
|
|
107
83
|
if (schema instanceof zod_1.default.ZodBoolean)
|
|
108
|
-
return
|
|
109
|
-
if (schema instanceof zod_1.default.ZodDate)
|
|
110
|
-
|
|
84
|
+
return this.faker.datatype.boolean();
|
|
85
|
+
if (schema instanceof zod_1.default.ZodDate) {
|
|
86
|
+
const constraints = this.getChecks(schema);
|
|
87
|
+
const minConstraint = constraints.find((c) => c._zod.def.check === 'greater_than');
|
|
88
|
+
const maxConstraint = constraints.find((c) => c._zod.def.check === 'less_than');
|
|
89
|
+
const getDateBound = (constraint) => {
|
|
90
|
+
if (!constraint)
|
|
91
|
+
return undefined;
|
|
92
|
+
return new Date(constraint._zod.def.value);
|
|
93
|
+
};
|
|
94
|
+
const minDateBound = getDateBound(minConstraint);
|
|
95
|
+
const maxDateBound = getDateBound(maxConstraint);
|
|
96
|
+
if (minDateBound && maxDateBound) {
|
|
97
|
+
return this.faker.date.between({ from: minDateBound, to: maxDateBound });
|
|
98
|
+
}
|
|
99
|
+
if (minDateBound) {
|
|
100
|
+
return this.faker.date.future({ refDate: minDateBound });
|
|
101
|
+
}
|
|
102
|
+
if (maxDateBound) {
|
|
103
|
+
return this.faker.date.past({ refDate: maxDateBound });
|
|
104
|
+
}
|
|
105
|
+
return this.faker.date.recent();
|
|
106
|
+
}
|
|
111
107
|
if (schema instanceof zod_1.default.ZodArray) {
|
|
112
|
-
const
|
|
113
|
-
const
|
|
114
|
-
const
|
|
115
|
-
const
|
|
116
|
-
|
|
108
|
+
const checks = this.getChecks(schema);
|
|
109
|
+
const minCheck = checks.find((c) => c._zod.def.check === 'min_length');
|
|
110
|
+
const maxCheck = checks.find((c) => c._zod.def.check === 'max_length');
|
|
111
|
+
const minLength = (_b = (_a = minCheck === null || minCheck === void 0 ? void 0 : minCheck._zod.def) === null || _a === void 0 ? void 0 : _a.minimum) !== null && _b !== void 0 ? _b : constants_1.Constants.DEFAULT_ARRAY_MIN_LENGTH;
|
|
112
|
+
const maxLength = (_d = (_c = maxCheck === null || maxCheck === void 0 ? void 0 : maxCheck._zod.def) === null || _c === void 0 ? void 0 : _c.maximum) !== null && _d !== void 0 ? _d : constants_1.Constants.DEFAULT_ARRAY_MAX_LENGTH;
|
|
113
|
+
const arrayLength = this.faker.number.int({ min: minLength, max: maxLength });
|
|
114
|
+
return Array.from({ length: arrayLength }, () => this.generateFromSchema(schema.element));
|
|
117
115
|
}
|
|
118
116
|
if (schema instanceof zod_1.default.ZodObject) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
const value = shape[key].def.defaultValue;
|
|
124
|
-
if (value) {
|
|
125
|
-
mockData[key] = value;
|
|
126
|
-
continue;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
mockData[key] = this.generateMockData(fieldSchema);
|
|
130
|
-
}
|
|
131
|
-
return mockData;
|
|
117
|
+
return Object.fromEntries(Object.entries(schema.shape).map(([key, propertySchema]) => [
|
|
118
|
+
key,
|
|
119
|
+
this.generateFromSchema(propertySchema)
|
|
120
|
+
]));
|
|
132
121
|
}
|
|
133
122
|
if (schema instanceof zod_1.default.ZodEnum) {
|
|
134
|
-
return
|
|
123
|
+
return this.faker.helpers.arrayElement(schema.options);
|
|
135
124
|
}
|
|
136
125
|
if (schema instanceof zod_1.default.ZodRecord) {
|
|
137
|
-
const
|
|
138
|
-
const
|
|
139
|
-
if (
|
|
140
|
-
const
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
-
return selectedKeys.reduce((record, key) => (Object.assign(Object.assign({}, record), { [key]: this.generateMockData(valueSchema) })), {});
|
|
126
|
+
const recordKeySchema = schema.keyType;
|
|
127
|
+
const recordValueSchema = schema.valueType;
|
|
128
|
+
if (recordKeySchema instanceof zod_1.default.ZodEnum) {
|
|
129
|
+
const enumOptions = recordKeySchema.options;
|
|
130
|
+
const selectedKeys = this.faker.helpers.arrayElements(enumOptions, enumOptions.length);
|
|
131
|
+
return selectedKeys.reduce((recordData, propertyKey) => (Object.assign(Object.assign({}, recordData), { [propertyKey]: this.generateFromSchema(recordValueSchema) })), {});
|
|
144
132
|
}
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
133
|
+
const propertiesCount = this.faker.number.int({ min: constants_1.Constants.DEFAULT_RECORD_MIN_PROPERTIES, max: constants_1.Constants.DEFAULT_RECORD_MAX_PROPERTIES });
|
|
134
|
+
const generateRecordKey = (index) => {
|
|
135
|
+
if (recordKeySchema instanceof zod_1.default.ZodString)
|
|
136
|
+
return this.generateStringValue(recordKeySchema);
|
|
137
|
+
if (recordKeySchema instanceof zod_1.default.ZodNumber)
|
|
138
|
+
return this.generateNumberValue(recordKeySchema).toString();
|
|
139
|
+
return `key${index}`;
|
|
140
|
+
};
|
|
141
|
+
return Array.from({ length: propertiesCount })
|
|
142
|
+
.reduce((recordData, _, index) => (Object.assign(Object.assign({}, recordData), { [generateRecordKey(index)]: this.generateFromSchema(recordValueSchema) })), {});
|
|
155
143
|
}
|
|
156
144
|
if (schema instanceof zod_1.default.ZodOptional)
|
|
157
|
-
return this.
|
|
145
|
+
return this.generateFromSchema(schema.unwrap());
|
|
158
146
|
if (schema instanceof zod_1.default.ZodNullable)
|
|
159
|
-
return this.
|
|
160
|
-
if (schema instanceof zod_1.default.ZodDefault)
|
|
161
|
-
|
|
147
|
+
return this.generateFromSchema(schema.unwrap());
|
|
148
|
+
if (schema instanceof zod_1.default.ZodDefault) {
|
|
149
|
+
const defaultValueOrFunction = schema.def.defaultValue;
|
|
150
|
+
if (typeof defaultValueOrFunction === 'function') {
|
|
151
|
+
return defaultValueOrFunction();
|
|
152
|
+
}
|
|
153
|
+
return defaultValueOrFunction;
|
|
154
|
+
}
|
|
162
155
|
if (schema instanceof zod_1.default.ZodLazy)
|
|
163
|
-
return this.
|
|
164
|
-
if (
|
|
165
|
-
return this.
|
|
156
|
+
return this.generateFromSchema(schema.def.getter());
|
|
157
|
+
if ([zod_1.default.ZodAny, zod_1.default.ZodUnknown].some(Type => schema instanceof Type)) {
|
|
158
|
+
return this.generateUnknownTypeValue();
|
|
159
|
+
}
|
|
166
160
|
if (schema instanceof zod_1.default.ZodVoid)
|
|
167
161
|
return undefined;
|
|
168
162
|
if (schema instanceof zod_1.default.ZodNull)
|
|
@@ -170,108 +164,205 @@ class ZodMockSchema {
|
|
|
170
164
|
if (schema instanceof zod_1.default.ZodLiteral)
|
|
171
165
|
return schema.value;
|
|
172
166
|
if (schema instanceof zod_1.default.ZodPipe) {
|
|
173
|
-
|
|
174
|
-
return pipe;
|
|
167
|
+
return this.generateFromSchema(schema.def.in);
|
|
175
168
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
169
|
+
if (schema instanceof zod_1.default.ZodSet) {
|
|
170
|
+
const checks = this.getChecks(schema);
|
|
171
|
+
const minConstraint = checks.find((c) => c._zod.def.check === 'min_size');
|
|
172
|
+
const maxConstraint = checks.find((c) => c._zod.def.check === 'max_size');
|
|
173
|
+
const minSetSize = (_f = (_e = minConstraint === null || minConstraint === void 0 ? void 0 : minConstraint._zod.def) === null || _e === void 0 ? void 0 : _e.minimum) !== null && _f !== void 0 ? _f : constants_1.Constants.DEFAULT_SET_MIN_SIZE;
|
|
174
|
+
const maxSetSize = (_h = (_g = maxConstraint === null || maxConstraint === void 0 ? void 0 : maxConstraint._zod.def) === null || _g === void 0 ? void 0 : _g.maximum) !== null && _h !== void 0 ? _h : constants_1.Constants.DEFAULT_SET_MAX_SIZE;
|
|
175
|
+
const targetSize = this.faker.number.int({ min: minSetSize, max: maxSetSize });
|
|
176
|
+
const items = new Set();
|
|
177
|
+
const setWithDef = schema;
|
|
178
|
+
for (let i = 0; i < targetSize * 3; i++) {
|
|
179
|
+
items.add(this.generateFromSchema(setWithDef.def.valueType));
|
|
180
|
+
if (items.size >= targetSize)
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
return items;
|
|
184
|
+
}
|
|
185
|
+
if (schema instanceof zod_1.default.ZodMap) {
|
|
186
|
+
const mapSize = constants_1.Constants.DEFAULT_MAP_SIZE;
|
|
187
|
+
const mapData = new Map();
|
|
188
|
+
const mapWithDef = schema;
|
|
189
|
+
for (let i = 0; i < mapSize; i++) {
|
|
190
|
+
const key = this.generateFromSchema(mapWithDef.def.keyType);
|
|
191
|
+
const value = this.generateFromSchema(mapWithDef.def.valueType);
|
|
192
|
+
mapData.set(key, value);
|
|
193
|
+
}
|
|
194
|
+
return mapData;
|
|
184
195
|
}
|
|
185
|
-
if (
|
|
186
|
-
const
|
|
187
|
-
const
|
|
188
|
-
|
|
196
|
+
if (schema instanceof zod_1.default.ZodTuple) {
|
|
197
|
+
const tupleWithDef = schema;
|
|
198
|
+
const fixedTupleItems = (_k = (_j = tupleWithDef.def.items) === null || _j === void 0 ? void 0 : _j.map((itemSchema) => this.generateFromSchema(itemSchema))) !== null && _k !== void 0 ? _k : [];
|
|
199
|
+
if (!tupleWithDef.def.rest) {
|
|
200
|
+
return fixedTupleItems;
|
|
201
|
+
}
|
|
202
|
+
const restTupleItems = Array.from({ length: this.faker.number.int({ min: constants_1.Constants.DEFAULT_TUPLE_REST_MIN, max: constants_1.Constants.DEFAULT_TUPLE_REST_MAX }) }, () => this.generateFromSchema(tupleWithDef.def.rest));
|
|
203
|
+
return [...fixedTupleItems, ...restTupleItems];
|
|
189
204
|
}
|
|
190
|
-
if (
|
|
191
|
-
|
|
205
|
+
if (schema instanceof zod_1.default.ZodFunction) {
|
|
206
|
+
const functionWithDef = schema;
|
|
207
|
+
return (() => {
|
|
208
|
+
return this.generateFromSchema(functionWithDef.def.output);
|
|
209
|
+
});
|
|
192
210
|
}
|
|
193
|
-
if (
|
|
194
|
-
return
|
|
211
|
+
if (this.isBrandedType(schema)) {
|
|
212
|
+
return this.generateFromSchema(schema.unwrap());
|
|
195
213
|
}
|
|
196
|
-
if (
|
|
197
|
-
return
|
|
214
|
+
if (schema instanceof zod_1.default.ZodReadonly) {
|
|
215
|
+
return this.generateFromSchema(schema.def.innerType);
|
|
198
216
|
}
|
|
199
|
-
if (
|
|
200
|
-
|
|
217
|
+
if (schema instanceof zod_1.default.ZodDiscriminatedUnion) {
|
|
218
|
+
const unionOptions = Array.from(schema.def.options.values());
|
|
219
|
+
const selectedOption = this.faker.helpers.arrayElement(unionOptions);
|
|
220
|
+
return this.generateFromSchema(selectedOption);
|
|
201
221
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
222
|
+
if (this.isEffectsType(schema)) {
|
|
223
|
+
const inputValue = this.generateFromSchema(schema.def.schema);
|
|
224
|
+
if (((_l = schema.def.effect) === null || _l === void 0 ? void 0 : _l.type) === constants_1.Constants.EFFECT_TYPE_TRANSFORM) {
|
|
225
|
+
try {
|
|
226
|
+
return schema.def.effect.transform(inputValue, { addIssue: () => undefined, path: [] });
|
|
227
|
+
}
|
|
228
|
+
catch (_o) {
|
|
229
|
+
return inputValue;
|
|
230
|
+
}
|
|
208
231
|
}
|
|
209
|
-
|
|
232
|
+
return inputValue;
|
|
233
|
+
}
|
|
234
|
+
if (schema instanceof zod_1.default.ZodBigInt) {
|
|
235
|
+
const checks = this.getChecks(schema);
|
|
236
|
+
const minConstraint = checks.find((c) => c._zod.def.check === 'greater_than');
|
|
237
|
+
const maxConstraint = checks.find((c) => c._zod.def.check === 'less_than');
|
|
238
|
+
const getBigIntBound = (constraint, defaultValue) => {
|
|
239
|
+
if (!constraint)
|
|
240
|
+
return defaultValue;
|
|
241
|
+
return BigInt(constraint._zod.def.value);
|
|
242
|
+
};
|
|
243
|
+
const minBigInt = getBigIntBound(minConstraint, constants_1.Constants.DEFAULT_BIGINT_MIN);
|
|
244
|
+
const maxBigInt = getBigIntBound(maxConstraint, constants_1.Constants.DEFAULT_BIGINT_MAX);
|
|
245
|
+
const minNumber = minBigInt < maxBigInt ? Number(minBigInt) : Number(maxBigInt);
|
|
246
|
+
const maxNumber = minBigInt < maxBigInt ? Number(maxBigInt) : Number(minBigInt);
|
|
247
|
+
const generatedNumber = this.faker.number.int({ min: minNumber, max: maxNumber });
|
|
248
|
+
return BigInt(generatedNumber);
|
|
249
|
+
}
|
|
250
|
+
if (schema instanceof zod_1.default.ZodNaN) {
|
|
251
|
+
return NaN;
|
|
252
|
+
}
|
|
253
|
+
const schemaWithDef = schema;
|
|
254
|
+
const schemaType = ((_m = schemaWithDef._def) === null || _m === void 0 ? void 0 : _m.typeName) || 'Unknown Type';
|
|
255
|
+
throw new Error(`Unsupported Zod type: ${schemaType}\n\n` +
|
|
256
|
+
`This schema type is not currently supported by zod-mock-schema.\n` +
|
|
257
|
+
`If you need support for this type, please open an issue at:\n` +
|
|
258
|
+
`https://github.com/mikemajesty/zod-mock-schema/issues`);
|
|
210
259
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
const {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
260
|
+
generateStringValue(schema) {
|
|
261
|
+
var _a, _b, _c;
|
|
262
|
+
const { minLength, maxLength, format } = schema;
|
|
263
|
+
const formatGenerators = {
|
|
264
|
+
[constants_1.Constants.FORMAT_EMAIL]: this.faker.internet.email,
|
|
265
|
+
[constants_1.Constants.FORMAT_URL]: this.faker.internet.url,
|
|
266
|
+
[constants_1.Constants.FORMAT_UUID]: this.faker.string.uuid,
|
|
267
|
+
[constants_1.Constants.FORMAT_ULID]: this.faker.string.ulid
|
|
268
|
+
};
|
|
269
|
+
const simpleFormatResult = (_a = formatGenerators[format]) === null || _a === void 0 ? void 0 : _a.call(formatGenerators);
|
|
270
|
+
if (simpleFormatResult)
|
|
271
|
+
return simpleFormatResult;
|
|
272
|
+
if (format === constants_1.Constants.FORMAT_REGEX) {
|
|
273
|
+
const stringWithDef = schema;
|
|
274
|
+
const regexCheck = (_b = stringWithDef.def.checks) === null || _b === void 0 ? void 0 : _b.find((c) => c._zod.def.format === 'regex');
|
|
275
|
+
const regexPattern = (_c = regexCheck === null || regexCheck === void 0 ? void 0 : regexCheck._zod.def) === null || _c === void 0 ? void 0 : _c.pattern;
|
|
276
|
+
const regexGenerator = new randexp_1.default(regexPattern);
|
|
277
|
+
regexGenerator.randInt = (min, max) => this.faker.number.int({ min, max });
|
|
278
|
+
if (maxLength) {
|
|
279
|
+
regexGenerator.max = maxLength;
|
|
280
|
+
}
|
|
281
|
+
return regexGenerator.gen();
|
|
220
282
|
}
|
|
221
|
-
if (
|
|
222
|
-
|
|
223
|
-
return faker_1.faker.number.int({
|
|
224
|
-
min: minValue !== null && minValue !== void 0 ? minValue : min,
|
|
225
|
-
max: maxValue !== null && maxValue !== void 0 ? maxValue : this.getRandomNumber(1000, min)
|
|
226
|
-
});
|
|
283
|
+
if (['date-time', 'datetime'].includes(format)) {
|
|
284
|
+
return this.faker.date.recent().toISOString();
|
|
227
285
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
286
|
+
const minStringLength = minLength !== null && minLength !== void 0 ? minLength : constants_1.Constants.DEFAULT_STRING_MIN_LENGTH;
|
|
287
|
+
const maxStringLength = maxLength !== null && maxLength !== void 0 ? maxLength : constants_1.Constants.DEFAULT_STRING_MAX_LENGTH;
|
|
288
|
+
const stringLength = this.faker.number.int({ min: minStringLength, max: maxStringLength });
|
|
289
|
+
return this.faker.string.alpha({ length: stringLength });
|
|
290
|
+
}
|
|
291
|
+
generateNumberValue(schema) {
|
|
292
|
+
const { minValue, maxValue, format } = schema;
|
|
293
|
+
const hasDecimalBounds = [minValue, maxValue].some(v => !Number.isInteger(v));
|
|
294
|
+
const hasFloatFormat = format !== null && constants_1.Constants.FLOAT_FORMATS.includes(format);
|
|
295
|
+
const isFloat = hasFloatFormat || hasDecimalBounds;
|
|
296
|
+
const { min, max } = this.getNormalizedBounds(minValue, maxValue, isFloat);
|
|
297
|
+
if (isFloat) {
|
|
298
|
+
return this.faker.number.float({ min, max });
|
|
299
|
+
}
|
|
300
|
+
return this.faker.number.int({ min, max });
|
|
301
|
+
}
|
|
302
|
+
getBrazilianMockValue(type) {
|
|
303
|
+
return this.faker.helpers.arrayElement(constants_1.Constants.BRAZILIAN_MOCK_DATA[type]);
|
|
304
|
+
}
|
|
305
|
+
getChecks(schema) {
|
|
306
|
+
return schema.def.checks || [];
|
|
307
|
+
}
|
|
308
|
+
getNormalizedBounds(minValue, maxValue, isFloat = false) {
|
|
309
|
+
const isInfinity = (v) => v === Infinity || v === -Infinity;
|
|
310
|
+
const defaultMin = isFloat ? constants_1.Constants.DEFAULT_FLOAT_MIN : constants_1.Constants.DEFAULT_INT_MIN;
|
|
311
|
+
const defaultMax = isFloat ? constants_1.Constants.DEFAULT_FLOAT_MAX : constants_1.Constants.DEFAULT_INT_MAX;
|
|
312
|
+
const getBound = (value, defaultValue) => {
|
|
313
|
+
if (value === null || isInfinity(value))
|
|
314
|
+
return defaultValue;
|
|
315
|
+
return value;
|
|
316
|
+
};
|
|
317
|
+
const minBound = getBound(minValue, defaultMin);
|
|
318
|
+
const maxBound = getBound(maxValue, defaultMax);
|
|
319
|
+
if (minBound > maxBound) {
|
|
320
|
+
return { min: maxBound, max: minBound };
|
|
234
321
|
}
|
|
235
|
-
|
|
236
|
-
return faker_1.faker.number.int({
|
|
237
|
-
min: minValue !== null && minValue !== void 0 ? minValue : min,
|
|
238
|
-
max: maxValue !== null && maxValue !== void 0 ? maxValue : this.getRandomNumber(1000, min)
|
|
239
|
-
});
|
|
322
|
+
return { min: minBound, max: maxBound };
|
|
240
323
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
324
|
+
generateUnknownTypeValue() {
|
|
325
|
+
const typeGenerators = {
|
|
326
|
+
string: () => this.faker.lorem.words(constants_1.Constants.DEFAULT_ANY_WORDS_COUNT),
|
|
327
|
+
number: () => this.faker.number.int(constants_1.Constants.DEFAULT_ANY_NUMBER_MAX),
|
|
328
|
+
boolean: () => this.faker.datatype.boolean(),
|
|
329
|
+
date: () => this.faker.date.recent(),
|
|
330
|
+
array: () => Array.from({ length: constants_1.Constants.DEFAULT_ANY_ARRAY_LENGTH }, () => this.faker.lorem.word()),
|
|
331
|
+
object: () => ({ [this.faker.lorem.word()]: this.faker.lorem.words(constants_1.Constants.DEFAULT_ANY_WORDS_COUNT) })
|
|
248
332
|
};
|
|
333
|
+
const selectedType = this.faker.helpers.arrayElement(Object.keys(typeGenerators));
|
|
334
|
+
return typeGenerators[selectedType]();
|
|
335
|
+
}
|
|
336
|
+
isPlainObject(value) {
|
|
337
|
+
return typeof value === 'object' && [
|
|
338
|
+
value !== null,
|
|
339
|
+
!Array.isArray(value),
|
|
340
|
+
!(value instanceof Date),
|
|
341
|
+
!(value instanceof Set),
|
|
342
|
+
!(value instanceof Map)
|
|
343
|
+
].every(Boolean);
|
|
344
|
+
}
|
|
345
|
+
hasCustomOverrides(overrides) {
|
|
346
|
+
return !!overrides && 'overrides' in overrides;
|
|
347
|
+
}
|
|
348
|
+
isSchemaObject(schema) {
|
|
349
|
+
return typeof schema === constants_1.Constants.TYPE_OBJECT && schema !== null;
|
|
350
|
+
}
|
|
351
|
+
hasMetadata(schema) {
|
|
352
|
+
if (!this.isSchemaObject(schema))
|
|
353
|
+
return false;
|
|
354
|
+
return 'meta' in schema && typeof schema.meta === 'function';
|
|
249
355
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
const minSafe = this.getRandomNumber(1000);
|
|
255
|
-
const min = isMinInfinity ? minSafe : (minValue || 1);
|
|
256
|
-
const max = isMaxInfinity ? this.getRandomNumber(1000, minSafe) : (maxValue || this.getRandomNumber(1000, minSafe));
|
|
257
|
-
return { min, max };
|
|
356
|
+
isBrandedType(schema) {
|
|
357
|
+
if (!this.isSchemaObject(schema))
|
|
358
|
+
return false;
|
|
359
|
+
return 'unwrap' in schema && typeof schema.unwrap === constants_1.Constants.TYPE_FUNCTION;
|
|
258
360
|
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
if (randomType === 'number')
|
|
265
|
-
return faker_1.faker.number.int(100);
|
|
266
|
-
if (randomType === 'boolean')
|
|
267
|
-
return faker_1.faker.datatype.boolean();
|
|
268
|
-
if (randomType === 'date')
|
|
269
|
-
return faker_1.faker.date.recent();
|
|
270
|
-
if (randomType === 'array')
|
|
271
|
-
return Array.from({ length: 2 }, () => faker_1.faker.lorem.word());
|
|
272
|
-
if (randomType === 'object')
|
|
273
|
-
return { [faker_1.faker.lorem.word()]: faker_1.faker.lorem.words(2) };
|
|
274
|
-
return null;
|
|
361
|
+
isEffectsType(schema) {
|
|
362
|
+
if (!this.isSchemaObject(schema))
|
|
363
|
+
return false;
|
|
364
|
+
const def = schema.def;
|
|
365
|
+
return (def === null || def === void 0 ? void 0 : def.typeName) === constants_1.Constants.ZOD_TYPE_EFFECTS;
|
|
275
366
|
}
|
|
276
367
|
}
|
|
277
368
|
exports.ZodMockSchema = ZodMockSchema;
|