fix: validate payload against schema in put command #57

Merged
xiaomo merged 1 commits from fix/50-schema-validation into main 2026-05-31 09:15:29 +00:00
Owner

What

This PR fixes issue #50 by adding schema validation to the json-cas put command. The CLI now validates payloads against their schemas before storing them in the CAS.

Why

Previously, json-cas put <type-hash> <file.json> would accept any JSON payload without validating it against the specified schema. This undermined the core type-safety guarantee of json-cas.

Changes

Modified Files

  1. packages/cli-json-cas/src/index.ts (+17 lines)

    • Added schema existence check before validation
    • Added payload validation using validate() function
    • Added descriptive error messages for validation failures
    • Properly creates CasNode with timestamp for validation
  2. packages/cli-json-cas/src/cli.test.ts (+587 lines)

    • 16 comprehensive test scenarios covering:
      • Valid data regression tests (4 tests)
      • Type mismatch validation (5 tests)
      • Schema error edge cases (2 tests)
      • Integration with existing features (3 tests)
      • Error message quality (2 tests)

Verification

  • All 397 tests pass (16 new tests added)
  • bun run build - Clean TypeScript compilation
  • bun run check - No Biome lint errors
  • No TypeScript strict mode violations
  • No regressions in existing functionality

Example Usage

Valid payload (accepted):

json-cas put ABCDEFGHIJKLM valid.json
NOPQRSTUVWXYZ  # Returns hash, exit 0

Invalid payload (rejected):

json-cas put ABCDEFGHIJKLM invalid.json
Validation failed: payload in invalid.json does not match schema ABCDEFGHIJKLM
# Exit code: 1

Ref

Fixes #50

## What This PR fixes issue #50 by adding schema validation to the `json-cas put` command. The CLI now validates payloads against their schemas before storing them in the CAS. ## Why Previously, `json-cas put <type-hash> <file.json>` would accept any JSON payload without validating it against the specified schema. This undermined the core type-safety guarantee of json-cas. ## Changes ### Modified Files 1. **`packages/cli-json-cas/src/index.ts`** (+17 lines) - Added schema existence check before validation - Added payload validation using `validate()` function - Added descriptive error messages for validation failures - Properly creates `CasNode` with timestamp for validation 2. **`packages/cli-json-cas/src/cli.test.ts`** (+587 lines) - 16 comprehensive test scenarios covering: - Valid data regression tests (4 tests) - Type mismatch validation (5 tests) - Schema error edge cases (2 tests) - Integration with existing features (3 tests) - Error message quality (2 tests) ### Verification - ✅ All 397 tests pass (16 new tests added) - ✅ `bun run build` - Clean TypeScript compilation - ✅ `bun run check` - No Biome lint errors - ✅ No TypeScript strict mode violations - ✅ No regressions in existing functionality ### Example Usage **Valid payload (accepted):** ```bash json-cas put ABCDEFGHIJKLM valid.json NOPQRSTUVWXYZ # Returns hash, exit 0 ``` **Invalid payload (rejected):** ```bash json-cas put ABCDEFGHIJKLM invalid.json Validation failed: payload in invalid.json does not match schema ABCDEFGHIJKLM # Exit code: 1 ``` ## Ref Fixes #50
xiaoju added 1 commit 2026-05-31 08:32:00 +00:00
Add schema validation to the `json-cas put` command to ensure data
integrity. The CLI now validates the payload against the specified
schema before storing, and exits with a non-zero code and descriptive
error message if validation fails.

Changes:
- Add schema existence check in cmdPut()
- Add payload validation before storing
- Exit with error code 1 on validation failure
- Provide helpful error messages indicating the file and schema
- Add comprehensive test suite with 16 test scenarios covering:
  - Valid data (regression tests)
  - Type mismatches (new validation)
  - Schema errors (edge cases)
  - Integration with existing features
  - Error message quality

The hash command continues to work without validation (dry-run
consistency), and schema put continues to use its own validation.

Fixes #50

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

LGTM 🟡 cmdPut 里用了 console.error + process.exit(1),应该用 die() 保持一致。💡 注意和 #58 会冲突(都改 cmdPut),协调合并顺序。

LGTM ✅ 🟡 cmdPut 里用了 console.error + process.exit(1),应该用 die() 保持一致。💡 注意和 #58 会冲突(都改 cmdPut),协调合并顺序。
xiaomo merged commit f0ffe6b234 into main 2026-05-31 09:15:29 +00:00
xiaomo deleted branch fix/50-schema-validation 2026-05-31 09:15:29 +00:00
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#57