Skip to content

[WIP] feat!: useful validation errors#836

Draft
jkowalleck wants to merge 3 commits into
mainfrom
feat/validator_error_useful
Draft

[WIP] feat!: useful validation errors#836
jkowalleck wants to merge 3 commits into
mainfrom
feat/validator_error_useful

Conversation

@jkowalleck

@jkowalleck jkowalleck commented Jul 1, 2025

Copy link
Copy Markdown
Member

goal: have useful messages on validation errors.


TODO

  • implement solution
  • add tests

Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
@jkowalleck

Copy link
Copy Markdown
Member Author

currently work in progress.
if you want to collaborate, feel free to discuss implementations in this ticket. or pullrequest change requests pointing to the feat/validator_error_useful branch

Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
jkowalleck added a commit that referenced this pull request Jul 3, 2025
part of 
-  #827

in preparation for 
- #836

---------

Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
@italvi

italvi commented Jul 19, 2025

Copy link
Copy Markdown

@jkowalleck we already try to do this within our tool. Maybe there are some synergies possible, as we are also still facing sometimes nonsensical error message for nested dicts.

saquibsaifee added a commit to saquibsaifee/cyclonedx-python-lib that referenced this pull request Jul 14, 2026
Implements the planned improvement from PR CycloneDX#836 by the maintainer.

- ValidationError gains structured fields: message (str), path (tuple),
  and data (raw backend object) — replacing the bare data-only shape
- JsonValidationError._make_from_jsve recurses into nested jsonschema
  context errors to surface the most specific leaf failure for oneOf/anyOf
- XmlValidationError._make_from_xle maps lxml _LogEntry.message and path
- __repr__ and __str__ now emit the human-readable message field

Closes CycloneDX#827
saquibsaifee added a commit to saquibsaifee/cyclonedx-python-lib that referenced this pull request Jul 14, 2026
Implements the planned improvement from PR CycloneDX#836 by the maintainer.

- ValidationError gains structured fields: message (str), path (tuple),
  and data (raw backend object) — replacing the bare data-only shape
- JsonValidationError._make_from_jsve recurses into nested jsonschema
  context errors to surface the most specific leaf failure for oneOf/anyOf
- XmlValidationError._make_from_xle maps lxml _LogEntry.message and path
- __repr__ and __str__ now emit the human-readable message field

Closes CycloneDX#827

Signed-off-by: Saquib Saifee <saquibsaifee2@gmail.com>
saquibsaifee added a commit to saquibsaifee/cyclonedx-python-lib that referenced this pull request Jul 14, 2026
PRs already in main that this builds on:
- CycloneDX#834 (merged): added all_errors support and JsonValidationError /
  XmlValidationError subclass stubs
- CycloneDX#840 (merged): refined the subclass stubs further
- CycloneDX#836 (WIP by maintainer): placeholder for this exact work

Changes
-------
ValidationError (__init__.py)
- Add message (str): human-readable, bounded error message
- Add path (tuple[str|int, ...]): location of the offending value
- Keep data (Any): raw backend object, unchanged for backward compat
- __str__ now returns message; __repr__ is structured

JsonValidationError (json.py)
- __get_most_relevant_jsve: recurse into nested jsonschema context
  errors to surface the deepest leaf failure for oneOf/anyOf checks,
  instead of emitting the generic parent message
- _shorten_message: (1) replace a bloated repr(instance) with a
  head...tail summary for uniqueItems/large-document failures, then
  (2) hard-cap the whole message at 256 chars + ellipsis

XmlValidationError (xml.py)
- _shorten_xml_message: hard-cap lxml _LogEntry.message at 256 chars,
  preventing the full SPDX enumeration set from appearing verbatim
- path populated from _LogEntry.path (XPath location string)

Before vs after (invalid-license-id test files from issue CycloneDX#827)
  JSON str(error): 111,672 chars  ->  257 chars
  XML  str(error):  13,430 chars  ->  257 chars
  error.message:   AttributeError ->  bounded str
  error.path:      AttributeError ->  structured tuple

Tests (test_validation_json.py, test_validation_xml.py)
- Assert error is the correct subtype (JsonValidationError /
  XmlValidationError)
- Assert .message is a str and .path is a tuple
- Assert len(message) <= 257 across every invalid test file for every
  schema version

Closes CycloneDX#827

Signed-off-by: Saquib Saifee <saquibsaifee2@gmail.com>
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.

Validation errors are hard to present safely to the user (missing abstraction)

2 participants