def validate_request_keys(self, request_body): is_valid = True error_msg = "The following errors were found:\n" # Get keys required/supported required_keys = self._validation_ruleset.get_keys_of_required_inputs() if 'language' in request_body: language = request_body['language'] rule = self._validation_ruleset.get_validation_rule_for_key('language') validation_result = validate('language', language, rule) if validation_result.is_valid: required_keys += self._validation_ruleset.get_keys_for_language(request_body['language']) else: is_valid = False error_msg += f'Unsupported language "{language}" provided\n' supported_keys = required_keys + self._validation_ruleset.get_keys_of_optional_inputs() # Find any discrepancies missing_keys = get_set_difference(required_keys, request_body) unsupported_keys = get_set_difference(request_body, supported_keys) # Group the results together for key in missing_keys: is_valid = False error_msg += f'Missing required input "{key}"\n' for key in unsupported_keys: is_valid = False error_msg += f'Unsupported input "{key}" provided\n' result = ValidationResult( is_valid, "All input keys provided are valid\n" if is_valid else error_msg, None ) return result
def test_get_set_difference_finds_simple_missing_key(): # Arrange lst = ['a', 'b', 'c'] dictionary = {'a': 1, 'c': 3} # Act result = get_set_difference(lst, dictionary) # Assert assert result == ['b']
def test_get_set_difference_finds_simple_missing_item(): # Arrange dictionary = {'a': 1, 'b': 2, 'c': 3} lst = ['a', 'c'] # Act result = get_set_difference(dictionary, lst) # Assert assert result == ['b']
def test_get_set_difference_returns_empty_array_when_keys_subset_of_items(): # Arrange dictionary = {'a': 1, 'c': 3} lst = ['a', 'b', 'c'] # Act result = get_set_difference(dictionary, lst) # Assert assert result == []
def test_get_set_difference_returns_empty_array_when_items_subset_of_keys(): # Arrange lst = ['a', 'c'] dictionary = {'a': 1, 'b': 2, 'c': 3} # Act result = get_set_difference(lst, dictionary) # Assert assert result == []