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