How to use the marshmallow.fields.Nested function in marshmallow

To help you get started, we’ve selected a few marshmallow examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github marshmallow-code / marshmallow / tests / test_marshalling.py View on Github external
def test_deserialize_wrong_nested_type_with_validates_method(self, unmarshal):
        class TestSchema(Schema):
            value = fields.String()

            @validates('value')
            def validate_value(self, value):
                pass

        data = {
            'foo': 'not what we need'
        }
        fields_dict = {
            'foo': fields.Nested(TestSchema, required=True)
        }
        with pytest.raises(ValidationError) as excinfo:
            result = unmarshal.deserialize(data, fields_dict)

            assert result is None
            assert excinfo.value.messages == {'foo': {'_schema': ['Invalid input type.']}}
github hugapi / hug / examples / sqlalchemy_example / demo / validation.py View on Github external
).scalar():
            raise ValueError("User with a username {0} already exists.".format(data["username"]))


class DumpUserSchema(ModelSchema):
    @property
    def session(self):
        return self.context.db

    class Meta:
        model = TestModel
        fields = ("name",)


class DumpSchema(Schema):
    users = fields.Nested(DumpUserSchema, many=True)
github labd / commercetools-python-sdk / src / commercetools / schemas / _shopping_list.py View on Github external
delete_days_after_last_modification = marshmallow.fields.Integer(
        allow_none=True, missing=None, data_key="deleteDaysAfterLastModification"
    )
    description = LocalizedStringField(allow_none=True, missing=None)
    key = marshmallow.fields.String(allow_none=True, missing=None)
    line_items = marshmallow.fields.Nested(
        nested="commercetools.schemas._shopping_list.ShoppingListLineItemSchema",
        unknown=marshmallow.EXCLUDE,
        allow_none=True,
        many=True,
        missing=None,
        data_key="lineItems",
    )
    name = LocalizedStringField(allow_none=True)
    slug = LocalizedStringField(allow_none=True, missing=None)
    text_line_items = marshmallow.fields.Nested(
        nested="commercetools.schemas._shopping_list.TextLineItemSchema",
        unknown=marshmallow.EXCLUDE,
        allow_none=True,
        many=True,
        missing=None,
        data_key="textLineItems",
    )
    anonymous_id = marshmallow.fields.String(
        allow_none=True, missing=None, data_key="anonymousId"
    )

    class Meta:
        unknown = marshmallow.EXCLUDE

    @marshmallow.post_load
    def post_load(self, data, **kwargs):
github labd / commercetools-python-sdk / src / commercetools / schemas / _category.py View on Github external
"Marshmallow schema for :class:`commercetools.types.CategorySetCustomFieldAction`."
    name = marshmallow.fields.String(allow_none=True)
    value = marshmallow.fields.Raw(allow_none=True, missing=None)

    class Meta:
        unknown = marshmallow.EXCLUDE

    @marshmallow.post_load
    def post_load(self, data, **kwargs):
        del data["action"]
        return types.CategorySetCustomFieldAction(**data)


class CategorySetCustomTypeActionSchema(CategoryUpdateActionSchema):
    "Marshmallow schema for :class:`commercetools.types.CategorySetCustomTypeAction`."
    type = marshmallow.fields.Nested(
        nested="commercetools.schemas._type.TypeResourceIdentifierSchema",
        unknown=marshmallow.EXCLUDE,
        allow_none=True,
        missing=None,
    )
    fields = FieldContainerField(allow_none=True, missing=None)

    class Meta:
        unknown = marshmallow.EXCLUDE

    @marshmallow.post_load
    def post_load(self, data, **kwargs):
        del data["action"]
        return types.CategorySetCustomTypeAction(**data)
github labd / commercetools-python-sdk / src / commercetools / schemas / _order_edit.py View on Github external
data_key="stagedAction",
    )

    class Meta:
        unknown = marshmallow.EXCLUDE

    @marshmallow.post_load
    def post_load(self, data, **kwargs):
        del data["action"]
        return types.OrderEditAddStagedActionAction(**data)


class OrderEditAppliedSchema(OrderEditResultSchema):
    "Marshmallow schema for :class:`commercetools.types.OrderEditApplied`."
    applied_at = marshmallow.fields.DateTime(allow_none=True, data_key="appliedAt")
    excerpt_before_edit = marshmallow.fields.Nested(
        nested="commercetools.schemas._order_edit.OrderExcerptSchema",
        unknown=marshmallow.EXCLUDE,
        allow_none=True,
        data_key="excerptBeforeEdit",
    )
    excerpt_after_edit = marshmallow.fields.Nested(
        nested="commercetools.schemas._order_edit.OrderExcerptSchema",
        unknown=marshmallow.EXCLUDE,
        allow_none=True,
        data_key="excerptAfterEdit",
    )

    class Meta:
        unknown = marshmallow.EXCLUDE

    @marshmallow.post_load
github polyaxon / polyaxon / cli / polyaxon / schemas / polyflow / component.py View on Github external
# coding: utf-8
from __future__ import absolute_import, division, print_function

from marshmallow import fields, validate
from polyaxon_sdk import V1Component

from polyaxon.schemas.polyflow.base import BaseComponentConfig, BaseComponentSchema
from polyaxon.schemas.polyflow.io import IOSchema
from polyaxon.schemas.polyflow.run import RunMixin, RunSchema


class ComponentSchema(BaseComponentSchema):
    kind = fields.Str(allow_none=True, validate=validate.Equal("component"))
    inputs = fields.Nested(IOSchema, allow_none=True, many=True)
    outputs = fields.Nested(IOSchema, allow_none=True, many=True)
    run = fields.Nested(RunSchema, required=True)

    @staticmethod
    def schema_config():
        return ComponentConfig


class ComponentConfig(BaseComponentConfig, RunMixin, V1Component):
    SCHEMA = ComponentSchema
    IDENTIFIER = "component"
    REDUCED_ATTRIBUTES = BaseComponentConfig.REDUCED_ATTRIBUTES + [
        "inputs",
        "outputs",
        "run",
    ]

    def get_run_kind(self):
github polyaxon / polyaxon / polyaxon_schemas / ops / environments / legacy.py View on Github external
self.gpu_options = gpu_options
        self.log_device_placement = log_device_placement
        self.allow_soft_placement = allow_soft_placement
        self.intra_op_parallelism_threads = intra_op_parallelism_threads
        self.inter_op_parallelism_threads = inter_op_parallelism_threads


class TFRunSchema(BaseSchema):
    tf_random_seed = fields.Int(allow_none=True)
    save_summary_steps = fields.Int(allow_none=True)
    save_checkpoints_secs = fields.Int(allow_none=True)
    save_checkpoints_steps = fields.Int(allow_none=True)
    keep_checkpoint_max = fields.Int(allow_none=True)
    keep_checkpoint_every_n_hours = fields.Int(allow_none=True)

    session = fields.Nested(SessionSchema, allow_none=True)
    cluster = fields.Nested(TensorflowClusterSchema, allow_none=True)

    @staticmethod
    def schema_config():
        return TFRunConfig


class TFRunConfig(BaseConfig):
    IDENTIFIER = "run"
    SCHEMA = TFRunSchema

    def __init__(
        self,
        tf_random_seed=None,
        save_summary_steps=100,
        save_checkpoints_secs=None,
github polyaxon / polyaxon / polyaxon_schemas / ops / group / op.py View on Github external
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function

from marshmallow import fields, validate

from polyaxon_schemas.ops.experiment import ExperimentConfig, ExperimentSchema
from polyaxon_schemas.ops.group.hptuning import HPTuningSchema


class GroupSchema(ExperimentSchema):
    kind = fields.Str(allow_none=True, validate=validate.Equal("group"))
    hptuning = fields.Nested(HPTuningSchema, allow_none=True)

    @staticmethod
    def schema_config():
        return GroupConfig


class GroupConfig(ExperimentConfig):
    SCHEMA = GroupSchema
    IDENTIFIER = "group"
    REDUCED_ATTRIBUTES = ExperimentConfig.REDUCED_ATTRIBUTES + ["hptuning"]

    def __init__(
        self,
        version=None,
        kind=None,
        logging=None,
github apryor6 / flask_accepts / examples / simple_app.py View on Github external
from flask import Flask, request
from flask_accepts import accepts
from flask_restx import Api, Namespace, Resource
from marshmallow import Schema, fields


class TestSchema(Schema):
    id = fields.Integer(attribute="id")


class HigherSchema(Schema):
    id = fields.Integer(attribute="id")
    _list = fields.List(fields.Integer())
    test = fields.Nested(TestSchema, attribute="test")
    list_nested = fields.List(fields.Nested(TestSchema))


o = {"id": 1}

app = Flask(__name__)
api = Api(app)
ns = Namespace("test")


@ns.route("/")
class TestResource(Resource):
    @accepts(
        dict(name="hey", type=str),
        dict(name="test", type=int, required=True, default=3),
        schema=HigherSchema,
github cryptowatch / cw-sdk-python / cryptowatch / resources / instruments.py View on Github external
    @post_load
    def make_instruments(self, data, **kwargs):
        return InstrumentResource(**data)


class InstrumentAPIResponseSchema(Schema):
    result = fields.Nested(InstrumentSchema)
    allowance = fields.Nested(AllowanceSchema, partial=("account",), missing=None)

    @post_load
    def make_instrument_api_resp(self, data, **kwargs):
        return InstrumentAPIResponse(**data)


class InstrumentListAPIResponseSchema(Schema):
    result = fields.Nested(InstrumentSchema, many=True)
    allowance = fields.Nested(AllowanceSchema, partial=("account",), missing=None)

    @post_load
    def make_instrument_list_api_resp(self, data, **kwargs):
        return InstrumentListAPIResponse(**data)


class InstrumentAPIResponse:
    def __init__(self, result, allowance):
        self.instrument = result
        self._allowance = allowance
        self._fetched_at = dt.datetime.now()

    def __repr__(self):
        return "".format(self=self)