Skip to content

Reject unknown or wrong-typed top-level JSON commit input keys - #72

Merged
Ian-stetsenko merged 1 commit into
mainfrom
fix/commit-input-unknown-keys
Nov 3, 2026
Merged

Ian-stetsenko merged 1 commit into
mainfrom
fix/commit-input-unknown-keys

Conversation

@TommyC81

@TommyC81 TommyC81 commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

fix(commit): reject unknown or wrong-typed top-level JSON input keys

What

JsonInputReader (stdin and --file JSON input) now validates the top-level shape before reading it. Unknown top-level keys are rejected with an error naming each one, the allowed schema (intent, body, trailers), and a hint that trailer values belong inside the trailers object. Wrong-typed values (intent/body must be strings, trailers must be an object), non-object payloads (array, number, string, null), and invalid JSON are rejected with the offending type. Every failure is an actionable LoreError with exit 1.

Why

The current reader silently ignores every top-level key except intent/body/trailers: a flat schema ({"intent": "...", "Constraint": [...]}) commits with exit 0 and every trailer dropped, and wrong-typed values are silently coerced (non-string intent becomes empty, non-string body vanishes). Tooling that feeds JSON through --file or stdin gets a barren atom with a success exit code, and lore validate passes it. Also relevant to the Rust parity port (#71), which currently replicates the lenient behavior.

Behavior notes

  • Inner trailer parsing stays lenient (non-string array values are still filtered); only the top level is strict.
  • An absent intent still defaults to empty string and fails downstream validation exactly as before; {} behaves identically to today.
  • Invalid JSON now exits 1 with a formatted, actionable message instead of leaking a raw SyntaxError past the error handler.

Tests

  • npm run build and npm run typecheck clean.
  • vitest 479 passed: 2 lenient-coercion tests replaced with rejection tests, 12 top-level validation cases added (flat schema with the exit-1 contract pinned via instanceof LoreError, single and multiple unknown keys, nested envelope, array and number top level, __proto__ own key, wrong-typed trailers as string/array/null, invalid JSON, empty-object pass-through).
  • npm run lint fails on main itself (ESLint 9 expects a flat config the repo does not ship yet); pre-existing and out of scope here.

Proposed changelog entry (happy to include it directly if maintainers prefer PRs to carry it):

Fixed: lore commit JSON input no longer silently drops unknown top-level keys or wrong-typed values: a flat or misspelled schema fails with exit 1 and an actionable error naming the allowed schema, instead of committing a barren atom.

JSON input (stdin and --file) silently ignored every top-level key
except intent/body/trailers: a flat schema (trailers at the top level)
committed with exit 0 and every trailer dropped, and wrong-typed values
were silently coerced (non-string intent became empty, non-string body
vanished).

JsonInputReader now validates the top-level shape and throws an
actionable LoreError (exit 1) instead: unknown keys are named with the
allowed schema and a hint that trailer values belong inside the trailers
object; non-object payloads, invalid JSON, and wrong-typed
intent/body/trailers are rejected with the offending type. Inner trailer
parsing stays lenient (non-string values are still filtered); only the
top level is strict.

Lore-id: cd854cfb
Constraint: inner trailer parsing stays lenient (non-string values still filtered); only the top level is strict
Constraint: absent intent still defaults to empty string so the downstream commitBuilder.validate missing-intent path is unchanged
Tested: npm run build and npm run typecheck clean
Tested: vitest 479 passed (2 lenient-coercion tests replaced with rejection tests, 12 top-level validation cases added)
Not-tested: npm run lint: fails on origin/main itself (ESLint 9 has no flat config); pre-existing, out of scope
Confidence: high
Scope-risk: narrow
Reversibility: clean
@TommyC81
TommyC81 force-pushed the fix/commit-input-unknown-keys branch from a648bce to c011681 Compare October 30, 2026 03:48
@TommyC81 TommyC81 changed the title fix(commit): reject unknown or wrong-typed top-level JSON input keys Reject unknown or wrong-typed top-level JSON commit input keys Oct 30, 2026
@Ian-stetsenko
Ian-stetsenko merged commit 11a05cd into main Nov 3, 2026
2 checks passed
@Ian-stetsenko

Copy link
Copy Markdown
Owner

Merged to main in 11a05cd and shipping in 0.7.0. Thanks! Review notes:

  • Verified the exit-1 contract and messages locally; the __proto__ own-key case and the invalid-JSON path were good additions.
  • Agreed on keeping the inner trailer parsing lenient and only hardening the top level; {} behaving as before keeps the change non-breaking for anyone who only pipes intent.
  • Took your proposed changelog wording almost verbatim into CHANGELOG.md.
  • You are right that npm run lint fails on main (ESLint 9 wants a flat config the repo does not ship). That is pre-existing and tracked separately from this PR.

@TommyC81
TommyC81 deleted the fix/commit-input-unknown-keys branch November 3, 2026 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants