Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def compare_dict(self, data, spec, path):
if fold_type(data) is not dict:
return self.result.fail(
f"{self.interaction_name} element is not an object (is {nice_type(data)})", path
)
for key in spec:
if key not in data:
return self.result.fail(f"{self.interaction_name} element {key!r} is missing", path)
p = path + [key]
if not self.compare(data[key], spec[key], p):
return self.result.fail(
f"{self.interaction_name} element {key} ({nice_type(data[key])}) "
f"does not match spec ({nice_type(spec[key])})",
path,
)
return True
def compare_list(self, data, path, spec):
if fold_type(data) is not list:
return self.result.fail(
f"{self.interaction_name} element is not an array (is {nice_type(data)})", path
)
if len(data) != len(spec):
return self.result.fail(
f"{self.interaction_name} array is incorrect length (is {len(data)} elements)", path
)
for i, (data_elem, spec_elem) in enumerate(zip(data, spec)):
p = path + [i]
if not self.compare(data_elem, spec_elem, p):
return self.result.fail(
f"{self.interaction_name} element {i} ({nice_type(data_elem)}) "
f"does not match spec ({nice_type(spec_elem)})",
path,
)
return True
def compare_list(self, data, path, spec):
if fold_type(data) is not list:
return self.result.fail(
f"{self.interaction_name} element is not an array (is {nice_type(data)})", path
)
if len(data) != len(spec):
return self.result.fail(
f"{self.interaction_name} array is incorrect length (is {len(data)} elements)", path
)
for i, (data_elem, spec_elem) in enumerate(zip(data, spec)):
p = path + [i]
if not self.compare(data_elem, spec_elem, p):
return self.result.fail(
f"{self.interaction_name} element {i} ({nice_type(data_elem)}) "
f"does not match spec ({nice_type(spec_elem)})",
path,
)
return True
def apply_rules_dict(self, data, spec, path):
log.debug(f"apply_rules_dict data={data!r} spec={spec!r} path={format_path(path)}")
if fold_type(data) is not dict:
return self.result.fail(
f"{self.interaction_name} element is not an object (is {nice_type(data)})", path
)
for k in spec:
p = path + [k]
if k not in data:
# we always flag a failure if a given key is not in the response
return self.result.fail(f"{self.interaction_name} element {k!r} is missing", path)
if not self.apply_rules(data[k], spec[k], p):
return False
return True