def __call__(self, msg): __tracebackhide__ = True try: return self.validator(dict(msg)) except MultipleInvalid as error: if self.allow_extra: def split(acc, error): extra_errors, not_extra_errors = acc if is_extra_key_error(error): extra_errors.append(error) else: not_extra_errors.append(error) return (extra_errors, not_extra_errors) extra_errors, not_extra_errors = reduce( split, error.errors, ([], [])) list(map(log_unexpected, extra_errors)) if not_extra_errors: # Re-raise the rest raise ValidationError(MultipleInvalid(not_extra_errors)) else: raise ValidationError(error) except Invalid as error: if self.allow_extra: if is_extra_key_error(error): log_unexpected(error) else: raise ValidationError(error) else: raise ValidationError(error)
def _ensure_dict_and_validate(msg): try: return validator(dict(msg)) except MultipleInvalid as error: def split(acc, error): extra_errors, not_extra_errors = acc if is_extra_key_error(error): extra_errors.append(error) else: not_extra_errors.append(error) return (extra_errors, not_extra_errors) extra_errors, not_extra_errors = reduce(split, error.errors, ([], [])) # Apply invalid_into_unexpected to each error for extra keys list(map(invalid_into_unexpected, extra_errors)) if not_extra_errors: # Re-raise the rest raise ValidationError(MultipleInvalid(not_extra_errors)) except Invalid as error: if warn_on_extra: if is_extra_key_error(error): invalid_into_unexpected(error) else: raise ValidationError(error) else: raise ValidationError(error)
def __inner(input): if not isinstance(input, (tuple, list)): raise Invalid("expected sequence") errs = MultipleInvalid() result = [] for ndx, elem in enumerate(input): try: result.append(validator(elem)) except Invalid as exc: exc.path.append(ndx) errs.add(exc) if errs.errors: raise errs if isinstance(input, tuple): result = tuple(result) return result
def __call__(self, v): if not isinstance(v, (list, tuple)): raise Invalid(self.msg or 'Value {} is not sequence!'.format(v)) if len(v) != len(self._schemas): raise Invalid(self.msg or 'List lengths differ, value:{} != target:{}'.format(len(v), len(self._schemas))) consumed = set() missing = [] for index, value in enumerate(v): found = False for i, s in enumerate(self._schemas): if i in consumed: continue try: s(value) except Invalid: pass else: found = True consumed.add(i) break if not found: missing.append((index, value)) if len(missing) == 1: el = missing[0] raise Invalid(self.msg or 'Element #{} ({}) is not valid against any validator'.format(el[0], el[1])) elif missing: raise MultipleInvalid([Invalid(self.msg or 'Element #{} ({}) is not valid against any validator'.format( el[0], el[1])) for el in missing]) return v
def test__validate_owncacertdata_exception(mock_Schema): mocked_schema = mock.MagicMock() mocked_schema.side_effect = MultipleInvalid("x") mock_Schema.return_value = mocked_schema with pytest.raises(OnwCAInvalidDataStructure) as err: _validate_owncacertdata({ "cert": "cert", "cert_bytes": "cert_bytes", "key": "key", "key_bytes": "key_bytes", "public_key": "public_key", "public_key_bytes": "public_key_bytes", }) assert "OnwcaKeyData: " in err.value
def __inner(dct): if not isinstance(dct, dict): raise DictInvalid("expected dict") errs = MultipleInvalid() result = {} for k, v in dct.items(): key, key_err = __schema_validate(key_validator, k) val, val_err = __schema_validate(value_validator, v) if not (key_err or val_err): result[key_validator(key)] = value_validator(val) else: if key_err: # wrap in KeyInvalid to aid distinction # between key- and value errors err = __wrap_err(KeyInvalid, key_err) errs.add(err) if val_err: val_err.path.append(k) errs.add(val_err) if errs.errors: raise errs return result