Programmatic Validation
You can also integrate the package programmatically in your Python code.
Here’s an example:
# Import the `services` and `models` module from the rocrate_validator package
from rocrate_validator import services, models
# Create an instance of `ValidationSettings` class to configure the validation
settings = services.ValidationSettings(
# Set the path to the RO-Crate root directory
rocrate_uri='/path/to/ro-crate',
# Set the identifier of the RO-Crate profile to use for validation.
# If not set, the system will attempt to automatically determine the appropriate validation profile.
profile_identifier='ro-crate-1.1',
# Set the requirement level for the validation
requirement_severity=models.Severity.REQUIRED,
)
# Call the validation service with the settings
result = services.validate(settings)
# Check if the validation was successful
if not result.has_issues():
print("RO-Crate is valid!")
else:
print("RO-Crate is invalid!")
# Explore the issues
for issue in result.get_issues():
# Every issue object has a reference to the check that failed, the severity of the issue, and a message describing the issue.
print(f"Detected issue of severity {issue.severity.name} with check \"{issue.check.identifier}\": {issue.message}")
The following is a possible output:
RO-Crate is invalid!
Detected issue of severity REQUIRED with check "ro-crate-1.1:root_entity_exists: The RO-Crate must contain a root entity.
See also
To resolve resources from a local cache or run validation without network
access (the offline / no_cache settings of ValidationSettings),
see Offline Mode and HTTP Caching.
Metadata-only Validation
In addition to full validation, which checks both metadata and data files, the library also supports metadata-only validation. This is useful when you want to ensure that the metadata conforms to the expected schema without checking the actual data files.
To perform metadata-only validation, you can use the validate_metadata_as_dict from the rocrate_validator.services module. This function takes a dictionary representing the metadata and validates it against a given validation profile.
import json
from rocrate_validator.services import validate_metadata_as_dict
settings = {
"profile_identifier": "workflow-ro-crate-1.0"
}
with open('tests/data/crates/invalid/0_main_workflow/main_workflow_bad_type/ro-crate-metadata.json', 'r') as f:
# load the metadata from the JSON file
rocrate_metadata = json.load(f)
# validate the metadata dictionary
result = validate_metadata_as_dict(rocrate_metadata, settings=settings)
# process the validation result as needed
...
Formatting Validation Results
Validation results can be rendered using different output formatters provided by
the library. Two formatter types are available: text and JSON.
Both rely on the rich Python library and integrate with the
rocrate_validator.utils.io_helpers.output.console.Console class, which extends
rich.console.Console to support custom formatter registration.
To format results, create a Console instance, register one formatter,
and then print any validation output object (e.g., the full report or the
aggregated statistics).
TextOutputFormatter
TextOutputFormatter renders validation reports as human-readable, styled text.
It is typically used for console output, report generation, or writing results
to a file.
from rocrate_validator.utils.io_helpers.output.console import Console
from rocrate_validator.utils.io_helpers.output.text import TextOutputFormatter
console = Console()
console.register_formatter(TextOutputFormatter())
# Print the main validation result
console.print(result)
# Print aggregated statistics (violations by severity, executed checks, etc.)
console.print(result.statistics)
# Write the output to a file
with open("validation_report.txt", "w") as f:
file_console = Console(file=f)
file_console.register_formatter(TextOutputFormatter())
file_console.print(result.statistics)
file_console.print(result)
JSONOutputFormatter
JSONOutputFormatter produces JSON-structured output, suitable for logging,
programmatic processing, or integration with external tools.
from rocrate_validator.utils.io_helpers.output.console import Console
from rocrate_validator.utils.io_helpers.output.json import JSONOutputFormatter
console = Console()
console.register_formatter(JSONOutputFormatter())
# Print the main validation result as JSON
console.print(result)
# Print the aggregated statistics
console.print(result.statistics)
# Write the output to a file
with open("validation_report.json", "w") as f:
file_console = Console(file=f)
file_console.register_formatter(JSONOutputFormatter())
file_console.print(result)