chore: split out nvidia variant

This commit is contained in:
Aiden McClelland
2026-03-02 16:04:53 -07:00
parent b1c533d670
commit 011a3f9d9f
12 changed files with 164 additions and 26 deletions

View File

@@ -14,14 +14,78 @@ import type { DeepPartial } from './types'
type ZodDeepPartial = <T>(a: _z.ZodType<T>) => _z.ZodType<DeepPartial<T>>
// Add deepPartial to z at runtime, wrapping with .passthrough() to allow extra keys
;(_z as any).deepPartial = <T>(a: _z.ZodType<T>) =>
(zodDeepPartial(a) as any).passthrough()
// Recursively make all ZodObjects in a schema loose (preserve extra keys at every nesting level).
// Uses _zod.def.type duck-typing instead of instanceof to avoid issues with mismatched zod versions.
function deepLoose<S extends _z.ZodType>(schema: S): S {
const def = (schema as any)._zod?.def
if (!def) return schema
let result: _z.ZodType
switch (def.type) {
case 'optional':
result = deepLoose(def.innerType).optional()
break
case 'nullable':
result = deepLoose(def.innerType).nullable()
break
case 'object': {
const newShape: Record<string, _z.ZodType> = {}
for (const key in (schema as any).shape) {
newShape[key] = deepLoose((schema as any).shape[key])
}
result = _z.looseObject(newShape)
break
}
case 'array':
result = _z.array(deepLoose(def.element))
break
case 'union':
result = _z.union(def.options.map((o: _z.ZodType) => deepLoose(o)))
break
case 'intersection':
result = _z.intersection(deepLoose(def.left), deepLoose(def.right))
break
case 'record':
result = _z.record(def.keyType, deepLoose(def.valueType))
break
case 'tuple':
result = _z.tuple(def.items.map((i: _z.ZodType) => deepLoose(i)))
break
case 'lazy':
result = _z.lazy(() => deepLoose(def.getter()))
break
default:
return schema
}
return result as S
}
// Augment zod's z namespace so z.deepPartial is typed
type ZodDeepLoose = <T>(a: _z.ZodType<T>) => _z.ZodType<T>
// Add deepPartial and deepLoose to z at runtime
;(_z as any).deepPartial = <T>(a: _z.ZodType<T>) => deepLoose(zodDeepPartial(a))
;(_z as any).deepLoose = deepLoose
// Augment zod's z namespace so z.deepPartial and z.deepLoose are typed
declare module 'zod' {
namespace z {
const deepPartial: ZodDeepPartial
const deepLoose: ZodDeepLoose
}
}
// 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;
// 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()
}
}
}