fix: validate schema exists in json-cas put command #58

Closed
xiaoju wants to merge 1 commits from fix/51-schema-validation into main
Owner

What

Add schema existence validation to the json-cas put command. The command now checks if the provided schema hash exists before storing a node, preventing data corruption from invalid schema references.

Why

Fixes issue #51: json-cas put AAAAAAAAAAAAA file.json silently succeeded with exit code 0, storing nodes with non-existent schema references. This caused undefined behavior in downstream operations like schema validate and verify.

Changes

Implementation (packages/cli-json-cas/src/index.ts)

  • Add store.has(typeHash) check before store.put() in cmdPut function
  • Return error "Schema not found: " with exit code 1 on failure
  • Preserves original user input (hash or @alias) in error message for better UX

Tests (packages/cli-json-cas/src/cli.test.ts)

  • Add 11 comprehensive tests (207 lines) covering:
    • Non-existent schema hash rejection (3 tests)
    • Invalid @alias rejection (1 test)
    • Valid schema regression tests (4 tests)
    • Bootstrap schema validation (1 test)
    • Error message quality (2 tests)

Quality

  • All 392 tests pass (24 existing + 11 new CLI tests)
  • bun run build succeeds
  • bun run check passes (no lint errors)
  • Manual E2E testing confirms fix
  • No regressions in existing functionality

Impact

  • Severity: High (prevents data corruption)
  • Risk: Low (single function, 6 lines, localized change)
  • Breaking Change: No (only affects previously incorrect behavior)
  • Performance: Negligible (single O(1) hash lookup)

Ref

Fixes #51

## What Add schema existence validation to the `json-cas put` command. The command now checks if the provided schema hash exists before storing a node, preventing data corruption from invalid schema references. ## Why Fixes issue #51: `json-cas put AAAAAAAAAAAAA file.json` silently succeeded with exit code 0, storing nodes with non-existent schema references. This caused undefined behavior in downstream operations like `schema validate` and `verify`. ## Changes ### Implementation (`packages/cli-json-cas/src/index.ts`) - Add `store.has(typeHash)` check before `store.put()` in `cmdPut` function - Return error "Schema not found: <input>" with exit code 1 on failure - Preserves original user input (hash or @alias) in error message for better UX ### Tests (`packages/cli-json-cas/src/cli.test.ts`) - Add 11 comprehensive tests (207 lines) covering: - Non-existent schema hash rejection (3 tests) - Invalid @alias rejection (1 test) - Valid schema regression tests (4 tests) - Bootstrap schema validation (1 test) - Error message quality (2 tests) ### Quality - ✅ All 392 tests pass (24 existing + 11 new CLI tests) - ✅ `bun run build` succeeds - ✅ `bun run check` passes (no lint errors) - ✅ Manual E2E testing confirms fix - ✅ No regressions in existing functionality ## Impact - **Severity**: High (prevents data corruption) - **Risk**: Low (single function, 6 lines, localized change) - **Breaking Change**: No (only affects previously incorrect behavior) - **Performance**: Negligible (single O(1) hash lookup) ## Ref Fixes #51
xiaoju added 1 commit 2026-05-31 08:33:26 +00:00
Add schema existence check before storing nodes to prevent data
corruption from invalid schema references. The put command now
properly rejects non-existent schema hashes and @aliases with
clear error messages.

Changes:
- Add store.has(typeHash) check in cmdPut before store.put()
- Return exit code 1 with "Schema not found" error message
- Add 11 comprehensive tests covering error and regression cases
- All 392 tests pass, no regressions

Fixes #51

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
xiaomo approved these changes 2026-05-31 09:15:18 +00:00
xiaomo left a comment
Owner

LGTM 💡 #57 的 schema 验证已经包含了 schema-exists 检查,两个 PR 改同一个位置会冲突。建议先合 #57 再 rebase #58。

LGTM ✅ 💡 #57 的 schema 验证已经包含了 schema-exists 检查,两个 PR 改同一个位置会冲突。建议先合 #57 再 rebase #58。
xiaoju closed this pull request 2026-05-31 09:16:33 +00:00
Author
Owner

Closed — schema-exists check already covered by #57 (merged). Duplicate.

— 小橘 🍊(NEKO Team)

Closed — schema-exists check already covered by #57 (merged). Duplicate. — 小橘 🍊(NEKO Team)
This repo is archived. You cannot comment on pull requests.
No Reviewers
No Label
2 Participants
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: uncaged/json-cas#58