def validate(self, current_data: Any) -> Report: report = Report(soft=True) for exp_data in self.expected_data: checker = Validator(expected_data=exp_data, report=report) checker.validate(current_data) if report.has_errors(): message = format_error_message(self, current_data) report.errors = ["Not valid data: %s" % message] return report
def validate(self, data: Any) -> Any: log.debug( "Checker settings: ignore_extra_keys=%s, soft=%s" % (self.ignore_extra_keys, self.soft) ) report = Report(self.soft) checker = Validator( expected_data=self.expected_data, report=report, ignore_extra_keys=self.ignore_extra_keys, ) checker.validate(data) if report.has_errors(): raise CheckerError(report) return data
def validate(self, current_data: Any) -> Report: expected = list( filtered_by_type(self.expected_data, type(current_data))) if not expected and self.expected_data: report = Report(soft=True) message = format_error_message(self, current_data) report.add("Not valid data: %s" % message) return report results = {} for exp_data in expected: report = Report(soft=True) checker = Validator(expected_data=exp_data, report=report) checker.validate(current_data) if not report.has_errors(): return report results[len(report)] = report min_error = min(list(results.keys())) return results[min_error]
def test_report_has_errors(): r = Report(soft=True) r.add("some error message") assert r.has_errors()
def test_report_without_errors(): r = Report(soft=True) assert not r.has_errors()
def validate(self, current_data: Any) -> Report: """ Examples: >>> from json_checker.core.reports import Report # make simple expected schema and data for that >>> EXPECTED_SCHEMA = {"id": int, "name": str, "items": [int]} >>> right_data = {"id": 1, "name": "#1", "items": [1, 2, 3]} >>> broken_data = {"id": "212", "name": "#1", "items": [1, '2', '3']} # Soft validation: >>> soft_checker = DictChecker( >>> EXPECTED_SCHEMA, report=Report(soft=True) >>> ) >>> soft_checker.validate(right_data) # Report object without errors >>> soft_checker.validate(broken_data) # Report object with 3 errors # Hard validation: >>> hard_checker = DictChecker( >>> EXPECTED_SCHEMA, report=Report(soft=False) >>> ) >>> hard_checker.validate(right_data) # Report object without errors # with first error, not valid `id` >>> hard_checker.validate(broken_data) # raise DictCheckerError Must be used into `json_checker` only :param dict | OrderedDict current_data: :return: Report """ if current_data == self.expected_data: return self.report if not isinstance(current_data, dict): message = format_error_message(dict, current_data) return self.add_or_raise(message) validated_keys = [] current_keys = list(current_data.keys()) for ex_key, value in filtered_items(self.expected_data, current_keys): if ex_key not in current_keys: message = "Missing keys in current response: %s" % ex_key self.report.add_or_raise(message, MissKeyCheckerError) continue report = Report(soft=True) checker = Validator( expected_data=value, report=report, ignore_extra_keys=self.ignore_extra_keys, ) checker.validate(current_data[ex_key]) validated_keys.append(ex_key) if report.has_errors(): self.add_or_raise('From key="%s": \n\t%s' % (ex_key, report)) if not self.ignore_extra_keys: miss_expected_keys = list(set(current_keys) - set(validated_keys)) if miss_expected_keys: message = "Missing keys in expected schema: " "%s" % ", ".join( miss_expected_keys) self.report.add_or_raise(message, MissKeyCheckerError) return self.report
def validate(self, current_data: Iterable) -> Report: """ Examples: >>> from json_checker.core.reports import Report # make different reports for examples >>> soft_report = Report(soft=True) >>> hard_report = Report(soft=False) # One to one with all current items >>> soft_checker = ListChecker([int], report=soft_report) >>> soft_checker.validate([1, 2, 3]) # Report object without errors >>> soft_checker.validate([1, '2', '3']) # Report object with 2 errors # One to one with all current items >>> hard_checker = ListChecker([int], report=hard_report) >>> hard_checker.validate([1, 2, 3]) # Report object without errors # with first error >>> hard_checker.validate([1, '2', '3']) # raise TypeCheckerError # One to one with all current items by position >>> hard_checker = ListChecker([1, 2, 3], report=hard_report) >>> hard_checker.validate([1, 2, 3]) # Report object without errors # One to one with all current items by position >>> hard_checker = ListChecker([int, int, str], report=hard_report) >>> hard_checker.validate([1, 2, '3']) # Report object without errors Must be used into `json_checker` only :param list | tuple | set | frozenset current_data: :return: Report """ if self.expected_data == current_data: return self.report if ( # expected [int], current 123 (not isinstance(current_data, (list, tuple, set, frozenset))) or # expected [int], current [] (not current_data and self.expected_data) or # expected [], current [1, 2, 3] (not self.expected_data and current_data) or # expected [int, str], current [1] (1 > len(self.expected_data) > 1)): error = format_error_message(self.expected_data, current_data) return self.add_or_raise(error) if len(self.expected_data) == len(current_data): for exp, cur in list(zip(self.expected_data, current_data)): soft_report = Report(soft=True) checker = Validator(expected_data=exp, report=soft_report) checker.validate(cur) if soft_report.has_errors(): self.add_or_raise(str(soft_report)) return self.report expected = self.expected_data[0] for data in current_data: soft_report = Report(soft=True) checker = Validator(expected_data=expected, report=soft_report) checker.validate(data) if soft_report.has_errors(): self.add_or_raise(str(soft_report)) return self.report