"""Test your protocol for issues before you continueto work with it or you publish it."""importjsonimportjsonschemaimportosimportwarningsfromtypingimportDict,List,Tuple
[docs]defvalidate(protocol=None,verbose=False):""" Test protocol :param protocol: Protocol code to test :param type: dict or str :param verbose: Print errors :param type: bool :return: True if tests are passed with an empty list, otherwise False with a list of errors :rtype: bool, list """file_path=os.path.join(os.path.dirname(__file__),'schema.json')try:withopen(file_path,'r',encoding='utf-8')asfp:schema=json.load(fp)exceptjson.JSONDecodeError:warnings.warn('Error: Invalid JSON (%s)'%file_path)schema={}passvalidator=SchemaValidator(schema)is_valid,errors=validator.validate_with_all_errors(protocol)ifnotis_validandverbose:forerrorinerrors:print(error)returnis_valid,errors
[docs]defvalidate_with_all_errors(self,data:Dict)->Tuple[bool,List[str]]:""" Validates data and returns all validation errors. Args: data: The data to validate against the schema Returns: Tuple of (is_valid, list_of_error_messages) """errors=[]forerrorinself.validator.iter_errors(data):# Format error pathpath=' -> '.join(str(p)forpinerror.path)iferror.pathelse'root'# Create detailed error messageerror_msg=f"Path '{path}': {error.message}"errors.append(error_msg)returnlen(errors)==0,errors