DavidTimms / zod-fast-check

Generate fast-check arbitraries from Zod schemas.
MIT License
106 stars 10 forks source link

Support Zod 3.21 & 3.22 #12

Open DavidTimms opened 1 year ago

DavidTimms commented 1 year ago

The new version of Zod brings new schema features which will need corresponding support in zod-fast-check.

https://github.com/colinhacks/zod/releases/tag/v3.21

Features:

peterhirn commented 1 year ago

I tried to start working on the new bigint features (lt, gt, etc.). Unfortunately in this release email validation was broken https://github.com/colinhacks/zod/issues/2396. After upgrading zod the email test fails with Counterexample: ["a@a.a.aa"]. I might give it another shot once the email issue is resolved in zod.

Since I only need min/max support for my use-case I patched zod-fast-check using pnpm patch / yarn patch.

diff --git a/dist/zod-fast-check.js b/dist/zod-fast-check.js
index 43b3f209cb5740f9407b90494a0299d307a35cc2..ef7f4178fffc6891b83fdb491e0ec758def39de7 100644
--- a/dist/zod-fast-check.js
+++ b/dist/zod-fast-check.js
@@ -213,8 +213,22 @@ const arbitraryBuilders = {
             }
         }
     },
-    ZodBigInt() {
-        return fast_check_1.default.bigInt();
+    ZodBigInt(schema) {
+        let min = undefined;
+        let max = undefined;
+
+        for (const check of schema._def.checks) {
+            switch (check.kind) {
+                case "min":
+                    min = min === undefined || check.value < min ? check.value : min;
+                    break;
+                case "max":
+                    max = max === undefined || check.value > max ? check.value : max;
+                    break;
+            }
+        }
+
+        return fast_check_1.default.bigInt({min, max});
     },
     ZodBoolean() {
         return fast_check_1.default.boolean();