def check_lambda(contract, data): ''' The lambda fails when it raises an Exception or if return value is False. Note that it does NOT fail if the return value is None. Examples of valid input: check_lambda({"lambda": lambda data: re.match("^[0-9]+$") is not None}, "123") check_lambda({"lambda": lambda data: data in ["aa", "bb"]}, "aa") Examples of invalid input (raises an Exception): check_lambda({"lambda": lambda data: re.match("^[0-9]+$") is not None}, "123a") check_lambda({"lambda": lambda data: data in ["aa", "bb"]}, "cc") ''' try: ret = contract['lambda'](data) except: ret = False if ret is False: raise CheckException( key="invalid-check-lambda", context={ "contract":contract, "data":data})
def check_switch_contract(contract, data): ''' Sepcify depending on a dict key value on a contract for the value of a dict key. The switch-key must be a string value. Examples of valid input: check_switch_contract( { "switch-key": "mode", "contract-key": "mode-data", "contracts": { "str-mode": [ {'check': 'isinstance', 'type': str} ], "int-mode": [ {'check': 'isinstance', 'type': int} ] } }, {"mode": "str", "mode-data": "foo"}) Examples of invalid input (raises an Exception): check_switch_contract( { "switch-key": "mode", "contract-key": "mode-data", "contracts": { "str-mode": [ {'check': 'isinstance', 'type': str} ], "int-mode": [ {'check': 'isinstance', 'type': int} ] } }, {"mode": "str", "mode-data": 452453}) ''' switch_key = contract['switch-key'] contract_key = contract['contract-key'] key_val = data[switch_key] contracts = contract['contracts'] if key_val not in contracts: raise CheckException( key="unknown-key-contract", context={ "contract":contract, "data":data}) check_list(contracts[key_val], data[contract_key])
def check_item(contract, data): try: { "isinstance": check_isinstance, "iterate-list": check_iterate_list, "length": check_length, "lambda": check_lambda, "index-check-list": check_index_check_list, "dict-keys-exist": check_dict_keys_exist, "dict-keys-exact": check_dict_keys_exact, "switch-contract-by-dict-key": check_switch_contract }[contract['check']](contract, data) except CheckException as e: raise e except Exception as e: raise CheckException(key="invalid-check", context=contract)
def check_length(contract, data): ''' Examples of valid input: check_length({"range": [2,4]}, "123") check_length({"range": [3,5]}, [1,2,3]) Examples of invalid input (raises an Exception): check_length({"range": [2,4]}, "12345") check_length({"range": [3,5]}, [1,2]) ''' l = len(data) if l > contract['range'][1] or l < contract['range'][0]: raise CheckException( key="invalid-data-length", context={ "contract":contract, "data":data, "data-length": l})
def check_isinstance(contract, data): ''' Examples of valid input: check_isinstance({"type": str}, "my-string") check_isinstance({"type": int}, 78) check_isinstance({"type": Foo}, Foo()) Examples of invalid input (raises an Exception): check_isinstance({"type": str}, 78) check_isinstance({"type": int}, "my-string") check_isinstance({"type": Foo}, Bar()) ''' if not isinstance(data, contract['type']): raise CheckException( key="invalid-check", context={ "contract":contract, "data":data, "checked-type":contract['type'], "data-type": type(data)})
def check_dict_keys_exact(contract, data): ''' The list of keys must exist in the dictionary, and there are none other. Examples of valid input: check_dict_keys_exist( {"keys": ["key1", "key2"]}, {"key1": None, "key2": "foo"}) Examples of invalid input (raises an Exception): check_dict_keys_exist( {"keys": ["key1", "key2"]}, {"key1": None, "key2": "foo", "key3": ["bar"]}) ''' if set(contract['keys']) != set(data.keys()): raise CheckException( key="dict-keys-not-exact", context={ "contract":contract, "data":data})
def check_dict_keys_exist(contract, data): ''' The list of keys must exist in the dictionary. Examples of valid input: check_dict_keys_exist( {"keys": ["key1", "key3"]}, {"key1": None, "key2": "foo", "key3": ["bar"]}) Examples of invalid input (raises an Exception): check_dict_keys_exist( {"keys": ["key1", "key4"]}, {"key1": None, "key2": "foo", "key3": ["bar"]}) ''' for key in contract['keys']: if key not in data: raise CheckException( key="dict-keys-not-found", context={ "contract":contract, "data":data})
def execute(data, config, name, ae): if not dni_constraint(data['request'].get(name, '')): raise CheckException( key='invalid-dni', context=data['request'].get(name, '')) return PipeReturnvalue.CONTINUE