zodzilla 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +325 -2
- package/dist/api.d.ts +8 -0
- package/dist/api.js +8 -0
- package/dist/check.d.ts +53 -0
- package/dist/check.js +103 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/internal.d.ts +58 -0
- package/dist/internal.js +130 -0
- package/dist/invalid.d.ts +15 -0
- package/dist/invalid.js +250 -0
- package/dist/numbers.d.ts +25 -0
- package/dist/numbers.js +188 -0
- package/dist/options.d.ts +16 -0
- package/dist/options.js +5 -0
- package/dist/route.d.ts +40 -0
- package/dist/route.js +175 -0
- package/dist/runner.d.ts +58 -0
- package/dist/runner.js +97 -0
- package/dist/strings.d.ts +5 -0
- package/dist/strings.js +198 -0
- package/dist/valid.d.ts +12 -0
- package/dist/valid.js +258 -0
- package/package.json +72 -4
package/dist/valid.js
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import fc from 'fast-check';
|
|
2
|
+
import { parse } from 'zod/v4/core';
|
|
3
|
+
import { checksOf, collection, defOf, depthOptions, formatPath, recurse, sizeBounds, unsupported, validated, withContext, ZodzillaError, } from './internal.js';
|
|
4
|
+
import { bigintArb, dateArb, numberArb } from './numbers.js';
|
|
5
|
+
import { stringArb } from './strings.js';
|
|
6
|
+
/** Values the schema accepts, shaped like what you'd pass to schema.parse() */
|
|
7
|
+
export function valid(schema, options = {}) {
|
|
8
|
+
return withContext(options, (ctx) => arbitrary(schema, [], ctx));
|
|
9
|
+
}
|
|
10
|
+
/** What schema.parse() returns for those values, with transforms and defaults applied */
|
|
11
|
+
export function parsed(schema, options = {}) {
|
|
12
|
+
return valid(schema, options).map((value) => parse(schema, value));
|
|
13
|
+
}
|
|
14
|
+
// Kinds whose builders read their own checks. Checks on any other kind are refinements, handled by validation
|
|
15
|
+
const READS_CHECKS = new Set(['string', 'number', 'bigint', 'date', 'array', 'set', 'map', 'file']);
|
|
16
|
+
export function arbitrary(schema, path, ctx) {
|
|
17
|
+
const override = ctx.overrides.get(schema);
|
|
18
|
+
if (override)
|
|
19
|
+
return override;
|
|
20
|
+
const arb = build(schema, path, ctx);
|
|
21
|
+
return READS_CHECKS.has(defOf(schema).type) || checksOf(schema).length === 0 ? arb : validated(arb, schema, path, ctx);
|
|
22
|
+
}
|
|
23
|
+
function build(schema, path, ctx) {
|
|
24
|
+
const def = defOf(schema);
|
|
25
|
+
switch (def.type) {
|
|
26
|
+
case 'string':
|
|
27
|
+
return stringArb(schema, path, ctx);
|
|
28
|
+
case 'number':
|
|
29
|
+
return numberArb(schema, path, ctx);
|
|
30
|
+
case 'bigint':
|
|
31
|
+
return bigintArb(schema, path, ctx);
|
|
32
|
+
case 'date':
|
|
33
|
+
return dateArb(schema, path, ctx);
|
|
34
|
+
case 'boolean':
|
|
35
|
+
return fc.boolean();
|
|
36
|
+
case 'symbol':
|
|
37
|
+
return fc.string().map((description) => Symbol(description));
|
|
38
|
+
case 'undefined':
|
|
39
|
+
case 'void':
|
|
40
|
+
return fc.constant(undefined);
|
|
41
|
+
case 'null':
|
|
42
|
+
return fc.constant(null);
|
|
43
|
+
case 'nan':
|
|
44
|
+
return fc.constant(Number.NaN);
|
|
45
|
+
case 'any':
|
|
46
|
+
case 'unknown':
|
|
47
|
+
return fc.anything();
|
|
48
|
+
case 'literal':
|
|
49
|
+
case 'enum': {
|
|
50
|
+
const values = [...(schema._zod.values ?? [])];
|
|
51
|
+
if (!values.length)
|
|
52
|
+
throw new ZodzillaError(`${formatPath(path)} is an empty enum, so no value is valid there`);
|
|
53
|
+
return fc.constantFrom(...values);
|
|
54
|
+
}
|
|
55
|
+
case 'template_literal':
|
|
56
|
+
return templateArb(schema, path, ctx);
|
|
57
|
+
case 'array': {
|
|
58
|
+
const { min, max, other } = sizeBounds(schema);
|
|
59
|
+
const element = arbitrary(def.element, [...path, '[]'], ctx);
|
|
60
|
+
const arb = collection(ctx, min, max, (minLength, maxLength) => fc.array(element, { minLength, ...(maxLength === undefined ? {} : { maxLength }), depthIdentifier: ctx.depth }));
|
|
61
|
+
return other ? validated(arb, schema, path, ctx) : arb;
|
|
62
|
+
}
|
|
63
|
+
case 'set': {
|
|
64
|
+
const { min, max, other } = sizeBounds(schema);
|
|
65
|
+
const element = arbitrary(def.valueType, [...path, '[]'], ctx);
|
|
66
|
+
const arb = collection(ctx, min, max, (minLength, maxLength) => fc
|
|
67
|
+
.uniqueArray(element, {
|
|
68
|
+
minLength,
|
|
69
|
+
...(maxLength === undefined ? {} : { maxLength }),
|
|
70
|
+
comparator: 'SameValueZero',
|
|
71
|
+
depthIdentifier: ctx.depth,
|
|
72
|
+
})
|
|
73
|
+
.map((items) => new Set(items)));
|
|
74
|
+
return other ? validated(arb, schema, path, ctx) : arb;
|
|
75
|
+
}
|
|
76
|
+
case 'map': {
|
|
77
|
+
const { min, max, other } = sizeBounds(schema);
|
|
78
|
+
const entry = fc.tuple(arbitrary(def.keyType, [...path, '<key>'], ctx), arbitrary(def.valueType, [...path, '[]'], ctx));
|
|
79
|
+
const arb = collection(ctx, min, max, (minLength, maxLength) => fc
|
|
80
|
+
.uniqueArray(entry, {
|
|
81
|
+
minLength,
|
|
82
|
+
...(maxLength === undefined ? {} : { maxLength }),
|
|
83
|
+
selector: ([key]) => key,
|
|
84
|
+
comparator: 'SameValueZero',
|
|
85
|
+
depthIdentifier: ctx.depth,
|
|
86
|
+
})
|
|
87
|
+
.map((entries) => new Map(entries)));
|
|
88
|
+
return other ? validated(arb, schema, path, ctx) : arb;
|
|
89
|
+
}
|
|
90
|
+
case 'tuple':
|
|
91
|
+
return tupleArb(def, path, ctx);
|
|
92
|
+
case 'object':
|
|
93
|
+
return objectArb(def, path, ctx);
|
|
94
|
+
case 'record':
|
|
95
|
+
return recordArb(schema, def, path, ctx);
|
|
96
|
+
case 'union': {
|
|
97
|
+
const options = def.options.filter((option) => defOf(option).type !== 'never');
|
|
98
|
+
if (!options.length)
|
|
99
|
+
throw new ZodzillaError(`${formatPath(path)} is a union with no possible options`);
|
|
100
|
+
const arb = fc.oneof(depthOptions(ctx), ...options.map((option) => arbitrary(option, path, ctx)));
|
|
101
|
+
// z.xor() needs exactly one option to match, which picking a single option doesn't guarantee
|
|
102
|
+
const exclusive = def.inclusive === false && !('discriminator' in def);
|
|
103
|
+
return exclusive ? validated(arb, schema, path, ctx) : arb;
|
|
104
|
+
}
|
|
105
|
+
case 'intersection': {
|
|
106
|
+
const left = arbitrary(def.left, path, ctx);
|
|
107
|
+
const right = arbitrary(def.right, path, ctx);
|
|
108
|
+
const objects = defOf(def.left).type === 'object' && defOf(def.right).type === 'object';
|
|
109
|
+
const merged = objects ? fc.tuple(left, right).map(([l, r]) => ({ ...l, ...r })) : left;
|
|
110
|
+
return validated(merged, schema, path, ctx);
|
|
111
|
+
}
|
|
112
|
+
case 'optional':
|
|
113
|
+
case 'default':
|
|
114
|
+
case 'prefault':
|
|
115
|
+
return fc.option(arbitrary(def.innerType, path, ctx), { nil: undefined, ...depthOptions(ctx) });
|
|
116
|
+
case 'nullable':
|
|
117
|
+
return fc.option(arbitrary(def.innerType, path, ctx), { nil: null, ...depthOptions(ctx) });
|
|
118
|
+
case 'nonoptional':
|
|
119
|
+
return validated(arbitrary(def.innerType, path, ctx), schema, path, ctx);
|
|
120
|
+
case 'readonly':
|
|
121
|
+
return arbitrary(def.innerType, path, ctx);
|
|
122
|
+
case 'catch':
|
|
123
|
+
// .catch() swallows failures, so any value at all is valid input
|
|
124
|
+
return fc.oneof(arbitrary(def.innerType, path, ctx), fc.anything());
|
|
125
|
+
case 'pipe': {
|
|
126
|
+
const bag = schema._zod.bag;
|
|
127
|
+
if (bag.truthy && bag.falsy)
|
|
128
|
+
return stringboolArb(bag.truthy, bag.falsy, bag.case === 'insensitive');
|
|
129
|
+
// Codecs can turn an output back into an input, so generate outputs and encode them
|
|
130
|
+
if (def.reverseTransform) {
|
|
131
|
+
const encode = def.reverseTransform;
|
|
132
|
+
const encoded = arbitrary(def.out, path, ctx).map((value) => encode(value, { value, issues: [] }));
|
|
133
|
+
return validated(encoded, schema, path, ctx);
|
|
134
|
+
}
|
|
135
|
+
// Otherwise generate for the input side, and keep what the output side (and any transform) accepts
|
|
136
|
+
return validated(arbitrary(def.in, path, ctx), schema, path, ctx);
|
|
137
|
+
}
|
|
138
|
+
case 'transform':
|
|
139
|
+
return fc.anything();
|
|
140
|
+
case 'lazy':
|
|
141
|
+
return recurse('valid', schema, ctx, () => arbitrary(def.getter(), path, ctx));
|
|
142
|
+
case 'promise':
|
|
143
|
+
return arbitrary(def.innerType, path, ctx).map((value) => Promise.resolve(value));
|
|
144
|
+
case 'file': {
|
|
145
|
+
const mime = checksOf(schema).find((check) => check.check === 'mime_type')?.mime[0] ?? '';
|
|
146
|
+
const file = fc
|
|
147
|
+
.uint8Array({ maxLength: 64 })
|
|
148
|
+
.map((b) => new File([new Uint8Array(b)], 'file.bin', { type: mime }));
|
|
149
|
+
return validated(file, schema, path, ctx);
|
|
150
|
+
}
|
|
151
|
+
case 'never':
|
|
152
|
+
throw new ZodzillaError(`${formatPath(path)} is z.never(), so no value is valid there`);
|
|
153
|
+
default:
|
|
154
|
+
throw unsupported(schema, path);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function stringboolArb(truthy, falsy, anyCase) {
|
|
158
|
+
const words = fc.constantFrom(...truthy, ...falsy);
|
|
159
|
+
if (!anyCase)
|
|
160
|
+
return words;
|
|
161
|
+
return fc
|
|
162
|
+
.tuple(words, fc.nat())
|
|
163
|
+
.map(([word, mask]) => [...word].map((c, i) => ((mask >> i) & 1 ? c.toUpperCase() : c.toLowerCase())).join(''));
|
|
164
|
+
}
|
|
165
|
+
function templateArb(schema, path, ctx) {
|
|
166
|
+
try {
|
|
167
|
+
return validated(fc.stringMatching(schema._zod.pattern), schema, path, ctx);
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
throw unsupported(schema, path);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
function tupleArb(def, path, ctx) {
|
|
174
|
+
const fixed = fc.tuple(...def.items.map((item, i) => arbitrary(item, [...path, i], ctx)));
|
|
175
|
+
if (def.rest) {
|
|
176
|
+
const rest = arbitrary(def.rest, [...path, '[]'], ctx);
|
|
177
|
+
const tail = collection(ctx, 0, undefined, (minLength, maxLength) => fc.array(rest, { minLength, ...(maxLength === undefined ? {} : { maxLength }), depthIdentifier: ctx.depth }));
|
|
178
|
+
return fc.tuple(fixed, tail).map(([head, more]) => [...head, ...more]);
|
|
179
|
+
}
|
|
180
|
+
// Trailing optional items can be left off entirely
|
|
181
|
+
const optionalTail = trailingOptional(def.items);
|
|
182
|
+
return optionalTail
|
|
183
|
+
? fc.tuple(fixed, fc.nat(optionalTail)).map(([items, cut]) => items.slice(0, items.length - cut))
|
|
184
|
+
: fixed;
|
|
185
|
+
}
|
|
186
|
+
export function trailingOptional(items) {
|
|
187
|
+
let count = 0;
|
|
188
|
+
while (count < items.length && items[items.length - 1 - count]._zod.optin)
|
|
189
|
+
count++;
|
|
190
|
+
return count;
|
|
191
|
+
}
|
|
192
|
+
function objectArb(def, path, ctx) {
|
|
193
|
+
const model = {};
|
|
194
|
+
const requiredKeys = [];
|
|
195
|
+
for (const [key, value] of Object.entries(def.shape)) {
|
|
196
|
+
model[key] = arbitrary(value, [...path, key], ctx);
|
|
197
|
+
if (!value._zod.optin)
|
|
198
|
+
requiredKeys.push(key);
|
|
199
|
+
}
|
|
200
|
+
const known = fc.record(model, { requiredKeys, noNullPrototype: true });
|
|
201
|
+
const extras = extraKeys(def, path, ctx);
|
|
202
|
+
if (!extras)
|
|
203
|
+
return known;
|
|
204
|
+
// About a quarter of objects get extra keys. Cross-shrinking towards the first option (none) lets a
|
|
205
|
+
// counterexample lose them when they aren't what broke the code
|
|
206
|
+
const maybeExtras = fc.oneof({ withCrossShrink: true }, { weight: 3, arbitrary: fc.constant(undefined) }, { weight: 1, arbitrary: extras });
|
|
207
|
+
return fc.tuple(known, maybeExtras).map(withExtras);
|
|
208
|
+
}
|
|
209
|
+
// Enough to show whether unknown keys are stripped, without building deep structures for every object
|
|
210
|
+
const plainValue = fc.oneof(fc.string({ maxLength: 8 }), fc.integer(), fc.boolean(), fc.constant(null));
|
|
211
|
+
/** Keys an object accepts beyond its shape: stripped by default, kept by loose objects and catchalls */
|
|
212
|
+
function extraKeys(def, path, ctx) {
|
|
213
|
+
if (!ctx.extraKeys)
|
|
214
|
+
return undefined;
|
|
215
|
+
const catchall = def.catchall && defOf(def.catchall).type;
|
|
216
|
+
if (catchall === 'never')
|
|
217
|
+
return undefined;
|
|
218
|
+
const values = !def.catchall || catchall === 'unknown' || catchall === 'any'
|
|
219
|
+
? plainValue
|
|
220
|
+
: arbitrary(def.catchall, [...path, '*'], ctx);
|
|
221
|
+
const key = fc.string({ minLength: 1, maxLength: 10 }).filter((k) => !Object.hasOwn(def.shape, k));
|
|
222
|
+
return fc.array(fc.tuple(key, values), { minLength: 1, maxLength: 2, depthIdentifier: ctx.depth });
|
|
223
|
+
}
|
|
224
|
+
function withExtras([known, extras]) {
|
|
225
|
+
if (!extras)
|
|
226
|
+
return known;
|
|
227
|
+
const out = { ...known };
|
|
228
|
+
for (const [key, value] of extras) {
|
|
229
|
+
// A "__proto__" key has to be defined, not assigned, to become an own property as it would from JSON.parse
|
|
230
|
+
if (key === '__proto__')
|
|
231
|
+
Object.defineProperty(out, key, { value, enumerable: true, writable: true, configurable: true });
|
|
232
|
+
else
|
|
233
|
+
out[key] = value;
|
|
234
|
+
}
|
|
235
|
+
return out;
|
|
236
|
+
}
|
|
237
|
+
/** The keys of a record keyed by an enum or literal, which Zod requires all of (unless partial) */
|
|
238
|
+
export function finiteKeys(keyType) {
|
|
239
|
+
const kind = defOf(keyType).type;
|
|
240
|
+
return kind === 'enum' || kind === 'literal' ? [...(keyType._zod.values ?? [])].map(String) : undefined;
|
|
241
|
+
}
|
|
242
|
+
function recordArb(schema, def, path, ctx) {
|
|
243
|
+
const keys = finiteKeys(def.keyType);
|
|
244
|
+
if (keys) {
|
|
245
|
+
const model = Object.fromEntries(keys.map((key) => [key, arbitrary(def.valueType, [...path, key], ctx)]));
|
|
246
|
+
return fc.record(model, { requiredKeys: def.partial ? [] : keys, noNullPrototype: true });
|
|
247
|
+
}
|
|
248
|
+
const key = arbitrary(def.keyType, [...path, '<key>'], ctx).map(String);
|
|
249
|
+
const value = arbitrary(def.valueType, [...path, '*'], ctx);
|
|
250
|
+
const arb = collection(ctx, 0, undefined, (minKeys, maxKeys) => fc.dictionary(key, value, {
|
|
251
|
+
minKeys,
|
|
252
|
+
...(maxKeys === undefined ? {} : { maxKeys }),
|
|
253
|
+
noNullPrototype: true,
|
|
254
|
+
depthIdentifier: ctx.depth,
|
|
255
|
+
}));
|
|
256
|
+
// Non-string key schemas (numbers, unions of literals) get stringified, so check Zod still accepts them
|
|
257
|
+
return defOf(def.keyType).type === 'string' ? arb : validated(arb, schema, path, ctx);
|
|
258
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,74 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zodzilla",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "API fuzz testing with Zod: finds the requests that break your endpoints",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"api",
|
|
7
|
+
"elysia",
|
|
8
|
+
"fast-check",
|
|
9
|
+
"fuzz",
|
|
10
|
+
"fuzz-testing",
|
|
11
|
+
"fuzzing",
|
|
12
|
+
"hono",
|
|
13
|
+
"nextjs",
|
|
14
|
+
"property-based-testing",
|
|
15
|
+
"testing",
|
|
16
|
+
"trpc",
|
|
17
|
+
"zod"
|
|
18
|
+
],
|
|
19
|
+
"homepage": "https://github.com/postriemann/zodzilla#readme",
|
|
20
|
+
"bugs": "https://github.com/postriemann/zodzilla/issues",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"author": "Edward Schauman-Haigh (https://github.com/postriemann)",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/postriemann/zodzilla.git"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"type": "module",
|
|
31
|
+
"sideEffects": false,
|
|
32
|
+
"main": "./dist/index.js",
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"exports": {
|
|
35
|
+
".": {
|
|
36
|
+
"types": "./dist/index.d.ts",
|
|
37
|
+
"default": "./dist/index.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public",
|
|
42
|
+
"registry": "https://registry.npmjs.org/"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"bench": "tsc -p tsconfig.build.json && node bench/throughput.mjs",
|
|
46
|
+
"examples": "tsc -p tsconfig.build.json && pnpm -r run typecheck && pnpm -r run test",
|
|
47
|
+
"build": "tsc -p tsconfig.build.json",
|
|
48
|
+
"format": "oxfmt && nixfmt flake.nix",
|
|
49
|
+
"format:check": "oxfmt --check && nixfmt --check flake.nix",
|
|
50
|
+
"lint": "oxlint --deny-warnings",
|
|
51
|
+
"prepack": "tsc -p tsconfig.build.json",
|
|
52
|
+
"test": "vitest run",
|
|
53
|
+
"test:deep": "NUM_RUNS=3000 vitest run",
|
|
54
|
+
"typecheck": "tsc --noEmit"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@fast-check/vitest": "^0.6.1",
|
|
58
|
+
"@hono/zod-validator": "^0.9.1",
|
|
59
|
+
"@trpc/server": "^11.36.3",
|
|
60
|
+
"@types/node": "^26.6.11",
|
|
61
|
+
"fast-check": "^4.11.0",
|
|
62
|
+
"hono": "^4.13.23",
|
|
63
|
+
"typescript": "^7.0.2",
|
|
64
|
+
"vitest": "^5.1.2",
|
|
65
|
+
"zod": "^4.6.7"
|
|
66
|
+
},
|
|
67
|
+
"peerDependencies": {
|
|
68
|
+
"fast-check": "^4.0.0",
|
|
69
|
+
"zod": "^4.1.0"
|
|
70
|
+
},
|
|
71
|
+
"engines": {
|
|
72
|
+
"node": ">=22"
|
|
73
|
+
}
|
|
74
|
+
}
|