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 withturbohtml.parse_xml().- Raises:
ValueError – when the schema is malformed or its root is not
xs:schema.
- assert_valid(document)[source]¶
Validate the document, raising
SchemaValidationErrorwith the errors when it is invalid.
- validate(document)[source]¶
Validate a parsed document (or element), returning the full
ValidationResult.- Parameters:
- Return type:
- 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 withturbohtml.parse_xml().- Raises:
ValueError – when the schema is malformed.
- assert_valid(document)[source]¶
Validate the document, raising
SchemaValidationErrorwith the errors when it is invalid.
- validate(document)[source]¶
Validate a parsed document (or element), returning the full
ValidationResult.- Parameters:
- Return type:
- class turbohtml.validate.ValidationResult(valid: bool, errors: tuple[ValidationError, ...])[source]¶
The outcome of validating a document: a
validflag and the errors found.The result is truthy exactly when the document is valid, so
if schema.validate(doc):reads naturally.- Parameters:
valid (
bool)errors (
tuple[ValidationError,...])
- 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:
- exception turbohtml.validate.SchemaValidationError(errors)[source]¶
Raised by
XMLSchema.assert_valid()/RelaxNG.assert_valid()when a document is invalid.- Parameters:
errors (
tuple[ValidationError,...])