checkArgs
Check a call's arguments against a function's parameters.
import { checkValue } from "#codebase/tcmts/core/schema"; import type { ParamsSchema } from "#codebase/tcmts/core/schema"; export function checkArgs(schema: ParamsSchema, args: unknown[]): string | undefined { const { params, defs } = schema; const rest = params.at(-1)?.rest ? params.at(-1) : undefined; const fixed = rest ? params.slice(0, -1) : params; if (!rest && args.length > fixed.length) return `expected at most ${fixed.length} argument(s), got ${args.length}`; for (const [i, p] of fixed.entries()) { if (i >= args.length || args[i] === undefined) { if (p.optional || checkValue(p.s, undefined, "", defs) === undefined) continue; return `argument \`${p.name}\` is missing`; } const e = checkValue(p.s, args[i], `argument \`${p.name}\``, defs); if (e) return e; } if (rest) { for (let i = fixed.length; i < args.length; i++) { const e = checkValue(rest.s, args[i], `argument \`${rest.name}\`[${i - fixed.length}]`, defs); if (e) return e; } } return undefined; }