Skip to content

Conversation

@tommy-ca
Copy link
Contributor

@tommy-ca tommy-ca commented Jun 16, 2025

Summary

This pull request introduces robust JSON schema validation for tasks and tasks files in the project. The primary goal is to enforce a strict, well-defined data structure for all task-related operations, improving reliability and developer experience.


Key Changes

1. JSON Schema Validation

  • Integrates ajv and ajv-formats for schema validation.
  • Adds two new schema files:
    • schemas/task.schema.json: Defines the structure and rules for individual task objects.
    • schemas/tasks-file.schema.json: Defines the structure for the overall tasks.json file, including tasks arrays and metadata.

2. Validation Utilities

  • Introduces scripts/modules/task-validator.js for:
    • Initializing and compiling JSON schemas.
    • Validating single tasks, task arrays, and the full tasks file.
    • Formatting validation errors for clear reporting.

3. CLI Enhancements

  • Adds a new CLI command: validate-tasks
    • Validates the full tasks file or a specific tag within it.
    • Provides user-friendly error and success messages.
  • Updates CLI help documentation to detail the new validation command.

4. Task Management Improvements

  • Integrates validation into task addition and update flows.
    • Tasks are now validated before being added or updated—invalid tasks are rejected with actionable errors.
  • Reading/writing of tasks files automatically validates them against the schema.

5. Dependency Updates

  • Updates package.json and package-lock.json with new dependencies.

6. Testing

  • Adds comprehensive integration and unit tests for:
    • Schema validation logic
    • The CLI validation command
    • Multiple valid and invalid data scenarios

Motivation

  • Prevents invalid or inconsistent task data from being stored or processed.
  • Provides immediate feedback on schema violations, aiding debugging and onboarding.
  • Establishes a foundation for future extensibility and automation around task management.

Usage

  • Validate an entire tasks file:
    task-master validate-tasks --file path/to/tasks.json
    
  • Validate a specific tag:
    task-master validate-tasks --file path/to/tasks.json --tag <tagName>
    

Additional Notes

  • All reads and writes to tasks.json are now validated.
  • CLI validation offers clear, actionable error messages.
  • The changes are backward compatible for data that conforms to the new schemas.

Checklist

  • Schemas added and documented
  • Validation integrated throughout task management
  • CLI and help updated
  • All relevant tests added or updated

google-labs-jules bot and others added 3 commits June 16, 2025 03:13
This commit introduces a comprehensive JSON schema validation system for Task Master tasks.

Key changes:
- Added JSON schemas for individual task objects (`schemas/task.schema.json`) and the overall `tasks.json` file structure (`schemas/tasks-file.schema.json`). These schemas define the expected structure, data types, and constraints for tasks and their storage.
- Implemented a new validation module (`scripts/modules/task-validator.js`) using the `ajv` library. This module loads the defined schemas and provides utility functions to validate task objects and the entire `tasks.json` file. It includes a helper to format AJV errors into messages that are easy for you to understand.
- Integrated schema validation into core task operations:
    - `readJSON` now logs warnings if `tasks.json` does not conform to the schema.
    - `writeJSON` now prevents writing `tasks.json` if its content is invalid, throwing an error to safeguard data integrity.
    - `addTask` and `updateTaskById` now validate task objects against the schema before saving, preventing the introduction of malformed tasks.
- Introduced a new CLI command `validate-tasks` (alias `validate`) that allows you to manually check your `tasks.json` file against the schema. It supports validating the entire file or a specific tag's tasks.
- Standardized and improved error reporting for validation failures across the application.
- Added comprehensive unit tests for the validation module and integration tests for the `validate-tasks` command. Updated existing unit tests for `utils.js` to reflect validation integration.

This feature enhances data integrity, provides clearer error feedback, and helps ensure consistency in the task data managed by Task Master.
- Fixed schema file path resolution in task-validator.js
- Fixed import path for task-validator in commands.js
- Added ajv-formats dependency for date-time format support
- Fixed ES module compatibility issues in integration tests
- Corrected test expectations for schema validation behavior
- Fixed invalid JSON in test fixture files
- Added verbose option to AJV for better error reporting
- All task-validator unit tests (21/21) now passing
- All validate-tasks integration tests (7/7) now passing

The JSON schema validation feature is now fully functional:
- Validates task objects against task.schema.json
- Validates tasks.json file structure against tasks-file.schema.json
- Integrated into CLI via 'validate-tasks' command
- Supports both full file and tag-specific validation
- Provides detailed error messages with paths and descriptions
Phase 1 - Critical Fixes (Immediate):

✅ Add robust error handling for schema compilation failures
- Implemented comprehensive error handling for JSON schema loading and compilation
- Added try-catch blocks around all schema operations (file loading, parsing, compilation)
- Provided fallback behavior when schemas can't be loaded or compiled
- Added proper error logging with user-friendly error messages
- Implemented lazy initialization with caching for schema compilation state

✅ Fix missing import in commands.js
- Import for task-validator was already present and working correctly

✅ Add comprehensive error handling tests
- Created new test file: tests/unit/task-validator-error-handling.test.js
- Added tests for schema compilation failures, runtime errors, and edge cases
- Added tests for new error formatting functions (schema-compilation, validation-runtime, schema-reference)
- Added tests for initialization and state management functions
- Updated existing tests to work with new error handling structure

Key improvements:
- Schema compilation errors are now caught and handled gracefully
- Validation functions return structured error information including schema errors
- New error types: schema-compilation, validation-runtime, schema-reference
- Robust initialization system with proper error recovery
- Comprehensive test coverage for all error scenarios
- Integration with existing validation command works seamlessly

All tests pass and validation command works correctly with real data.
@changeset-bot
Copy link

changeset-bot bot commented Jun 16, 2025

⚠️ No Changeset found

Latest commit: 62d29e8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@Crunchyman-ralph Crunchyman-ralph changed the base branch from main to next June 16, 2025 18:21
Copy link
Collaborator

@Crunchyman-ralph Crunchyman-ralph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use zod, mind changing it to zod ?

Copy link
Collaborator

@Crunchyman-ralph Crunchyman-ralph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more changes requested

Comment on lines +3655 to +3663
programInstance
.command('validate-tasks')
.alias('validate')
.description('Validates the structure and content of the tasks.json file.')
.option('-f, --file <filepath>', 'Path to the tasks file (defaults to .taskmaster/tasks/tasks.json)')
.option('-t, --tag <tagName>', 'Validate only the specified tag tasks array')
.action(async (options) => {
const projectRoot = findProjectRoot(process.cwd()) || process.cwd();
const tasksFilePath = options.file || path.join(projectRoot, '.taskmaster', 'tasks', 'tasks.json');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you're going to add a command, don't forget to add an MCP tool command as well

Copy link
Collaborator

@Crunchyman-ralph Crunchyman-ralph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More comments, thanks for the PR

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to use zod here not ajv

Comment on lines 255 to 263
// Validate tasks.json content after reading
if (filepath.endsWith('tasks.json') && typeof data === 'object' && data !== null) {
const { isValid, errors } = validateTasksFile(data);
if (!isValid) {
errors.forEach(error => {
log('warn', `Validation warning for ${filepath} - ${formatAjvError(error)}`);
});
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use path-utils, maybe use it to determine if a task is a task ?

openhands-agent and others added 4 commits June 16, 2025 19:39
- Replace AJV with Zod for JSON schema validation in task-validator.js
- Convert all validation schemas to Zod format with proper type definitions
- Update error conversion to maintain AJV-compatible format for existing code
- Add isTasksFile() helper in utils.js using path constants instead of hardcoded strings
- Create validate-tasks MCP tool for validating tasks files and specific tags
- All existing tests pass with improved validation strictness
- CLI validate-tasks command continues to work with enhanced error reporting
This commit refactors the task and tasks.json validation system from using AJV and separate JSON Schema files to using Zod schemas defined directly in code.

Key changes:
- Defined Zod schemas for task objects (including recursive definitions), tag metadata, tag objects, and the overall `tasks.json` file structure in `scripts/schemas/zod-schemas.js`.
- Updated the `scripts/modules/task-validator.js` module to use these Zod schemas for its validation functions (`validateTask`, `validateTasksFile`, `validateTasksArray`). This replaces all previous AJV-based logic.
- Implemented `formatZodError` within `task-validator.js` to convert Zod's `safeParse` error objects into user-friendly formatted strings.
- Updated all modules that previously used the AJV validator (`utils.js`, `task-manager/add-task.js`, `task-manager/update-task-by-id.js`, and `commands.js` for the `validate-tasks` command) to now use the Zod-based validator and its error reporting mechanisms.
- Overhauled unit tests for `task-validator.js` and updated integration/unit tests for `validate-tasks` CLI and `utils.js` to align with Zod's validation logic and error structures.
- Removed the old JSON Schema files (`schemas/task.schema.json`, `schemas/tasks-file.schema.json`) and uninstalled the `ajv` dependency from the project.

This refactoring centralizes schema definitions within JavaScript, improves type safety benefits often associated with Zod, and simplifies the validation setup.
This commit addresses issues identified during the testing of the Zod-based schema validation refactoring.

Key fixes:
- Resolved an 'Illegal return statement' syntax error in `scripts/modules/utils.js`.
- Updated `getDebugFlag` and `getLogLevel` in `scripts/modules/config-manager.js` to prioritize environment variables, enabling effective debug logging for integration tests run via `execSync`.
- Corrected an invalid JSON fixture file (`tasks-for-tag-validation.json`) by removing a comment that caused parsing errors.
- These changes have resulted in all integration tests for the `validate-tasks` CLI command (`tests/integration/cli/validate-tasks.test.js`) now passing.

Note: Unit tests for `scripts/modules/utils.js` concerning `fs` mocking remain unresolved and will be addressed in a separate effort. This commit ensures the core Zod validation feature and its CLI interface are functioning correctly based on integration testing.
Moved the `validateTasksFile` call within the `readJSON` function in `scripts/modules/utils.js` to be inside the main `try...catch` block that follows `JSON.parse()`.

This ensures that schema validation is only attempted on successfully parsed JSON data, preventing potential errors if `JSON.parse` itself fails and `data` is not an object. This also makes the error handling for parsing vs. schema validation slightly cleaner within `readJSON`.
@Crunchyman-ralph
Copy link
Collaborator

@CodeRabbit please review and give a summary of this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 16, 2025

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Get PR details including title, description, and files changed
gh pr view 794 --json title,body,files,additions,deletions,state,author

Length of output: 4742


🏁 Script executed:

#!/bin/bash
# Examine the schema files to understand validation structure
echo "=== Task Schema ==="
cat schemas/task.schema.json

echo -e "\n=== Tasks File Schema ==="
cat schemas/tasks-file.schema.json

echo -e "\n=== Task Validator Implementation ==="
cat scripts/modules/task-validator.js

Length of output: 6353


🏁 Script executed:

#!/bin/bash
# Examine the CLI integration and test coverage
echo "=== CLI Commands Integration ==="
cat scripts/modules/commands.js | grep -A 20 -B 5 "validate-tasks"

echo -e "\n=== Zod Schemas Implementation ==="
cat scripts/schemas/zod-schemas.js

echo -e "\n=== Task Manager Integration ==="
echo "--- Add Task Integration ---"
cat scripts/modules/task-manager/add-task.js

echo -e "\n--- Update Task Integration ---"
cat scripts/modules/task-manager/update-task.js 2>/dev/null || echo "File not found, checking update-task-by-id.js"
cat scripts/modules/task-manager/update-task-by-id.js

Length of output: 52839


🏁 Script executed:

#!/bin/bash
# Examine test coverage and utils integration
echo "=== Test Coverage Overview ==="
echo "--- Validation Tests ---"
head -30 tests/unit/task-validator.test.js

echo -e "\n--- CLI Integration Tests ---"
head -30 tests/integration/cli/validate-tasks.test.js

echo -e "\n=== Utils Integration ==="
echo "--- Utils readJSON/writeJSON with validation ---"
cat scripts/modules/utils.js | grep -A 15 -B 5 "readJSON\|writeJSON"

Length of output: 9619


@Crunchyman-ralph, I've completed a comprehensive review of this JSON schema validation feature. Here's my analysis:

PR Summary

This PR introduces robust JSON schema validation for the task management system, adding validation at both the individual task level and the complete tasks file level. It's a well-architected feature that significantly improves data integrity and developer experience.

Key Components Added

1. Schema Definition (Dual Approach)

  • JSON Schemas: schemas/task.schema.json and schemas/tasks-file.schema.json for documentation/reference
  • Zod Schemas: scripts/schemas/zod-schemas.js for runtime validation with better error messages

2. Validation Engine

  • task-validator.js: Core validation logic with user-friendly error formatting
  • Three validation functions: validateTask(), validateTasksArray(), validateTasksFile()
  • Recursive subtask support: Properly handles nested task structures

3. CLI Integration

  • New validate-tasks command: Validates entire files or specific tags
  • Enhanced help documentation
  • User-friendly error reporting

4. Seamless Integration

  • Task creation/updates: Validation automatically applied during add/update operations
  • File I/O operations: readJSON/writeJSON now validate tasks.json files
  • Backward compatibility: Existing valid data continues to work

Technical Strengths

Comprehensive validation coverage - covers all task properties and file structure
Excellent error handling - Zod provides detailed, actionable error messages
Proper integration points - validation occurs at the right places in the workflow
Recursive schema support - handles subtasks correctly with lazy evaluation
Extensive test coverage - both unit and integration tests included
Maintains existing patterns - follows project conventions and coding style

Areas for Improvement

1. Schema Consistency
The task-validator.js file references both JSON Schema and Zod schemas, which could be confusing. Consider standardizing on Zod throughout since it's used for runtime validation.

2. Validation Integration in Utils

// In utils.js - the validation logic could be more robust
if (filepath.endsWith('tasks.json') && typeof data === 'object' && data !== null) {
    const { isValid, errors } = validateTasksFile(data);
    if (!isValid && errors) {
        errors.forEach(formattedErrorString => {
            log('warn', `Validation warning for ${filepath} - ${formattedErrorString}`);
        });
    }
}

3. CLI Output Enhancement
The validate-tasks command could benefit from more detailed output options (e.g., JSON output for scripting, summary statistics).

Required Actions

⚠️ Missing Changeset - Based on the retrieved learnings, this new feature requires a changeset file since it adds significant functionality. The changeset bot correctly flagged this.

# Generate changeset
npx changeset add

Test Coverage Assessment

The testing is comprehensive:

  • Unit tests: task-validator.test.js with 272 additions covering validation logic
  • Integration tests: validate-tasks.test.js with CLI command testing
  • Fixture files: Multiple test scenarios including valid/invalid cases

Performance Considerations

The validation adds minimal overhead since:

  • Schemas are compiled once and reused
  • Validation only occurs during file I/O operations
  • No validation in tight loops or frequent operations

Recommendation

This is a high-quality feature implementation that significantly improves the robustness of the task management system. The dual schema approach (JSON Schema for docs, Zod for runtime) is well-thought-out, and the integration is seamless.

Approve after:

  1. Adding the required changeset file
  2. Consider addressing the schema consistency point

The implementation demonstrates strong software engineering practices with proper error handling, comprehensive testing, and maintainable code structure.

🐰
/| /|
( . .)
) (
( v )
^^ ^^ ^^


🧠 Learnings used
Learnt from: Crunchyman-ralph
PR: eyaltoledano/claude-task-master#992
File: .coderabbit.yaml:1-169
Timestamp: 2025-07-16T16:06:07.129Z
Learning: CodeRabbit configuration files like .coderabbit.yaml are internal development tooling that affect the review process but don't impact end users, so changesets are not required for these configuration additions.

Learnt from: CR
PR: eyaltoledano/claude-task-master#0
File: .cursor/rules/git_workflow.mdc:0-0
Timestamp: 2025-07-12T06:21:51.555Z
Learning: Pull Request descriptions must use the provided template, including sections for Task Overview, Subtasks Completed, Implementation Details, Testing, Breaking Changes, and Related Tasks

Learnt from: CR
PR: eyaltoledano/claude-task-master#0
File: .cursor/rules/changeset.mdc:0-0
Timestamp: 2025-07-12T06:20:29.698Z
Learning: Applies to .changeset/*.md : Every pull request containing new features, bug fixes, breaking changes, performance improvements, significant refactoring, user-facing documentation updates, dependency updates, or impactful build/tooling changes should include a changeset file.

Learnt from: CR
PR: eyaltoledano/claude-task-master#0
File: .cursor/rules/git_workflow.mdc:0-0
Timestamp: 2025-07-12T06:21:51.555Z
Learning: Pull Request titles must follow the format: 'Task <ID>: <Task Title>'

Learnt from: CR
PR: eyaltoledano/claude-task-master#0
File: .cursor/rules/dependencies.mdc:0-0
Timestamp: 2025-07-12T06:21:26.587Z
Learning: Applies to scripts/modules/dependency-manager.js : Remove references to non-existent tasks during validation

Learnt from: CR
PR: eyaltoledano/claude-task-master#0
File: .cursor/rules/tests.mdc:0-0
Timestamp: 2025-07-12T06:24:34.555Z
Learning: Applies to **/*.test.js : Verify modifications on the in-memory task objects passed to writeJSON.

Learnt from: CR
PR: eyaltoledano/claude-task-master#0
File: .cursor/rules/glossary.mdc:0-0
Timestamp: 2025-07-12T06:21:59.567Z
Learning: Guidelines for integrating new features into the Task Master CLI with tagged system considerations (new_features.mdc).

Learnt from: CR
PR: eyaltoledano/claude-task-master#0
File: .cursor/rules/tasks.mdc:0-0
Timestamp: 2025-07-12T06:23:51.375Z
Learning: Applies to scripts/modules/task-manager.js : Use consistent formatting for task files, include all task properties in text files, and format dependencies with status indicators.

Learnt from: CR
PR: eyaltoledano/claude-task-master#0
File: .cursor/rules/tasks.mdc:0-0
Timestamp: 2025-07-12T06:23:51.375Z
Learning: Applies to scripts/modules/task-manager.js : Extract tasks from PRD documents using AI, create tasks in the current tag context (defaulting to "master"), provide clear prompts to guide AI task generation, and validate/clean up AI-generated tasks.

Learnt from: CR
PR: eyaltoledano/claude-task-master#0
File: .cursor/rules/taskmaster.mdc:0-0
Timestamp: 2025-07-12T06:23:40.816Z
Learning: Applies to .taskmaster/tasks/tasks.json : Do not manually edit tasks.json; always use Taskmaster commands to update tasks.

Learnt from: CR
PR: eyaltoledano/claude-task-master#0
File: .cursor/rules/changeset.mdc:0-0
Timestamp: 2025-07-12T06:20:29.698Z
Learning: Provide both a concise changeset summary (for the changelog) and a detailed Git commit message (for repository history) when making user-relevant changes.

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.

3 participants