Python marshmallow.EXCLUDE Examples
The following are 13
code examples of marshmallow.EXCLUDE().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
marshmallow
, or try the search function
.
Example #1
Source File: base.py From aries-cloudagent-python with Apache License 2.0 | 6 votes |
def deserialize(cls, obj): """ Convert from JSON representation to a model instance. Args: obj: The dict to load into a model instance Returns: A model instance for this data """ schema = cls._get_schema_class()(unknown=EXCLUDE) try: return schema.loads(obj) if isinstance(obj, str) else schema.load(obj) except ValidationError as e: LOGGER.exception(f"{cls.__name__} message validation error:") raise BaseModelError(f"{cls.__name__} schema validation failed") from e
Example #2
Source File: base.py From aries-cloudagent-python with Apache License 2.0 | 6 votes |
def serialize(self, as_string=False) -> dict: """ Create a JSON-compatible dict representation of the model instance. Args: as_string: Return a string of JSON instead of a dict Returns: A dict representation of this model, or a JSON string if as_string is True """ schema = self.Schema(unknown=EXCLUDE) try: return schema.dumps(self) if as_string else schema.dump(self) except ValidationError as e: LOGGER.exception(f"{self.__class__.__name__} message serialization error:") raise BaseModelError( f"{self.__class__.__name__} schema validation failed" ) from e
Example #3
Source File: test_marshmallow.py From umongo with MIT License | 6 votes |
def test_unknown_fields(self): class ExcludeBaseSchema(ma.Schema): class Meta: unknown = ma.EXCLUDE @self.instance.register class ExcludeUser(self.User): MA_BASE_SCHEMA_CLS = ExcludeBaseSchema user_ma_schema_cls = self.User.schema.as_marshmallow_schema() assert issubclass(user_ma_schema_cls, ma.Schema) exclude_user_ma_schema_cls = ExcludeUser.schema.as_marshmallow_schema() assert issubclass(exclude_user_ma_schema_cls, ExcludeBaseSchema) data = {'name': 'John', 'dummy': 'dummy'} excl_data = {'name': 'John'} # By default, marshmallow schemas raise on unknown fields with pytest.raises(ma.ValidationError) as excinfo: user_ma_schema_cls().load(data) assert excinfo.value.messages == {'dummy': ['Unknown field.']} # With custom schema, exclude unknown fields assert exclude_user_ma_schema_cls().load(data) == excl_data
Example #4
Source File: test_api.py From MegaQC with GNU General Public License v3.0 | 5 votes |
def test_post_resource(endpoint, admin_token, session, client, app): """ POST /resources. """ resource = resource_from_endpoint(app, endpoint) # Construct an instance of the model model = resource.data_layer["model"] factory = find_factory(model) instance = factory() session.commit() clone = factory_clone(instance, factory) # clone = clone_model(instance) session.expunge(clone) # If we're pretending to be a client, we don't want to send the dump_only fields # that might be computed dump_only = dump_only_fields(resource.schema) request = resource.schema(many=False, use_links=False, exclude=dump_only).dump( clone ) count_1 = session.query(model).count() # Do the request url = url_for(endpoint) rv = client.post(url, json=request, headers={"access_token": admin_token}) # Check the request was successful assert rv.status_code == 201, rv.json["errors"] ret = rv.json del ret["jsonapi"] # Check that we now have data count_2 = session.query(model).count() assert count_2 - count_1 == 1 # Validate the returned data data = resource.schema(many=False, unknown=EXCLUDE).load(ret)
Example #5
Source File: pagination.py From flask-smorest with MIT License | 5 votes |
def _pagination_parameters_schema_factory( def_page, def_page_size, def_max_page_size): """Generate a PaginationParametersSchema""" class PaginationParametersSchema(ma.Schema): """Deserializes pagination params into PaginationParameters""" class Meta: ordered = True if MARSHMALLOW_VERSION_MAJOR < 3: strict = True else: unknown = ma.EXCLUDE page = ma.fields.Integer( missing=def_page, validate=ma.validate.Range(min=1) ) page_size = ma.fields.Integer( missing=def_page_size, validate=ma.validate.Range(min=1, max=def_max_page_size) ) @ma.post_load def make_paginator(self, data, **kwargs): return PaginationParameters(**data) return PaginationParametersSchema
Example #6
Source File: conftest.py From flask-smorest with MIT License | 5 votes |
def schemas(): class DocSchema(CounterSchema): if MARSHMALLOW_VERSION_MAJOR < 3: class Meta: strict = True item_id = ma.fields.Int(dump_only=True) field = ma.fields.Int(attribute='db_field') class DocEtagSchema(CounterSchema): if MARSHMALLOW_VERSION_MAJOR < 3: class Meta: strict = True field = ma.fields.Int(attribute='db_field') class QueryArgsSchema(ma.Schema): class Meta: ordered = True if MARSHMALLOW_VERSION_MAJOR < 3: strict = True else: unknown = ma.EXCLUDE arg1 = ma.fields.String() arg2 = ma.fields.Integer() return namedtuple( 'Model', ('DocSchema', 'DocEtagSchema', 'QueryArgsSchema'))( DocSchema, DocEtagSchema, QueryArgsSchema)
Example #7
Source File: base.py From aries-cloudagent-python with Apache License 2.0 | 5 votes |
def validate(self): """Validate a constructed model.""" schema = self.Schema(unknown=EXCLUDE) errors = schema.validate(self.serialize()) if errors: raise ValidationError(errors) return self
Example #8
Source File: cache.py From renku-python with Apache License 2.0 | 5 votes |
def project_clone_view(user_data): """Clone a remote repository.""" project_data = ProjectCloneContext().load({ **user_data, **request.json }, unknown=EXCLUDE) project = _project_clone(user_data, project_data) return result_response(ProjectCloneResponseRPC(), project)
Example #9
Source File: templates.py From renku-python with Apache License 2.0 | 5 votes |
def read_manifest_from_template(user, cache): """Read templates from the manifest file of a template repository.""" project_data = ManifestTemplatesRequest().load({ **user, **request.args, }, unknown=EXCLUDE) project = _project_clone(user, project_data) manifest = read_template_manifest(project.abs_path) return result_response( ManifestTemplatesResponseRPC(), {'templates': manifest} )
Example #10
Source File: projects.py From renku-python with Apache License 2.0 | 5 votes |
def make_project(self, user, project_data): """Store user project metadata.""" project_data.update({'user_id': user.user_id}) project_obj = self.project_schema.load(project_data, unknown=EXCLUDE) project_obj.save() return project_obj
Example #11
Source File: compat.py From flask-rebar with MIT License | 5 votes |
def exclude_unknown_fields(schema): schema.unknown = marshmallow.EXCLUDE return schema
Example #12
Source File: datasets.py From laguinho-api with MIT License | 5 votes |
def publish(): result = dataset_metadata.load(request.json, unknown=EXCLUDE) if dataset_exists(result): return jsonify('Dataset already exists'), 409 datasets_metadata.insert_one(result) del result['_id'] return jsonify(result), 201
Example #13
Source File: test_marshmallow.py From umongo with MIT License | 4 votes |
def test_custom_ma_base_schema_cls(self): # Define custom marshmallow schema base class class ExcludeBaseSchema(ma.Schema): class Meta: unknown = ma.EXCLUDE # Typically, we'll use it in all our schemas, so let's define base # Document and EmbeddedDocument classes using this base schema class @self.instance.register class MyDocument(Document): MA_BASE_SCHEMA_CLS = ExcludeBaseSchema @self.instance.register class MyEmbeddedDocument(EmbeddedDocument): MA_BASE_SCHEMA_CLS = ExcludeBaseSchema # Now, all our objects will generate "exclude" marshmallow schemas @self.instance.register class Accessory(MyEmbeddedDocument): brief = fields.StrField() value = fields.IntField() @self.instance.register class Bag(MyDocument): item = fields.EmbeddedField(Accessory) content = fields.ListField(fields.EmbeddedField(Accessory)) data = { 'item': {'brief': 'sportbag', 'value': 100, 'name': 'Unknown'}, 'content': [ {'brief': 'cellphone', 'value': 500, 'name': 'Unknown'}, {'brief': 'lighter', 'value': 2, 'name': 'Unknown'} ], 'name': 'Unknown', } excl_data = { 'item': {'brief': 'sportbag', 'value': 100}, 'content': [ {'brief': 'cellphone', 'value': 500}, {'brief': 'lighter', 'value': 2}] } ma_schema = Bag.schema.as_marshmallow_schema() assert ma_schema().load(data) == excl_data