Skip to content

Commit 054a091

Browse files
committed
fix(build): use non-destructured require for zod + @strapi/utils
Prevents the same runtime crash that hit magic-link: Cannot read properties of undefined (reading 'string') at z.string() during plugin load Pack-up's rollup-commonjs rewrites `const { z } = require('zod')` into `_interopDefault(...).default.z`. For dual ESM/CJS packages like zod 3.25+ and @strapi/utils, that `.default` branch does NOT expose the named members, so `z` resolves to undefined and every schema build crashes at plugin boot. Replaced destructured requires with direct property access: const { z } = require('zod') → const z = require('zod') const { errors: strapiErrors } = require('@strapi/utils') → const strapiErrors = require('@strapi/utils').errors Property access after a plain require() is not touched by rollup- commonjs, so the runtime value matches source semantics.
1 parent 9361f24 commit 054a091

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

server/src/validation.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
'use strict';
22

3-
const { z } = require('zod');
3+
// NOTE: Do not destructure `z` at require-time.
4+
// @strapi/sdk-plugin's bundler (pack-up → @rollup/plugin-commonjs) rewrites
5+
// `const { z } = require('zod')` as an ESM-style named import, which resolves
6+
// to `_interopDefault(require('zod')).default.z` — undefined in zod 3.25+
7+
// because the dual ESM/CJS `.default` branch does not expose the `z`
8+
// namespace. Plain `require('zod')` gives the same API via direct property
9+
// access and sidesteps the rewrite.
10+
const z = require('zod');
411

512
const emailString = z.string().email().max(254);
613
const safeString = z.string().max(1000);
@@ -327,7 +334,7 @@ function validate(schemaName, body) {
327334

328335
const result = schema.safeParse(body);
329336
if (!result.success) {
330-
const { errors: strapiErrors } = require('@strapi/utils');
337+
const strapiErrors = require('@strapi/utils').errors;
331338
const flattened = result.error.flatten();
332339
// eslint-disable-next-line global-require
333340
const strapiLog = (typeof strapi !== 'undefined' && strapi && strapi.log) ? strapi.log : null;

0 commit comments

Comments
 (0)