def _load(self, data, partial=None): if not isinstance(data, dict): return UnmarshalResult({}, {"_schema": "Invalid data type: %s" % data}) data = dict(data) data_type = data.get(self.type_field) if not data_type: return UnmarshalResult( {}, {self.type_field: ["Missing data for required field."]}) try: schema = self._get_schema(data_type) except TypeError: # data_type could be unhashable return UnmarshalResult( {}, {self.type_field: ["Invalid value: %s" % data_type]}) if not schema: return UnmarshalResult( {}, {self.type_field: ["Unsupported value: %s" % data_type]}) return schema.load(data, many=False, partial=partial)
def _load(self, data, partial=None): if not isinstance(data, dict): return UnmarshalResult({}, {'_schema': 'Invalid data type: %s' % data}) data = dict(data) data_type = data.get(self.type_field) if self.type_field in data and self.type_field_remove: data.pop(self.type_field) if not data_type: return UnmarshalResult({}, { self.type_field: ['Missing data for required field.'] }) try: type_schema = self.type_schemas.get(data_type) except TypeError: # data_type could be unhashable return UnmarshalResult({}, { self.type_field: ['Invalid value: %s' % data_type] }) if not type_schema: return UnmarshalResult({}, { self.type_field: ['Unsupported value: %s' % data_type], }) schema = (type_schema if isinstance(type_schema, Schema) else type_schema()) return schema.load(data, many=False, partial=partial)
def load(self, data, many=None, partial=None): if not isinstance(data, dict): return UnmarshalResult({}, {'_schema': 'Invalid data type: %s' % data}) schema = common.resolve_schema_instance(self.value_type) res = [] for key, value in data.items(): payload = value.copy() payload[self.key_name] = key result = schema.load(payload) res.append(result.data) return UnmarshalResult(res, [])
def load(self, value): # don't support many data = {self.key: value} r, errors = self.schema.load(data) return UnmarshalResult( data=r.get(self.key) or self.missing, errors=errors.get(self.key) or errors, )
def _load(self, data, partial=None): data_type = data.get(self.type_field) if not data_type: return UnmarshalResult({}, { self.type_field: [u'Missing data for required field.'] }) type_schema = self.type_schemas.get(data_type) if not type_schema: return UnmarshalResult({}, { self.type_field: ['Unsupported value: %s' % data_type], }) schema = (type_schema if isinstance(type_schema, Schema) else type_schema()) return schema.load(data, many=False, partial=partial)
def load(self, data, session=None, instance=None, *args, **kwargs): # Can't use post_load because we can't override marshmallow_sqlalchemy's post_load hook try: objs = super(ExecutionElementBaseSchema, self).load(data, session=session, instance=instance, *args, **kwargs) except InvalidExecutionElement as e: objs = UnmarshalResult({}, e.errors) if not objs.errors: errors = {} all_objs = objs.data if not isinstance(objs.data, list): all_objs = [all_objs] for obj in (obj for obj in all_objs if isinstance(obj, ExecutionElement)): try: obj.validate() except InvalidExecutionElement as e: errors[e.name] = e.errors if errors: objs.errors.update(errors) return objs
def load(self, data, many=None, partial=None, skip_validation=False): # prepare record error message instance (we wan't this instance to be the same as in result = super().load(data, many=many, partial=partial, skip_validation=skip_validation) if not result.errors: ret = self.validate_dynamic_fields(result.data, data, skip_validation=skip_validation) else: ret = result.data return UnmarshalResult(ret, result.errors)
async def async_load(self, data, many=None, partial=None): """the async_load function DO NOT handler the original processors function like `post_load` or `pre_load`. but it uses the original validators. """ result, errors = await self._async_do_load(data, many, partial=partial, postprocess=True) return UnmarshalResult(data=result, errors=errors)
def load(self, data, many=None, partial=None, skip_validation=False): if skip_validation: result, errors = self._do_load_no_validate(data, many, partial=partial, postprocess=True) else: result, errors = self._do_load(data, many, partial=partial, postprocess=True) return UnmarshalResult(data=result, errors=errors)
def load(self, data, *args, **kwargs): permissions_data = data.pop('permissions', []) errors = {} result = super(MicroserviceSchema, self).load(data, *args, **kwargs) errors.update(result.errors) self.validate_permissions(permissions_data, errors) if errors: return UnmarshalResult(data=None, errors=errors) result.data['permissions'] = permissions_data return result
def load(self, data, many=None, partial=None): r = super().load(data, many=many, partial=partial) if r.errors: if self.many if many is None else bool(many): # xxx: data = [d.get("v") or self.default for d in r.data] errors = {k: v["v"] for k, v in r.errors.items()} else: data = data["v"] errors = r.errors["v"] return UnmarshalResult(data=data, errors=errors) return r
def test_raises_validation_error(http_req, decoder, mocker): """Extractor.extract should raise ValidationError if validation of decoded data failed.""" read_data = mocker.Mock() schema = mocker.Mock() schema.load.return_value = UnmarshalResult({}, [{'test': 'error_msg'}]) ext = Extractor(schema=schema, decoder=decoder, required=True, read_data=read_data) with pytest.raises(ValidationError) as exc_info: ext.extract(http_req) assert exc_info.value.errors == [{'test': 'error_msg'}]
def load(self, data, many=None, partial=None): many = self.many if many is None else bool(many) if partial is None: partial = self.partial if not many: return self._load(data, partial=partial) result_data = [] result_errors = {} for idx, item in enumerate(data): result = self._load(item, partial=partial) result_data.append(result.data) if result.errors: result_errors[idx] = result.errors return UnmarshalResult(result_data, result_errors)