misc bugfixes

This commit is contained in:
Aiden McClelland
2026-03-02 18:02:20 -07:00
parent 011a3f9d9f
commit f004c46977
5 changed files with 135 additions and 154 deletions

View File

@@ -74,21 +74,38 @@ declare module 'zod' {
}
// Override z.object to produce loose objects by default (extra keys are preserved, not stripped).
// Patches the source module in require.cache where 'object' is a writable property;
const _origObject = _z.object
const _patchedObject = (...args: Parameters<typeof _z.object>) =>
_origObject(...args).loose()
// In CJS (Node.js), patch the source module in require.cache where 'object' is a writable property;
// the CJS getter chain (index → external → schemas) then relays the patched version.
// We walk only the zod entry module's dependency tree and match by identity (=== origObject).
const _origObject = _z.object
const _zodModule = require.cache[require.resolve('zod')]
for (const child of _zodModule?.children ?? []) {
for (const grandchild of child.children ?? []) {
const desc = Object.getOwnPropertyDescriptor(grandchild.exports, 'object')
if (desc?.value === _origObject && desc.writable) {
grandchild.exports.object = (...args: Parameters<typeof _z.object>) =>
_origObject(...args).loose()
try {
const _zodModule = require.cache[require.resolve('zod')]
for (const child of _zodModule?.children ?? []) {
for (const grandchild of child.children ?? []) {
const desc = Object.getOwnPropertyDescriptor(grandchild.exports, 'object')
if (desc?.value === _origObject && desc.writable) {
grandchild.exports.object = _patchedObject
}
}
}
} catch (_) {
// Not in CJS/Node environment (e.g. browser) — require.cache unavailable
}
export { _z as z }
// z.object is a non-configurable getter on the zod namespace, so we can't override it directly.
// Shadow it by exporting a new object with _z as prototype and our patched object on the instance.
const z: typeof _z = Object.create(_z, {
object: {
value: _patchedObject,
writable: true,
configurable: true,
enumerable: true,
},
})
export { z }
export * as utils from './util'