Skip to content

feat: validators return specific error classes#840

Merged
jkowalleck merged 2 commits into
mainfrom
feat/validators_return_specific_error_classes
Jul 3, 2025
Merged

feat: validators return specific error classes#840
jkowalleck merged 2 commits into
mainfrom
feat/validators_return_specific_error_classes

Conversation

@jkowalleck

@jkowalleck jkowalleck commented Jul 3, 2025

Copy link
Copy Markdown
Member

Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
@jkowalleck jkowalleck requested a review from a team as a code owner July 3, 2025 10:12
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
@jkowalleck jkowalleck added the enhancement New feature or request label Jul 3, 2025
@codacy-production

Copy link
Copy Markdown

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
+0.01% 100.00% (target: 80.00%)
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (34a11aa) 5628 5267 93.59%
Head commit (0b765bc) 5636 (+8) 5275 (+8) 93.59% (+0.01%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#840) 17 17 100.00%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

@jkowalleck jkowalleck merged commit 23a0f72 into main Jul 3, 2025
47 checks passed
@jkowalleck jkowalleck deleted the feat/validators_return_specific_error_classes branch July 3, 2025 10:24
@jkowalleck

Copy link
Copy Markdown
Member Author

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant