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.

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 = validation_report = validate_metadata_as_dict(rocrate_metadata, settings=settings)

    # process the validation result as needed
    ...