@anatine/zod-mock
Advanced tools
Comparing version 3.0.4 to 3.1.0
{ | ||
"name": "@anatine/zod-mock", | ||
"version": "3.0.4", | ||
"version": "3.1.0", | ||
"description": "Zod auto-mock object generator using Faker at @faker-js/faker", | ||
@@ -24,8 +24,8 @@ "main": "src/index.js", | ||
"peerDependencies": { | ||
"@faker-js/faker": "^6.0.0-alpha.7" | ||
"@faker-js/faker": "^6.0.0-beta.0", | ||
"randexp": "^0.5.3", | ||
"zod": "^3.13.4" | ||
}, | ||
"typings": "./src/index.d.ts", | ||
"dependencies": { | ||
"zod": "^3.11.6" | ||
} | ||
"dependencies": {} | ||
} |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -6,0 +10,0 @@ if (k2 === undefined) k2 = k; |
@@ -6,2 +6,3 @@ "use strict"; | ||
const faker_1 = require("@faker-js/faker"); | ||
const randExp = require("randexp"); | ||
function parseObject(zodRef, options) { | ||
@@ -15,3 +16,3 @@ return Object.keys(zodRef.shape).reduce((carry, key) => (Object.assign(Object.assign({}, carry), { [key]: generateMock(zodRef.shape[key], Object.assign(Object.assign({}, options), { keyName: key })) })), {}); | ||
const sectionName = Object.keys(faker_1.faker).find((sectionKey) => { | ||
return Object.keys(faker_1.faker[sectionKey]).find((fnKey) => { | ||
return Object.keys(faker_1.faker[sectionKey] || {}).find((fnKey) => { | ||
var _a; | ||
@@ -40,2 +41,3 @@ const lower = fnKey.toLowerCase(); | ||
if (fnName) { | ||
// TODO: it would be good to clean up these type castings | ||
const fn = (_a = faker_1.faker[sectionKey]) === null || _a === void 0 ? void 0 : _a[fnName]; | ||
@@ -70,2 +72,12 @@ if (typeof fn === 'function') { | ||
const { checks = [] } = zodRef._def; | ||
const regexCheck = checks.find((check) => check.kind === 'regex'); | ||
if (regexCheck && 'regex' in regexCheck) { | ||
const generator = new randExp(regexCheck.regex); | ||
const max = checks.find((check) => check.kind === 'max'); | ||
if (max && 'value' in max) { | ||
generator.max = max.value; | ||
} | ||
const genRegString = generator.gen(); | ||
return genRegString; | ||
} | ||
const lowerCaseKeyName = (_a = options === null || options === void 0 ? void 0 : options.keyName) === null || _a === void 0 ? void 0 : _a.toLowerCase(); | ||
@@ -202,2 +214,7 @@ // Prioritize user provided generators. | ||
} | ||
function parseUnion(zodRef, options) { | ||
// Map the options to various possible mock values | ||
const mockOptions = zodRef._def.options.map((option) => generateMock(option, options)); | ||
return faker_1.faker.helpers.randomize(mockOptions); | ||
} | ||
const workerMap = { | ||
@@ -219,2 +236,3 @@ ZodObject: parseObject, | ||
ZodEffects: parseTransform, | ||
ZodUnion: parseUnion, | ||
}; | ||
@@ -221,0 +239,0 @@ function generateMock(zodRef, options) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25332
291
3
+ Addeddrange@1.1.1(transitive)
+ Addedrandexp@0.5.3(transitive)
+ Addedret@0.2.2(transitive)
- Removedzod@^3.11.6