@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.
Files changed (42) hide show
  1. package/README.md +162 -57
  2. package/dist/__tests__/advanced-schemas.spec.d.ts +1 -0
  3. package/dist/__tests__/advanced-schemas.spec.js +149 -0
  4. package/dist/__tests__/advanced-schemas.spec.js.map +1 -0
  5. package/dist/__tests__/brazilian-formats.spec.d.ts +1 -0
  6. package/dist/__tests__/brazilian-formats.spec.js +205 -0
  7. package/dist/__tests__/brazilian-formats.spec.js.map +1 -0
  8. package/dist/__tests__/complex-intersections.spec.d.ts +1 -0
  9. package/dist/__tests__/complex-intersections.spec.js +176 -0
  10. package/dist/__tests__/complex-intersections.spec.js.map +1 -0
  11. package/dist/__tests__/edge-cases.spec.d.ts +1 -0
  12. package/dist/__tests__/edge-cases.spec.js +206 -0
  13. package/dist/__tests__/edge-cases.spec.js.map +1 -0
  14. package/dist/__tests__/faker-injection.spec.d.ts +1 -0
  15. package/dist/__tests__/faker-injection.spec.js +110 -0
  16. package/dist/__tests__/faker-injection.spec.js.map +1 -0
  17. package/dist/__tests__/overrides-all-types.spec.d.ts +1 -0
  18. package/dist/__tests__/overrides-all-types.spec.js +283 -0
  19. package/dist/__tests__/overrides-all-types.spec.js.map +1 -0
  20. package/dist/__tests__/overrides.spec.d.ts +1 -0
  21. package/dist/__tests__/overrides.spec.js +345 -0
  22. package/dist/__tests__/overrides.spec.js.map +1 -0
  23. package/dist/__tests__/unsupported-types.spec.d.ts +1 -0
  24. package/dist/__tests__/unsupported-types.spec.js +27 -0
  25. package/dist/__tests__/unsupported-types.spec.js.map +1 -0
  26. package/dist/__tests__/{zod-mock-schema.spec.d.ts → zod-types.spec.d.ts} +8 -8
  27. package/dist/__tests__/zod-types.spec.js +526 -0
  28. package/dist/__tests__/zod-types.spec.js.map +1 -0
  29. package/dist/constants.d.ts +46 -0
  30. package/dist/constants.js +83 -0
  31. package/dist/constants.js.map +1 -0
  32. package/dist/index.d.ts +2 -2
  33. package/dist/index.js +1 -5
  34. package/dist/index.js.map +1 -1
  35. package/dist/types.d.ts +29 -15
  36. package/dist/types.js +1 -2
  37. package/dist/zod-mock-schema.d.ts +17 -20
  38. package/dist/zod-mock-schema.js +324 -236
  39. package/dist/zod-mock-schema.js.map +1 -1
  40. package/package.json +14 -4
  41. package/dist/__tests__/zod-mock-schema.spec.js +0 -357
  42. package/dist/__tests__/zod-mock-schema.spec.js.map +0 -1
@@ -1,278 +1,366 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
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 = faker_1.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
- generateMany(count, options = {}) {
59
- return Array.from({ length: count }, () => {
60
- const data = this.generate(options);
61
- return data;
62
- });
10
+ seed(seed) {
11
+ const seedArray = Array.isArray(seed) ? seed : [seed];
12
+ this.faker.seed(seedArray);
13
+ return this;
63
14
  }
64
- generateMockData(schema) {
65
- var _a, _b, _c, _d, _e;
66
- if ('meta' in schema && (schema === null || schema === void 0 ? void 0 : schema.meta)) {
67
- const meta = (schema === null || schema === void 0 ? void 0 : schema.meta()) || { format: null };
68
- if ((meta === null || meta === void 0 ? void 0 : meta.format) === 'cpf') {
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
- if ((meta === null || meta === void 0 ? void 0 : meta.format) === 'rg') {
75
- return this.getBralizilianMockedData().rg;
21
+ const mockData = this.generateFromSchema(this.schema);
22
+ if (!this.hasCustomOverrides(overrides)) {
23
+ return this.schema.parse(mockData);
76
24
  }
77
- if ((meta === null || meta === void 0 ? void 0 : meta.format) === 'phoneBR') {
78
- return this.getBralizilianMockedData().phoneBR;
25
+ if (this.isPlainObject(overrides.overrides) && this.isPlainObject(mockData)) {
26
+ const merged = { ...mockData, ...overrides.overrides };
27
+ return this.schema.parse(merged);
79
28
  }
80
- if ((meta === null || meta === void 0 ? void 0 : meta.format) === 'cep') {
81
- return this.getBralizilianMockedData().cep;
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 zod_1.default.ZodUnion) {
53
+ if (schema instanceof z.ZodUnion) {
85
54
  const options = schema.def.options;
86
- const randomOption = faker_1.faker.helpers.arrayElement(options);
87
- return this.generateMockData(randomOption);
55
+ const selectedOption = this.faker.helpers.arrayElement(options);
56
+ return this.generateFromSchema(selectedOption);
88
57
  }
89
- if (schema instanceof zod_1.default.ZodIntersection) {
90
- const left = this.generateMockData(schema.def.left);
91
- const right = this.generateMockData(schema.def.right);
92
- return Object.assign(Object.assign({}, left), right);
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 zod_1.default.ZodEmail)
95
- return faker_1.faker.internet.email();
96
- if (schema instanceof zod_1.default.ZodURL)
97
- return faker_1.faker.internet.url();
98
- if (schema instanceof zod_1.default.ZodUUID)
99
- return faker_1.faker.string.uuid();
100
- if (schema instanceof zod_1.default.ZodString) {
101
- return this.generateString(schema);
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 zod_1.default.ZodNumber) {
105
- return this.generateNumber(schema);
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 zod_1.default.ZodObject) {
119
- const mockData = {};
120
- const shape = schema.shape;
121
- for (const [key, fieldSchema] of Object.entries(shape)) {
122
- if (fieldSchema.type === 'default') {
123
- const value = shape[key].def.defaultValue;
124
- if (value) {
125
- mockData[key] = value;
126
- continue;
127
- }
128
- }
129
- mockData[key] = this.generateMockData(fieldSchema);
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 mockData;
98
+ return this.faker.date.recent();
132
99
  }
133
- if (schema instanceof zod_1.default.ZodEnum) {
134
- return faker_1.faker.helpers.arrayElement(schema.options);
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 zod_1.default.ZodRecord) {
137
- const keySchema = schema.keyType;
138
- const valueSchema = schema.valueType;
139
- if (keySchema instanceof zod_1.default.ZodEnum) {
140
- const options = keySchema.options;
141
- const propertyCount = options.length;
142
- const selectedKeys = faker_1.faker.helpers.arrayElements(options, propertyCount);
143
- return selectedKeys.reduce((record, key) => (Object.assign(Object.assign({}, record), { [key]: this.generateMockData(valueSchema) })), {});
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 propertyCount = faker_1.faker.number.int({ min: 1, max: 3 });
146
- return Array.from({ length: propertyCount })
147
- .reduce((record, _, index) => {
148
- const key = keySchema instanceof zod_1.default.ZodString
149
- ? this.generateString(keySchema)
150
- : keySchema instanceof zod_1.default.ZodNumber
151
- ? this.generateNumber(keySchema).toString()
152
- : `${index}`;
153
- return Object.assign(Object.assign({}, record), { [key]: this.generateMockData(valueSchema) });
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 zod_1.default.ZodOptional)
157
- return this.generateMockData(schema.unwrap());
158
- if (schema instanceof zod_1.default.ZodNullable)
159
- return this.generateMockData(schema.unwrap());
160
- if (schema instanceof zod_1.default.ZodDefault)
161
- return this.generateMockData(schema.unwrap());
162
- if (schema instanceof zod_1.default.ZodLazy)
163
- return this.generateMockData(schema.def.getter());
164
- if (schema instanceof zod_1.default.ZodAny || schema instanceof zod_1.default.ZodUnknown)
165
- return this.generateAnyValue();
166
- if (schema instanceof zod_1.default.ZodVoid)
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 zod_1.default.ZodNull)
161
+ if (schema instanceof z.ZodNull)
169
162
  return null;
170
- if (schema instanceof zod_1.default.ZodLiteral)
163
+ if (schema instanceof z.ZodLiteral)
171
164
  return schema.value;
172
- if (schema instanceof zod_1.default.ZodPipe) {
173
- const pipe = this.generateMockData(schema.def.in);
174
- return pipe;
165
+ if (schema instanceof z.ZodPipe) {
166
+ return this.generateFromSchema(schema.def.in);
175
167
  }
176
- return null;
177
- }
178
- generateString(schema) {
179
- var _a, _b, _c;
180
- const value = schema;
181
- const { minLength, maxLength, format } = value;
182
- if (format === 'email') {
183
- return faker_1.faker.internet.email();
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 (format === 'regex') {
186
- const regex = (_c = (_b = (_a = value.def.checks) === null || _a === void 0 ? void 0 : _a.find(c => { var _a; return (_a = c._zod.def) === null || _a === void 0 ? void 0 : _a.pattern; })) === null || _b === void 0 ? void 0 : _b._zod.def) === null || _c === void 0 ? void 0 : _c.pattern;
187
- const randexp = new randexp_1.default(regex);
188
- return randexp.gen();
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 (format === 'url') {
191
- return faker_1.faker.internet.url();
204
+ if (schema instanceof z.ZodFunction) {
205
+ const functionWithDef = schema;
206
+ return (() => {
207
+ return this.generateFromSchema(functionWithDef.def.output);
208
+ });
192
209
  }
193
- if (format === 'uuid') {
194
- return faker_1.faker.string.uuid();
210
+ if (this.isBrandedType(schema)) {
211
+ return this.generateFromSchema(schema.unwrap());
195
212
  }
196
- if (format === 'ulid') {
197
- return faker_1.faker.string.ulid();
213
+ if (schema instanceof z.ZodReadonly) {
214
+ return this.generateFromSchema(schema.def.innerType);
198
215
  }
199
- if (format === 'date-time' || format === 'datetime') {
200
- return faker_1.faker.date.recent().toISOString();
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
- const min = 10;
203
- const max = this.getRandomNumber(100, min);
204
- return faker_1.faker.string.alpha({
205
- length: {
206
- min: minLength !== null && minLength !== void 0 ? minLength : min,
207
- max: maxLength !== null && maxLength !== void 0 ? maxLength : max
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
- generateNumber(schema) {
212
- const value = schema;
213
- const { minValue, maxValue, format } = value;
214
- if (!format) {
215
- const { min, max } = this.getMinMax(minValue, maxValue);
216
- return faker_1.faker.number.int({
217
- min,
218
- max
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 (format === 'int' || format === 'integer' || format === 'int32' || format === 'int64' || format === 'uint' || format === 'uint32' || format === 'uint64' || format === 'safeint' || format === 'safeinteger') {
222
- const min = 1;
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
- if (format === 'float' || format === 'double' || format === 'decimal' || format === 'float32' || format === 'float64') {
229
- const min = 1.5;
230
- return faker_1.faker.number.float({
231
- min: minValue !== null && minValue !== void 0 ? minValue : min,
232
- max: maxValue !== null && maxValue !== void 0 ? maxValue : this.getRandomNumber(1000, min)
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
- const min = 1;
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
- });
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
- getBralizilianMockedData() {
242
- return {
243
- cpf: faker_1.faker.helpers.arrayElement(this.validCPF),
244
- cnpj: faker_1.faker.helpers.arrayElement(this.validCNPJ),
245
- rg: faker_1.faker.helpers.arrayElement(this.validRG),
246
- phoneBR: faker_1.faker.helpers.arrayElement(this.validPhoneBR),
247
- cep: faker_1.faker.helpers.arrayElement(this.validCEP)
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
- getMinMax(minValue, maxValue) {
251
- const infinity = [Infinity, -Infinity];
252
- const isMinInfinity = infinity.some(i => i === minValue);
253
- const isMaxInfinity = infinity.some(i => i === maxValue);
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
- generateAnyValue() {
260
- const types = ['string', 'number', 'boolean', 'date', 'array', 'object'];
261
- const randomType = faker_1.faker.helpers.arrayElement(types);
262
- if (randomType === 'string')
263
- return faker_1.faker.lorem.words(2);
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