Validation

XMLSchema and RelaxNG validate a document parsed with turbohtml.parse_xml() against an XSD 1.0 or RELAX NG schema. A schema compiles once in the C core; each validate() call walks the instance tree there and returns a ValidationResult – a valid flag plus a tuple of ValidationError records, one per violation, each locating the offending node. The Python layer only shapes the input and wraps the C result.

class turbohtml.validate.XMLSchema(source)[source]

A compiled XSD 1.0 schema.

Parameters:

source (str | Node) – the schema as XSD text, or a schema document parsed with turbohtml.parse_xml().

Raises:

ValueError – when the schema is malformed or its root is not xs:schema.

assert_valid(document)[source]

Validate the document, raising SchemaValidationError with the errors when it is invalid.

Parameters:

document (Document | Node)

Return type:

None

is_valid(document)[source]

Whether the document satisfies the schema.

Parameters:

document (Document | Node)

Return type:

bool

validate(document)[source]

Validate a parsed document (or element), returning the full ValidationResult.

Parameters:

document (Document | Node)

Return type:

ValidationResult

class turbohtml.validate.RelaxNG(source)[source]

A compiled RELAX NG schema (XML syntax).

Parameters:

source (str | Node) – the schema as RELAX NG text, or a schema document parsed with turbohtml.parse_xml().

Raises:

ValueError – when the schema is malformed.

assert_valid(document)[source]

Validate the document, raising SchemaValidationError with the errors when it is invalid.

Parameters:

document (Document | Node)

Return type:

None

is_valid(document)[source]

Whether the document satisfies the schema.

Parameters:

document (Document | Node)

Return type:

bool

validate(document)[source]

Validate a parsed document (or element), returning the full ValidationResult.

Parameters:

document (Document | Node)

Return type:

ValidationResult

class turbohtml.validate.ValidationResult(valid: bool, errors: tuple[ValidationError, ...])[source]

The outcome of validating a document: a valid flag and the errors found.

The result is truthy exactly when the document is valid, so if schema.validate(doc): reads naturally.

Parameters:
valid: bool

Alias for field number 0

errors: tuple[ValidationError, ...]

Alias for field number 1

class turbohtml.validate.ValidationError(message: str, path: str, line: int, type: str)[source]

One schema violation.

Parameters:
  • message (str) – a human-readable description of what failed.

  • path (str) – the /root/child document-order path of the offending node.

  • line (int) – the 1-based source line of the node, or 0 when unknown.

  • type (str) – the violation category – "structure", "datatype", or "facet".

message: str

Alias for field number 0

path: str

Alias for field number 1

line: int

Alias for field number 2

type: str

Alias for field number 3

exception turbohtml.validate.SchemaValidationError(errors)[source]

Raised by XMLSchema.assert_valid() / RelaxNG.assert_valid() when a document is invalid.

Parameters:

errors (tuple[ValidationError, ...])