Skip to content

fix(validation): report unreadable input or schema, not a traceback - #534

Merged
jbonofre merged 1 commit into
apache:mainfrom
ayushtkn:fix/validation-report-unreadable-input
Oct 8, 2026
Merged

jbonofre merged 1 commit into
apache:mainfrom
ayushtkn:fix/validation-report-unreadable-input

Conversation

@ayushtkn

@ayushtkn ayushtkn commented Oct 7, 2026

Copy link
Copy Markdown
Member

Summary

The exists() checks pass for a path that cannot be read as text, so these inputs reached a traceback while every neighbouring bad input got a clean line — a directory, a file without read permission, a binary file, and a --schema that is any of those or not JSON.

Read each file first and parse the text after, so one guard covers both files and neither read nests inside a parse:

Error: Could not read validation: [Errno 21] Is a directory: 'validation'
Error: Invalid JSON in schema junk.json: Expecting value: line 1 column 1
Error: model.yaml is not valid UTF-8 text: 'utf-8' codec can't decode ...

Exit status was already 1 in each case and is unchanged, as are the messages for a missing file, a missing schema and malformed YAML.

Related Issues

Checklist

Specification

  • Spec changes are included in core-spec/ and follow the existing structure
  • Spec changes have been discussed on the mailing list or in a linked issue
  • Breaking changes to the spec are clearly called out in the summary

Ontology

  • Ontology changes in ontology/ are consistent with spec changes
  • New or modified terms are defined and documented

Converters

  • Converter logic in converters/ is updated to reflect spec or ontology changes
  • New converters include tests under the converter's test directory
  • If adding a new converter, .github/labeler.yml is updated with the new path

Validation

  • Validation rules in validation/ are updated if the spec changed
  • New validation cases are covered by tests

Documentation

  • docs/ is updated to reflect any user-facing changes
  • New features or behaviors are documented with examples where appropriate
  • CONTRIBUTING.md is updated if the contribution process changed

Examples

  • examples/ are added or updated for any new spec constructs or converter support

Tests

  • All existing tests pass (pytest / CI green)
  • New functionality is covered by tests

Compliance

  • ASF license headers are present on all new source files
  • No third-party dependencies are added without PMC/IPMC approval
  • If third-party source code is vendored/copied (not just declared as a dependency), NOTICE and/or LICENSE have been updated per ASF policy

Copilot AI balanced review requested due to automatic review settings October 7, 2026 21:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Parsing YAML from a string removes the filename from parser diagnostics, contradicting the promised message preservation.

1 open finding
What changed in this PR

Improves validator diagnostics for unreadable, non-UTF-8, and malformed schema inputs.

Changes:

  • Adds guarded UTF-8 file reading and JSON parsing.
  • Adds integration tests for invalid input and schema paths.
File Description
validation/​validate.py Handles file-read, decoding, and schema parsing errors.
validation/​test_validate.py Tests new failure diagnostics and exit statuses.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread validation/validate.py Outdated
@ayushtkn
ayushtkn force-pushed the fix/validation-report-unreadable-input branch from ec257c9 to c0ddacf Compare October 7, 2026 21:27
@ayushtkn
ayushtkn requested a balanced review from Copilot October 7, 2026 21:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Constructor-time YAML reader errors still lose the filename, and permission tests are not portable.

2 open findings
1 resolved since last review

🧠 Review effort: Balanced

Comment thread validation/test_validate.py Outdated
Comment thread validation/validate.py Outdated
The exists() checks pass for a path that cannot be read as text, so these
inputs reached a traceback while every neighbouring bad input got a clean
line — a directory, a file without read permission, a binary file, and a
--schema that is any of those or not JSON.

Read each file first and parse the text after, so one guard covers both
files and neither read nests inside a parse:

  Error: Could not read validation: [Errno 21] Is a directory: 'validation'
  Error: Invalid JSON in schema junk.json: Expecting value: line 1 column 1
  Error: model.yaml is not valid UTF-8 text: 'utf-8' codec can't decode ...

The model is parsed through load_yaml_named, which names the loader so the
path stays in every parser mark: PyYAML names a str source "<unicode
string>", and a named stream would carry the path but drop the offending
line, because Reader.get_mark() attaches its buffer only for a str source.

Exit status was already 1 in each case and is unchanged, as are the
messages for a missing file, a missing schema and malformed YAML.
@ayushtkn
ayushtkn force-pushed the fix/validation-report-unreadable-input branch from c0ddacf to 96f90c0 Compare October 7, 2026 21:36
@ayushtkn
ayushtkn requested a balanced review from Copilot October 7, 2026 21:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The implementation is focused, preserves existing behavior, and thoroughly tests the new failure handling.

0 open findings

2 resolved since last review

🧠 Review effort: Balanced

@kayemkim kayemkim left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ran this merged onto current main (c460302) through the validation workflow on 3.11 to 3.14, all green. I also fed the eight inputs to main: a directory, a file without read permission and a binary file, each as the model and as the schema, plus a schema that is not JSON and a model with a NUL byte. Seven end in a traceback on main, the NUL case was already a clean line, and on this branch each prints the single Error line and exits 1, as the description says. The 26 model, ontology and mapping documents in the repository give byte-identical output on both trees, and nothing outside validation/ imports the loader or matches on these messages, so the wording changes stay inside the CLI. Merges cleanly with #271 as well. Good to go from my side.

@MonkeyCanCode
MonkeyCanCode self-requested a review October 8, 2026 06:05
@MonkeyCanCode

Copy link
Copy Markdown
Contributor

Thanks for the PR @ayushtkn . I will take a look later this weekend.

@jbonofre
jbonofre self-requested a review October 8, 2026 16:06

@jbonofre jbonofre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

Turning unreadable input, bad encoding, malformed YAML and invalid schema JSON into clear messages with exit code 1 is a good improvement. I checked the following:

  • OSError and UnicodeDecodeError are handled separately, so neither handler hides the other.
  • Setting loader.name means YAML errors name the file and keep the offending line.
  • dispose() runs in a finally.
  • The new tests match the actual messages and exit codes.

Nit: a schema that is valid JSON but not an object would still fail later. That was already the case before this PR, so it can be a follow-up 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants