Validators API Reference¶
Validation classes and utilities.
XSD Validator¶
XSDValidator
¶
Bases: BaseValidator
XSD schema validator for C-CDA documents.
Validates C-CDA documents against the official HL7 C-CDA XSD schemas.
Usage
Use default schemas (auto-downloads if needed)¶
validator = XSDValidator() result = validator.validate(document) if result.is_valid: ... print("Document is valid!") else: ... print("Validation errors:") ... for error in result.errors: ... print(f" - {error}")
Or provide custom schema path¶
validator = XSDValidator("/path/to/schemas/CDA.xsd") result = validator.validate(document)
Note
XSD schemas are automatically downloaded on first use if not present. Set auto_download=False to disable automatic downloads.
Attributes¶
schema_location
property
¶
Get the schema file location.
Functions¶
__init__(schema_path=None, auto_download=True)
¶
Initialize XSD validator with schema file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema_path
|
Optional[Union[str, Path]]
|
Path to the CDA.xsd schema file. If None, uses default location and auto-downloads if needed. |
None
|
auto_download
|
bool
|
Automatically download schemas if missing. Default: True. Set to False to disable automatic downloads. |
True
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If schema file doesn't exist and auto_download=False |
XMLSchemaParseError
|
If schema is invalid |
Note
On first use, XSD schemas (~2MB) will be automatically downloaded from HL7's official repository. This may take a few moments.
validate(document)
¶
Validate a C-CDA document against XSD schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
Union[_Element, str, bytes, Path]
|
Document to validate. Can be: - etree._Element: Parsed XML element - str: XML string or file path - bytes: XML bytes - Path: Path to XML file |
required |
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult with errors from schema validation |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file path doesn't exist |
XMLSyntaxError
|
If document is not well-formed XML |
validate_file(file_path)
¶
Convenience method to validate a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Union[str, Path]
|
Path to XML file |
required |
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult with errors from schema validation |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist |
validate_string(xml_string)
¶
Convenience method to validate an XML string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xml_string
|
str
|
XML document as string |
required |
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult with errors from schema validation |
validate_bytes(xml_bytes)
¶
Convenience method to validate XML bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xml_bytes
|
bytes
|
XML document as bytes |
required |
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult with errors from schema validation |
Schematron Validator¶
SchematronValidator
¶
Bases: BaseValidator
Schematron validator for C-CDA documents.
Validates C-CDA documents using ISO Schematron rules for business logic, template conformance, and ONC certification requirements.
Usage
Use default HL7 C-CDA R2.1 Schematron (auto-cleaned version)¶
validator = SchematronValidator() result = validator.validate(document) if result.is_valid: ... print("Document passes Schematron validation!") else: ... print("Validation errors:") ... for error in result.errors: ... print(f" - {error}")
Use custom Schematron file¶
validator = SchematronValidator("/path/to/custom.sch") result = validator.validate(document)
Note
Schematron validation requires both the .sch file and voc.xml vocabulary file. These are automatically downloaded and cleaned on first use.
The official HL7 Schematron file contains IDREF errors that prevent lxml from loading it. This validator automatically uses a cleaned version that fixes these errors while preserving all validation rules.
Source code in ccdakit/validators/schematron.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 | |
Attributes¶
schematron_location
property
¶
Get the Schematron file location.
validation_phase
property
¶
Get the validation phase being used.
Functions¶
__init__(schematron_path=None, phase=None, auto_download=True)
¶
Initialize Schematron validator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schematron_path
|
Optional[Union[str, Path]]
|
Path to Schematron file (.sch). If None, uses default HL7 C-CDA R2.1 Schematron. |
None
|
phase
|
Optional[str]
|
Schematron phase to use (e.g., "errors", "warnings"). If None, validates all phases. |
None
|
auto_download
|
bool
|
Automatically download Schematron files if missing. Default: True. Set to False to disable automatic downloads. |
True
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If schematron file doesn't exist and auto_download=False |
SchematronParseError
|
If schematron is invalid |
Note
On first use, Schematron files (~63MB) will be automatically downloaded from HL7's official GitHub repository. This may take a few moments.
Source code in ccdakit/validators/schematron.py
validate(document)
¶
Validate a C-CDA document against Schematron rules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
Union[_Element, str, bytes, Path]
|
Document to validate. Can be: - etree._Element: Parsed XML element - str: XML string or file path - bytes: XML bytes - Path: Path to XML file |
required |
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult with Schematron validation findings |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file path doesn't exist |
XMLSyntaxError
|
If document is not well-formed XML |
Source code in ccdakit/validators/schematron.py
validate_file(file_path)
¶
Convenience method to validate a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Union[str, Path]
|
Path to XML file |
required |
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult with Schematron validation findings |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist |
Source code in ccdakit/validators/schematron.py
validate_string(xml_string)
¶
Convenience method to validate an XML string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xml_string
|
str
|
XML document as string |
required |
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult with Schematron validation findings |
Source code in ccdakit/validators/schematron.py
validate_bytes(xml_bytes)
¶
Convenience method to validate XML bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xml_bytes
|
bytes
|
XML document as bytes |
required |
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult with Schematron validation findings |
Source code in ccdakit/validators/schematron.py
Base Validator¶
BaseValidator
¶
Bases: ABC
Abstract base class for C-CDA validators.
All validators should inherit from this class and implement the validate method.
Source code in ccdakit/validators/base.py
Functions¶
validate(document)
abstractmethod
¶
Validate a C-CDA document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
Union[_Element, str, bytes, Path]
|
Document to validate. Can be: - etree._Element: Parsed XML element - str: XML string or file path - bytes: XML bytes - Path: Path to XML file |
required |
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult with errors, warnings, and info messages |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file path doesn't exist |
XMLSyntaxError
|
If document is not well-formed XML |
Source code in ccdakit/validators/base.py
Validation Rules¶
ValidationRule
¶
Bases: ABC
Base class for custom validation rules.
Example
class MyCustomRule(ValidationRule): def init(self): super().init( name="my_custom_rule", description="Validates custom business logic" )
def validate(self, document: etree._Element) -> List[ValidationIssue]:
issues = []
# Implement validation logic
return issues
Source code in ccdakit/validators/rules.py
Functions¶
__init__(name, description)
¶
Initialize validation rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique identifier for the rule |
required |
description
|
str
|
Human-readable description of what the rule checks |
required |
Source code in ccdakit/validators/rules.py
validate(document)
abstractmethod
¶
Apply rule to document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
_Element
|
Parsed C-CDA XML document element |
required |
Returns:
| Type | Description |
|---|---|
List[ValidationIssue]
|
List of validation issues found (empty list if valid) |
Source code in ccdakit/validators/rules.py
Validation rule classes are available in:
- ccdakit.validators.rules - Base rules and composites
- ccdakit.validators.common_rules - Common reusable rules
See the Validation Guide for usage examples.
Schema Manager¶
SchemaManager
¶
Manager for C-CDA XSD schemas.
Helps with schema discovery, downloading, and path management.
Source code in ccdakit/validators/utils.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
Functions¶
__init__(schema_dir=None)
¶
Initialize schema manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema_dir
|
Optional[Path]
|
Directory containing schemas. Defaults to project's schemas/ directory. |
None
|
Source code in ccdakit/validators/utils.py
is_installed()
¶
Check if C-CDA schemas are installed.
Returns:
| Type | Description |
|---|---|
bool
|
True if CDA.xsd exists in schema directory |
get_cda_schema_path()
¶
Get path to main CDA.xsd schema file.
Returns:
| Type | Description |
|---|---|
Path
|
Path to CDA.xsd (may not exist) |
get_schema_info()
¶
Get information about installed schemas.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with schema installation status and paths |
Source code in ccdakit/validators/utils.py
download_schemas(version='R2.1', url=None, force=False)
¶
Download C-CDA schemas from HL7.
Note: This is a helper function, but schemas may need to be downloaded manually from HL7's website due to licensing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
str
|
C-CDA version (R2.1 or R2.0) |
'R2.1'
|
url
|
Optional[str]
|
Custom download URL (overrides version) |
None
|
force
|
bool
|
Force re-download even if schemas exist |
False
|
Returns:
| Type | Description |
|---|---|
Tuple[bool, str]
|
Tuple of (success: bool, message: str) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If version is not supported |
Source code in ccdakit/validators/utils.py
print_installation_instructions()
¶
Print instructions for manually downloading schemas.