Skip to content

Add line number tracking for schema validation errors #64

@michaeldistel

Description

@michaeldistel

Problem

When schema validation fails (e.g., invalid JSON, missing fields, incorrect types), error messages only show what's wrong but not where in the schema file the issue is located. Users have to manually search through the schema to find the problematic line.

Current behavior:

Schema validation failed: Invalid field type
Field 'amount' has invalid type

Desired behavior:

Schema validation failed: Invalid field type

Field 'amount':
  Location: invoice.schema.json:15:5
  Error: Invalid type 'int' (use 'integer' instead)

Proposed Solution

Implement position tracking during JSON parsing to map validation errors back to source locations:

  1. Parser enhancement: Use a JSON parser that preserves position information (e.g., json-source-map, jsonpos, or custom AST-based parser)
  2. Error context: Augment validation errors with line/column numbers
  3. Code snippets: Optionally show the relevant lines from the schema file with error highlighting

Acceptance Criteria

  • Schema validation errors include file path, line number, and column
  • Error messages show a snippet of the problematic schema section
  • Position tracking works for all schema validation error types
  • Minimal performance impact (consider lazy loading position data)
  • Works for both file-based and string-based schema inputs

Implementation Notes

This is a lower-priority enhancement because:

  • Most schemas are small enough to scan visually
  • Current error messages are already descriptive
  • Would add dependency or complexity for position tracking
  • Alternative: Users can use JSON validators/linters in their editors

Could be combined with:

Related

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions