Validators API¶
Validation engine and result types for DPP validation.
ValidationEngine¶
The main validation engine supporting seven-layer validation.
dppvalidator.validators.ValidationEngine
¶
Seven-layer validation engine for Digital Product Passports.
Provides configurable validation through seven layers: 1. Schema validation (JSON Schema Draft 2020-12) 2. Model validation (Pydantic v2) 3. Semantic validation (Business rules) 4. JSON-LD validation (Context expansion and term resolution) 5. Vocabulary validation (External code lists and ontologies) 6. Plugin validation (Custom validator plugins) 7. Signature verification (Verifiable Credential proofs)
Following the Result pattern, validation never raises exceptions.
Check result.valid and inspect result.errors for details.
Source code in src/dppvalidator/validators/engine.py
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 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | |
__init__(schema_version='auto', strict_mode=False, validate_vocabularies=False, layers=None, validate_jsonld=False, verify_signatures=False, load_plugins=True, max_input_size=None, enable_shacl=False)
¶
Initialize the validation engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema_version
|
str
|
UNTP DPP schema version to validate against. Use "auto" to detect version from input data (default). |
'auto'
|
strict_mode
|
bool
|
If True, enables strict JSON Schema validation |
False
|
validate_vocabularies
|
bool
|
If True, validates external vocabulary values |
False
|
layers
|
list[Literal['schema', 'model', 'semantic', 'jsonld']] | None
|
Specific layers to run. None means all layers. |
None
|
validate_jsonld
|
bool
|
If True, enables JSON-LD semantic validation (requires pyld) |
False
|
verify_signatures
|
bool
|
If True, verifies VC signatures (requires cryptography) |
False
|
load_plugins
|
bool
|
If True, discovers and loads plugin validators |
True
|
max_input_size
|
int | None
|
Maximum input size in bytes. None uses default (10MB). Set to 0 to disable size limits. |
None
|
enable_shacl
|
bool
|
If True, enables SHACL validation against official CIRPASS-2 shapes. Requires: uv add dppvalidator[rdf] or pip install dppvalidator[rdf] |
False
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If optional features are enabled but dependencies not installed: - enable_shacl=True requires [rdf] extra - validate_jsonld=True requires pyld (included in base install) - verify_signatures=True requires cryptography (included in base install) |
Source code in src/dppvalidator/validators/engine.py
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 | |
validate(data, *, fail_fast=False, max_errors=100)
¶
Validate DPP data through configured layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any] | str | Path
|
Raw JSON dict, JSON string, or path to JSON file |
required |
fail_fast
|
bool
|
Stop on first error if True |
False
|
max_errors
|
int
|
Maximum errors to collect before stopping |
100
|
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult with parsed passport if valid |
Source code in src/dppvalidator/validators/engine.py
validate_async(data)
async
¶
Validate data asynchronously (thread-offloaded).
This method wraps the synchronous validate() method using
asyncio.to_thread() to avoid blocking the event loop. Use this
for integrating with async frameworks like FastAPI or aiohttp.
Note
For natively async operations (network I/O), use validate_deep()
which uses httpx.AsyncClient for non-blocking HTTP requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Raw JSON dict |
required |
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult |
Example
async def handler(request): ... result = await engine.validate_async(request.json()) ... return {"valid": result.valid}
Source code in src/dppvalidator/validators/engine.py
validate_batch(items, *, concurrency=10)
async
¶
Validate multiple items concurrently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
list[dict[str, Any]]
|
List of raw JSON dicts |
required |
concurrency
|
int
|
Maximum concurrent validations |
10
|
Returns:
| Type | Description |
|---|---|
list[ValidationResult]
|
List of ValidationResults in same order as input |
Source code in src/dppvalidator/validators/engine.py
validate_deep(data, *, max_depth=3, follow_links=None, timeout=30.0, auth_header=None)
async
¶
Perform deep/recursive validation following linked documents.
Crawls the supply chain by following links in the DPP and validates each linked document, building a complete validation graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Root DPP document data |
required |
max_depth
|
int
|
Maximum depth to traverse (0 = root only) |
3
|
follow_links
|
list[str] | None
|
JSON paths to follow for links (uses defaults if None) |
None
|
timeout
|
float
|
HTTP request timeout in seconds |
30.0
|
auth_header
|
dict[str, str] | None
|
Authorization headers for authenticated requests |
None
|
Returns:
| Type | Description |
|---|---|
DeepValidationResult
|
DeepValidationResult with all validation results and link graph |
Source code in src/dppvalidator/validators/engine.py
validate_file(path)
¶
Validate a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Path to JSON file |
required |
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult |
options: show_source: false members: - init - validate
ValidationResult¶
Result of a validation operation.
dppvalidator.validators.ValidationResult
dataclass
¶
Result of DPP validation following the Result pattern.
Never raises exceptions for validation failures. Instead, check
the valid property and inspect errors for details.
Attributes:
| Name | Type | Description |
|---|---|---|
valid |
bool
|
Whether the passport passed all validation layers |
errors |
list[ValidationError]
|
List of validation errors (severity="error") |
warnings |
list[ValidationError]
|
List of validation warnings (severity="warning") |
info |
list[ValidationError]
|
List of informational messages (severity="info") |
schema_version |
str
|
UNTP DPP schema version used |
validated_at |
datetime
|
Timestamp of validation |
passport |
DigitalProductPassport | None
|
Parsed DigitalProductPassport if valid, None otherwise |
parse_time_ms |
float
|
Time spent parsing input |
validation_time_ms |
float
|
Time spent on validation layers |
Source code in src/dppvalidator/validators/results.py
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 | |
all_issues
property
¶
All errors, warnings, and info messages combined.
error_count
property
¶
Total number of errors.
warning_count
property
¶
Total number of warnings.
__repr__()
¶
Return a concise representation for debugging.
Source code in src/dppvalidator/validators/results.py
merge(other)
¶
Merge another result into this one.
Source code in src/dppvalidator/validators/results.py
raise_for_errors()
¶
Raise ValidationException if there are errors.
This is an opt-in method for users who prefer exception-based flow.
to_dict()
¶
Convert to dictionary for JSON serialization.
Source code in src/dppvalidator/validators/results.py
options: show_source: false
ValidationError¶
A single validation error or warning.
dppvalidator.validators.ValidationError
dataclass
¶
Represents a single validation error with full context.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
JSON path to the error location (e.g., "$.credentialSubject.product.id") |
message |
str
|
Human-readable error description |
code |
str
|
Machine-readable error code (e.g., "SEM001") |
layer |
Literal['schema', 'model', 'semantic', 'jsonld', 'plugin', 'vocabulary']
|
Validation layer that produced this error |
severity |
Literal['error', 'warning', 'info']
|
Error severity level |
suggestion |
str | None
|
Suggested fix for the error |
docs_url |
str | None
|
Link to detailed error documentation |
did_you_mean |
tuple[str, ...]
|
Similar valid values (for typo correction) |
context |
dict[str, Any]
|
Additional context for debugging |
Source code in src/dppvalidator/validators/results.py
to_dict()
¶
Convert to dictionary for JSON serialization.
Source code in src/dppvalidator/validators/results.py
options: show_source: false
Usage Example¶
from dppvalidator.validators import ValidationEngine, ValidationResult
# Create engine with specific layers
engine = ValidationEngine(layers=["model", "semantic"])
# Validate data
result = engine.validate(
{
"id": "https://example.com/dpp/001",
"issuer": {"id": "https://example.com/issuer", "name": "Acme Corp"},
}
)
# Check result
if result.valid:
print("Passport is valid!")
else:
for error in result.errors:
print(f"[{error.code}] {error.path}: {error.message}")
# Access validation metadata
print(f"Schema version: {result.schema_version}")
print(f"Validation time: {result.validation_time_ms:.2f}ms")
Validation Layers¶
The engine supports seven validation layers:
| Layer | Description |
|---|---|
| schema | JSON Schema validation (Draft 2020-12) |
| model | Pydantic model validation |
| semantic | Business rule validation |
| jsonld | JSON-LD context expansion and validation |
| vocabulary | External vocabulary validation |
| plugin | Custom plugin validators |
| signature | Verifiable Credential signatures |
Performance Features¶
Module-Level Schema Caching¶
Schemas are cached at the module level for better performance:
from dppvalidator.schemas.loader import clear_schema_cache
# Clear cache if needed (e.g., after updating schema files)
clear_schema_cache()
Plugin Registry Singleton¶
The plugin registry uses a singleton pattern:
from dppvalidator.plugins.registry import get_default_registry, reset_default_registry
# Get the shared registry
registry = get_default_registry()
# Reset for testing
reset_default_registry()
Error Codes¶
| Code | Layer | Description |
|---|---|---|
| SCH001 | schema | Required field missing |
| SCH002 | schema | Invalid type |
| MOD001 | model | Model validation error |
| JLD001 | jsonld | Invalid context |
| SEM001 | semantic | Invalid vocabulary value |
| SEM002 | semantic | Invalid date range |
| SIG001 | crypto | Invalid signature |
Note: This table shows common examples. See Error Reference for the complete list of 70+ error codes.