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

CompileContextProtocol

The slice of the compilation context the schema checks read.

IssueSeverity

How serious a validation issue is.

ValidationIssue

A single problem found while validating a document.

ValidationReport

An ordered collection of validation issues.

Functions

require_valid_report(report)

Raise ValidationError when report has errors.

report_or_log(report, severity, *, code, message[, ...])

Records an issue on report, or logs it when report is None.

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 in pyxsd.schema_checks accept 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
directive_ids: dict[str, Any]
composed_schema_roots: dict[int, Any]
composed_schema_namespaces: dict[int, str | None]
composed_element_ids: set[int]
composed_target_namespaces: set[str]
resolved_imports: set[str]
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: the IssueSeverity of 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
code: str
message: str
element: str | None = None
phase: str = 'instance'
location: pyxsd.locations.IssueLocation | None = None
constraint: pyxsd.locations.ConstraintInfo | None = None
details: dict[str, object] | None = None
location_precision: pyxsd.locations.LocationPrecision = 'unknown'
format()

Render the issue as one line, as shown by the CLI.

A location with exact text-span evidence adds source:line:column; a JSON data location adds source#pointer instead of a fabricated XML path; issues without a location keep their established rendering.

Return type:

str

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_complete is None for 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:
  • max_issues (int | None)

  • max_message_bytes (int | None)

  • diagnostics_complete (bool | None)

phase: str = 'instance'
add_error(message, *, code, element=None, phase=None, location=None, constraint=None, details=None, location_precision='unknown')

Record a fatal-severity issue.

Parameters:
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:
Return type:

None

for_phase(phase)

Only the issues attributed to phase ("schema"/"instance").

Parameters:

phase (str)

Return type:

list[ValidationIssue]

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 a pyxsd.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 None when unspecified.

None preserves 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:

int

property warning_count: int

All warnings, including those whose details exceeded the budget.

Return type:

int

property suppressed_count: int

Findings whose details were not retained (including merged reports).

Return type:

int

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_complete appears only when the report specifies it, so legacy XSD report JSON keeps its exact shape.

Return type:

dict[str, object]

to_json()

Serialize to_dict() deterministically without optional packages.

Return type:

str

property issues: list[ValidationIssue]

All recorded issues, in the order they were found.

Return type:

list[ValidationIssue]

property errors: list[ValidationIssue]

Only the error-severity issues.

Return type:

list[ValidationIssue]

property warnings: list[ValidationIssue]

Only the warning-severity issues.

Return type:

list[ValidationIssue]

property has_errors: bool

Whether any error-severity issue was recorded.

Return type:

bool

pyxsd.validation.require_valid_report(report)

Raise ValidationError when 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 the pyxsd logger at the matching level instead.

  • report: the report to record on, or None to log.

  • severity: the IssueSeverity of 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; None keeps the report’s own default phase.

Parameters:
Return type:

None