pyxsd.validation¶
Structured validation reporting.
While an XML instance is bound to the classes generated from a schema,
recoverable problems (missing attributes, wrong element order, facet
violations) are collected into a ValidationReport instead of
being printed immediately. The CLI prints the report to standard error
after the run; library users inspect the report of the
Document (a merged view of the owning
schema’s compilation issues and the document’s own binding issues)
or report for the schema phase alone.
Attributes¶
Classes¶
The slice of the compilation context the schema checks read. |
|
How serious a validation issue is. |
|
A single problem found while validating a document. |
|
An ordered collection of validation issues. |
Functions¶
|
Raise |
|
Records an issue on report, or logs it when report is |
Module Contents¶
- pyxsd.validation.logger¶
- class pyxsd.validation.CompileContextProtocol¶
The slice of the compilation context the schema checks read.
Annotation-only structural view of
CompositionContext, which satisfies it without inheriting from it: the declaration and derivation checks inpyxsd.schema_checksaccept this protocol, so they depend on the attributes they actually touch rather than on the concrete pipeline context.Members (beyond the four the brief names) are exactly the attributes the checks functions read.
- report: ValidationReport¶
- mode: Any¶
- classes: dict[str, type[pyxsd.schema_base.SchemaBase]]¶
- schema_context: pyxsd.schema_context.SchemaContext¶
- namespace_context: Any¶
- class pyxsd.validation.IssueSeverity¶
How serious a validation issue is.
- ERROR = 'error'¶
- WARNING = 'warning'¶
- class pyxsd.validation.ValidationIssue¶
A single problem found while validating a document.
severity: theIssueSeverityof the issue.code: a short, stable, machine-readable tag describing the kind of problem (for example"missing-attribute"or"order"). Codes are documented in the validation guide and are safe to match on in tooling.message: a human-readable, single-line description.element: the name of the element or type the issue was found in, when known.phase: the pipeline stage that produced the issue, either"schema"(reading the XSD, composing included schemas, building the generated classes) or"instance"(binding the XML document). Tooling and the conformance runner use this to check each stage independently.
- severity: IssueSeverity¶
- location: pyxsd.locations.IssueLocation | None = None¶
- constraint: pyxsd.locations.ConstraintInfo | None = None¶
- location_precision: pyxsd.locations.LocationPrecision = 'unknown'¶
- class pyxsd.validation.ValidationReport(*, max_issues=None, max_message_bytes=None, diagnostics_complete=None)¶
An ordered collection of validation issues.
A schema owns the report of its compilation; each document parsed from it gets a fresh instance report, and the document’s merged report presents both phases in order. The binding machinery fills the reports in as it walks the schema and instance documents.
diagnostics_completeisNonefor the existing XSD contract. RELAX NG reports set it to a boolean so callers can distinguish a complete diagnostic report from one whose retained issue details were suppressed by a retention budget. Merged reports apply the documented dominance rule.- Parameters:
- add_error(message, *, code, element=None, phase=None, location=None, constraint=None, details=None, location_precision='unknown')¶
Record a fatal-severity issue.
- Parameters:
message (str)
code (str)
element (str | None)
phase (str | None)
location (pyxsd.locations.IssueLocation | None)
constraint (pyxsd.locations.ConstraintInfo | None)
location_precision (pyxsd.locations.LocationPrecision)
- Return type:
None
- add_warning(message, *, code, element=None, phase=None, location=None, constraint=None, details=None, location_precision='unknown')¶
Record a recoverable-severity issue.
- Parameters:
message (str)
code (str)
element (str | None)
phase (str | None)
location (pyxsd.locations.IssueLocation | None)
constraint (pyxsd.locations.ConstraintInfo | None)
location_precision (pyxsd.locations.LocationPrecision)
- Return type:
None
- for_phase(phase)¶
Only the issues attributed to phase (
"schema"/"instance").- Parameters:
phase (str)
- Return type:
- extend(other)¶
Appends every issue from other to this report.
Used to merge the phase reports of one run into a single collection:
pyxsd.schema.Schema.parse()merges the schema-phase and instance-phase issues into the document’s report, and a caller adopting the findings of apyxsd.document.Document.revalidate()run merges that document’s report the same way. The issue objects are shared, not copied; the other report is left unchanged.- Parameters:
other (ValidationReport)
- Return type:
None
- property diagnostics_complete: bool | None¶
Whether retained diagnostics are complete, or
Nonewhen unspecified.Nonepreserves the XSD wire contract. RELAX NG reports set a boolean from actual retention: suppressed findings make it false. A merge can never upgrade unspecified evidence or override an incomplete contribution.- Return type:
bool | None
- property error_count: int¶
All errors, including those whose details exceeded the budget.
- Return type:
- property warning_count: int¶
All warnings, including those whose details exceeded the budget.
- Return type:
- property suppressed_count: int¶
Findings whose details were not retained (including merged reports).
- Return type:
- to_dict()¶
Return JSON-safe details and complete counts; decimals remain strings.
Unsupported detail values raise TypeError rather than falling back to a potentially lossy or executable object’s string representation.
diagnostics_completeappears only when the report specifies it, so legacy XSD report JSON keeps its exact shape.
- property issues: list[ValidationIssue]¶
All recorded issues, in the order they were found.
- Return type:
- property errors: list[ValidationIssue]¶
Only the error-severity issues.
- Return type:
- property warnings: list[ValidationIssue]¶
Only the warning-severity issues.
- Return type:
- pyxsd.validation.require_valid_report(report)¶
Raise
ValidationErrorwhen report has errors.The shared
require_valid()implementation for compiled schema types, so the message shape and attached report cannot drift between them.- Parameters:
report (ValidationReport)
- Return type:
None
- pyxsd.validation.report_or_log(report, severity, *, code, message, element=None, phase=None, constraint=None, location=None, location_precision='unknown', details=None)¶
Records an issue on report, or logs it when report is
None.The shared sink for validation diagnostics that may run with or without an owning report attached (hand-written overlay classes, representatives built in isolation). With a report, severity selects
ValidationReport.add_error()/ValidationReport.add_warning(); without one, the issue is logged on thepyxsdlogger at the matching level instead.report: the report to record on, orNoneto log.severity: theIssueSeverityof the issue.code: the machine-readable issue code.message: the human-readable description.element: the element or type the issue was found in, when known.phase: the pipeline stage to attribute the issue to;Nonekeeps the report’s own default phase.
- Parameters:
report (ValidationReport | None)
severity (IssueSeverity)
code (str)
message (str)
element (str | None)
phase (str | None)
constraint (pyxsd.locations.ConstraintInfo | None)
location (pyxsd.locations.IssueLocation | None)
location_precision (pyxsd.locations.LocationPrecision)
- Return type:
None