MCPcopy
hub / github.com/colinhacks/zod / generateFastpass

Function generateFastpass

packages/zod/src/v4/core/schemas.ts:1990–2091  ·  view source on GitHub ↗
(shape: any)

Source from the content-addressed store, hash-verified

1988 const _normalized = util.cached(() => normalizeDef(def));
1989
1990 const generateFastpass = (shape: any) => {
1991 const doc = new Doc(["shape", "payload", "ctx"]);
1992 const normalized = _normalized.value;
1993
1994 const parseStr = (key: string) => {
1995 const k = util.esc(key);
1996 return `shape[${k}]._zod.run({ value: input[${k}], issues: [] }, ctx)`;
1997 };
1998
1999 doc.write(`const input = payload.value;`);
2000
2001 const ids: any = Object.create(null);
2002 let counter = 0;
2003 for (const key of normalized.keys) {
2004 ids[key] = `key_${counter++}`;
2005 }
2006
2007 // A: preserve key order {
2008 doc.write(`const newResult = {};`);
2009 for (const key of normalized.keys) {
2010 const id = ids[key];
2011 const k = util.esc(key);
2012 const schema = shape[key];
2013 const isOptionalIn = schema?._zod?.optin === "optional";
2014 const isOptionalOut = schema?._zod?.optout === "optional";
2015
2016 doc.write(`const ${id} = ${parseStr(key)};`);
2017
2018 if (isOptionalIn && isOptionalOut) {
2019 // For optional-in/out schemas, ignore errors on absent keys
2020 doc.write(`
2021 if (${id}.issues.length) {
2022 if (${k} in input) {
2023 payload.issues = payload.issues.concat(${id}.issues.map(iss => ({
2024 ...iss,
2025 path: iss.path ? [${k}, ...iss.path] : [${k}]
2026 })));
2027 }
2028 }
2029
2030 if (${id}.value === undefined) {
2031 if (${k} in input) {
2032 newResult[${k}] = undefined;
2033 }
2034 } else {
2035 newResult[${k}] = ${id}.value;
2036 }
2037
2038 `);
2039 } else if (!isOptionalIn) {
2040 doc.write(`
2041 const ${id}_present = ${k} in input;
2042 if (${id}.issues.length) {
2043 payload.issues = payload.issues.concat(${id}.issues.map(iss => ({
2044 ...iss,
2045 path: iss.path ? [${k}, ...iss.path] : [${k}]
2046 })));
2047 }

Callers 1

schemas.tsFile · 0.85

Calls 5

writeMethod · 0.95
compileMethod · 0.95
parseStrFunction · 0.85
fnFunction · 0.85
createMethod · 0.45

Tested by

no test coverage detected