fix(functions): validate --data JSON client-side with PowerShell quoting hint
Problem
On Windows PowerShell, passing a JSON --data argument to functions invoke (e.g. through ConvertTo-Json + npx.cmd) can have its embedded double quotes stripped or mangled by the shell before the string ever reaches process.argv. The CLI previously forwarded whatever string it received straight into the HTTP request body with no client-side validation, so the malformed JSON only failed server-side with a confusing error:
Expected property name or '}' in JSON at position 1
Root cause: shell-level, but a code-side mitigation is correct and minimal
This is fundamentally a PowerShell argument-quoting issue — argv level, outside the CLI's control. There's no way for the CLI to recover the quotes PowerShell already stripped. However, the actual problem reported — a confusing, late, server-side failure with no actionable guidance — is fixable here, and this repo already has an established pattern for it (records create, compute deploy --env): validate the JSON client-side with JSON.parse, fail fast with a clear CLIError, and never make the network request on invalid input.
Fix
functions invoke --datanow runsJSON.parsebefore sending the request. On failure it throws aCLIErrorwith the messageInvalid JSON in --dataplus a concrete PowerShell quoting hint (--data '{\"key\":\"value\"}') and points at the new escape hatch.- Added
--data-file <path>(modeled oncompute deploy --env-file) so Windows users can skip inline shell quoting entirely by putting the JSON body in a file. --dataand--data-fileare mutually exclusive, validated the same way--env/--env-fileare incompute deploy.- Documented both the new flag and the PowerShell quoting gotcha in the README's
functions invokesection.
(The triage note on the issue referenced insforge-skills/skills/insforge-cli/SKILL.md:221 for docs — that path doesn't exist in this repo, so the quoting note was added to this repo's own README instead, next to the existing functions invoke examples.)
Test plan
- Added
src/commands/functions/invoke.test.ts: malformed--datathrowsInvalid JSON in --dataandfetchis never called; valid--datais sent verbatim;--data+--data-filetogether is rejected before any request. npm run test— all new tests pass; the 5 pre-existing failures (Windows path-separator issues insrc/lib/migrations.test.tsandsrc/lib/local/checkout.test.ts) are unrelated and reproduce identically onmainbefore this change.npx eslint src/commands/functions/invoke.ts src/commands/functions/invoke.test.ts— clean.npm run build— succeeds.
Fixes #272
🤖 Generated with Claude Code
Summary by cubic
Fixes functions invoke so malformed --data JSON fails fast with a clear error instead of a confusing server-side message, and adds --data-file so PowerShell users can skip inline shell quoting.
--dataand--data-fileare mutually exclusive. Empty values (--data '',--data-file '') are treated as present, so they're validated and rejected, not silently ignored.- Client-side
JSON.parsevalidates the body before any network request; the--dataerror includes PowerShell quoting guidance, while--data-fileerrors name the offending path. --data-filereads the body from a file and strips a leading UTF-8 BOM (PowerShell commonly writes BOM-prefixed UTF-8 files).- Tests cover malformed, empty, conflicting, and file-based cases; README documents the new flag and the quoting gotcha.
Summary by CodeRabbit
-
New Features
- Added
--data-fileas an alternative to--datawhen sending JSON with function requests. - Updated documentation with file-based examples and a Windows PowerShell quoting note.
- Added
-
Bug Fixes
- Invalid JSON and file-read failures now produce clear errors. Using both body options is rejected.