pyxsd.validation ================ .. py:module:: pyxsd.validation .. autoapi-nested-parse:: 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 :class:`ValidationReport` instead of being printed immediately. The CLI prints the report to standard error after the run; library users inspect the ``report`` of the :class:`~pyxsd.document.Document` (a merged view of the owning schema's compilation issues and the document's own binding issues) or :attr:`~pyxsd.schema.Schema.report` for the schema phase alone. Attributes ---------- .. autoapisummary:: pyxsd.validation.logger Classes ------- .. autoapisummary:: pyxsd.validation.CompileContextProtocol pyxsd.validation.IssueSeverity pyxsd.validation.ValidationIssue pyxsd.validation.ValidationReport Functions --------- .. autoapisummary:: pyxsd.validation.require_valid_report pyxsd.validation.report_or_log Module Contents --------------- .. py:data:: logger .. py:class:: CompileContextProtocol The slice of the compilation context the schema checks read. Annotation-only structural view of :class:`~pyxsd.schema_composition.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. .. py:attribute:: report :type: ValidationReport .. py:attribute:: mode :type: Any .. py:attribute:: classes :type: dict[str, type[pyxsd.schema_base.SchemaBase]] .. py:attribute:: schema_context :type: pyxsd.schema_context.SchemaContext .. py:attribute:: namespace_context :type: Any .. py:attribute:: directive_ids :type: dict[str, Any] .. py:attribute:: composed_schema_roots :type: dict[int, Any] .. py:attribute:: composed_schema_namespaces :type: dict[int, str | None] .. py:attribute:: composed_element_ids :type: set[int] .. py:attribute:: composed_target_namespaces :type: set[str] .. py:attribute:: resolved_imports :type: set[str] .. py:class:: IssueSeverity How serious a validation issue is. .. py:attribute:: ERROR :value: 'error' .. py:attribute:: WARNING :value: 'warning' .. py:class:: ValidationIssue A single problem found while validating a document. - ``severity``: the :class:`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. .. py:attribute:: severity :type: IssueSeverity .. py:attribute:: code :type: str .. py:attribute:: message :type: str .. py:attribute:: element :type: str | None :value: None .. py:attribute:: phase :type: str :value: 'instance' .. py:attribute:: location :type: pyxsd.locations.IssueLocation | None :value: None .. py:attribute:: constraint :type: pyxsd.locations.ConstraintInfo | None :value: None .. py:attribute:: details :type: dict[str, object] | None :value: None .. py:attribute:: location_precision :type: pyxsd.locations.LocationPrecision :value: 'unknown' .. py:method:: 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. .. py:class:: 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. .. py:attribute:: phase :type: str :value: 'instance' .. py:method:: add_error(message, *, code, element = None, phase = None, location = None, constraint = None, details = None, location_precision = 'unknown') Record a fatal-severity issue. .. py:method:: add_warning(message, *, code, element = None, phase = None, location = None, constraint = None, details = None, location_precision = 'unknown') Record a recoverable-severity issue. .. py:method:: for_phase(phase) Only the issues attributed to *phase* (``"schema"``/``"instance"``). .. py:method:: extend(other) Appends every issue from *other* to this report. Used to merge the phase reports of one run into a single collection: :meth:`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 :meth:`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. .. py:property:: diagnostics_complete :type: 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. .. py:property:: error_count :type: int All errors, including those whose details exceeded the budget. .. py:property:: warning_count :type: int All warnings, including those whose details exceeded the budget. .. py:property:: suppressed_count :type: int Findings whose details were not retained (including merged reports). .. py:method:: 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. .. py:method:: to_json() Serialize :meth:`to_dict` deterministically without optional packages. .. py:property:: issues :type: list[ValidationIssue] All recorded issues, in the order they were found. .. py:property:: errors :type: list[ValidationIssue] Only the error-severity issues. .. py:property:: warnings :type: list[ValidationIssue] Only the warning-severity issues. .. py:property:: has_errors :type: bool Whether any error-severity issue was recorded. .. py:function:: require_valid_report(report) Raise :class:`~pyxsd.exceptions.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. .. py:function:: 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 :meth:`ValidationReport.add_error` / :meth:`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 :class:`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.