-
Couldn't load subscription status.
- Fork 2.3k
Feature/json schema validation #794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Feature/json schema validation #794
Conversation
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.
|
There was a problem hiding this 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 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more changes requested
| 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'); |
There was a problem hiding this comment.
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
There was a problem hiding this 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
There was a problem hiding this comment.
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
| // 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)}`); | ||
| }); | ||
| } | ||
| } |
There was a problem hiding this comment.
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 ?
- 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`.
|
@CodeRabbit please review and give a summary of this PR |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: Length of output: 4742 🏁 Script executed: Length of output: 6353 🏁 Script executed: Length of output: 52839 🏁 Script executed: Length of output: 9619 @Crunchyman-ralph, I've completed a comprehensive review of this JSON schema validation feature. Here's my analysis: PR SummaryThis 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 Added1. Schema Definition (Dual Approach)
2. Validation Engine
3. CLI Integration
4. Seamless Integration
Technical Strengths✅ Comprehensive validation coverage - covers all task properties and file structure Areas for Improvement1. Schema Consistency 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 Required Actions# Generate changeset
npx changeset addTest Coverage AssessmentThe testing is comprehensive:
Performance ConsiderationsThe validation adds minimal overhead since:
RecommendationThis 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:
The implementation demonstrates strong software engineering practices with proper error handling, comprehensive testing, and maintainable code structure. 🐰 🧠 Learnings used |
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
ajvandajv-formatsfor schema validation.schemas/task.schema.json: Defines the structure and rules for individual task objects.schemas/tasks-file.schema.json: Defines the structure for the overalltasks.jsonfile, including tasks arrays and metadata.2. Validation Utilities
scripts/modules/task-validator.jsfor:3. CLI Enhancements
validate-tasks4. Task Management Improvements
5. Dependency Updates
package.jsonandpackage-lock.jsonwith new dependencies.6. Testing
Motivation
Usage
Additional Notes
tasks.jsonare now validated.Checklist