Skip to content

Commit 0f95c42

Browse files
committed
direct metadata validation from json
1 parent fe43ffd commit 0f95c42

1 file changed

Lines changed: 40 additions & 17 deletions

File tree

app/tasks/validation_tasks.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
import os
99
import shutil
10+
import json
1011
from typing import Optional
1112

1213
from rocrate_validator import services
@@ -22,7 +23,6 @@
2223
find_validation_object_on_minio
2324
)
2425
from app.utils.webhook_utils import send_webhook_notification
25-
from app.utils.file_utils import build_metadata_only_rocrate
2626

2727
logger = logging.getLogger(__name__)
2828

@@ -98,7 +98,7 @@ def process_validation_task_by_id(
9898

9999
@celery.task
100100
def process_validation_task_by_metadata(
101-
crate_json: str, profile_name: str | None, webhook_url: str | None
101+
crate_json: str, profile_name: str | None, webhook_url: str | None, profiles_path: Optional[str] = None
102102
) -> ValidationResult | str:
103103
"""
104104
Background task to process the RO-Crate validation for a given json metadata string.
@@ -111,19 +111,13 @@ def process_validation_task_by_metadata(
111111
:todo: Replace the Crate ID with a more comprehensive system, and replace profile name with URI.
112112
"""
113113

114-
skip_checks_list = ['ro-crate-1.1_12.1']
115-
file_path = None
116-
117114
try:
118-
# Fetch the RO-Crate from MinIO using the provided ID:
119-
file_path = build_metadata_only_rocrate(crate_json)
120-
121-
logging.info(f"Processing validation task for {file_path}")
115+
logging.info(f"Processing validation task for provided metadata string")
122116

123117
# Perform validation:
124-
validation_result = perform_ro_crate_validation(file_path,
118+
validation_result = perform_metadata_validation(crate_json,
125119
profile_name,
126-
skip_checks_list
120+
profiles_path
127121
)
128122

129123
if isinstance(validation_result, str):
@@ -132,9 +126,9 @@ def process_validation_task_by_metadata(
132126
raise Exception(f"Validation failed: {validation_result}")
133127

134128
if not validation_result.has_issues():
135-
logging.info(f"RO Crate {file_path} is valid.")
129+
logging.info("RO Crate metadata is valid.")
136130
else:
137-
logging.info(f"RO Crate {file_path} is invalid.")
131+
logging.info("RO Crate metadata is invalid.")
138132

139133
if webhook_url:
140134
send_webhook_notification(webhook_url, validation_result.to_json())
@@ -148,10 +142,6 @@ def process_validation_task_by_metadata(
148142
send_webhook_notification(webhook_url, error_data)
149143

150144
finally:
151-
# Clean up the temporary file if it was created:
152-
if file_path and os.path.exists(file_path):
153-
shutil.rmtree(file_path)
154-
155145
if isinstance(validation_result, str):
156146
return validation_result
157147
else:
@@ -196,6 +186,39 @@ def perform_ro_crate_validation(
196186
return str(e)
197187

198188

189+
def perform_metadata_validation(
190+
crate_json: str, profile_name: str | None, skip_checks_list: Optional[list] = None, profiles_path: Optional[str] = None
191+
) -> ValidationResult | str:
192+
"""
193+
Validates only RO-Crate metadata provided as a json string.
194+
195+
:param crate_json: The JSON string containing the metadata
196+
:param profile_name: The name of the validation profile to use. Defaults to None. If None, the CRS4 validator will
197+
attempt to determine the profile.
198+
:param profiles_path: The path to the profiles definition directory
199+
:param skip_checks_list: A list of checks to skip, if needed
200+
:return: The validation result.
201+
:raises Exception: If an error occurs during the validation process.
202+
"""
203+
204+
try:
205+
logging.info(f"Validating ro-crate metadata with profile {profile_name}")
206+
207+
settings = services.ValidationSettings(
208+
**({"metadata_only": True}),
209+
**({"metadata_dict": json.loads(crate_json)}),
210+
**({"profile_identifier": profile_name} if profile_name else {}),
211+
**({"skip_checks": skip_checks_list} if skip_checks_list else {}),
212+
**({"profiles_path": profiles_path} if profiles_path else {})
213+
)
214+
215+
return services.validate(settings)
216+
217+
except Exception as e:
218+
logging.error(f"Unexpected error during validation: {e}")
219+
return str(e)
220+
221+
199222
def check_ro_crate_exists(
200223
minio_client: object,
201224
bucket_name: str,

0 commit comments

Comments
 (0)