77import logging
88import os
99import shutil
10+ import json
1011from typing import Optional
1112
1213from rocrate_validator import services
2223 find_validation_object_on_minio
2324)
2425from app .utils .webhook_utils import send_webhook_notification
25- from app .utils .file_utils import build_metadata_only_rocrate
2626
2727logger = logging .getLogger (__name__ )
2828
@@ -98,32 +98,27 @@ def process_validation_task_by_id(
9898
9999@celery .task
100100def 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.
105105
106106 :param crate_json: A string containing the RO-Crate JSON metadata to validate.
107107 :param profile_name: The name of the validation profile to use. Defaults to None.
108108 :param webhook_url: The webhook URL to send notifications to. Defaults to None.
109+ :param profiles_path: The path to the profiles definition directory. Defaults to None.
109110 :raises Exception: If an error occurs during the validation process.
110111
111112 :todo: Replace the Crate ID with a more comprehensive system, and replace profile name with URI.
112113 """
113114
114- skip_checks_list = ['ro-crate-1.1_12.1' ]
115- file_path = None
116-
117115 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 } " )
116+ logging .info ("Processing validation task for provided metadata string" )
122117
123118 # Perform validation:
124- validation_result = perform_ro_crate_validation ( file_path ,
119+ validation_result = perform_metadata_validation ( crate_json ,
125120 profile_name ,
126- skip_checks_list
121+ profiles_path
127122 )
128123
129124 if isinstance (validation_result , str ):
@@ -132,9 +127,9 @@ def process_validation_task_by_metadata(
132127 raise Exception (f"Validation failed: { validation_result } " )
133128
134129 if not validation_result .has_issues ():
135- logging .info (f "RO Crate { file_path } is valid." )
130+ logging .info ("RO Crate metadata is valid." )
136131 else :
137- logging .info (f "RO Crate { file_path } is invalid." )
132+ logging .info ("RO Crate metadata is invalid." )
138133
139134 if webhook_url :
140135 send_webhook_notification (webhook_url , validation_result .to_json ())
@@ -148,10 +143,6 @@ def process_validation_task_by_metadata(
148143 send_webhook_notification (webhook_url , error_data )
149144
150145 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-
155146 if isinstance (validation_result , str ):
156147 return validation_result
157148 else :
@@ -196,6 +187,39 @@ def perform_ro_crate_validation(
196187 return str (e )
197188
198189
190+ def perform_metadata_validation (
191+ crate_json : str , profile_name : str | None , skip_checks_list : Optional [list ] = None , profiles_path : Optional [str ] = None
192+ ) -> ValidationResult | str :
193+ """
194+ Validates only RO-Crate metadata provided as a json string.
195+
196+ :param crate_json: The JSON string containing the metadata
197+ :param profile_name: The name of the validation profile to use. Defaults to None. If None, the CRS4 validator will
198+ attempt to determine the profile.
199+ :param profiles_path: The path to the profiles definition directory
200+ :param skip_checks_list: A list of checks to skip, if needed
201+ :return: The validation result.
202+ :raises Exception: If an error occurs during the validation process.
203+ """
204+
205+ try :
206+ logging .info (f"Validating ro-crate metadata with profile { profile_name } " )
207+
208+ settings = services .ValidationSettings (
209+ ** ({"metadata_only" : True }),
210+ ** ({"metadata_dict" : json .loads (crate_json )}),
211+ ** ({"profile_identifier" : profile_name } if profile_name else {}),
212+ ** ({"skip_checks" : skip_checks_list } if skip_checks_list else {}),
213+ ** ({"profiles_path" : profiles_path } if profiles_path else {})
214+ )
215+
216+ return services .validate (settings )
217+
218+ except Exception as e :
219+ logging .error (f"Unexpected error during validation: { e } " )
220+ return str (e )
221+
222+
199223def check_ro_crate_exists (
200224 minio_client : object ,
201225 bucket_name : str ,
0 commit comments