Python marshmallow.fields.Nested() Examples
The following are 30
code examples of marshmallow.fields.Nested().
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.fields
, or try the search function
.
Example #1
Source File: test_errors.py From flask-rebar with MIT License | 9 votes |
def create_app(self): app = Flask(__name__) Rebar().init_app(app=app) class NestedSchema(validation.RequestSchema): baz = fields.List(fields.Integer()) class Schema(validation.RequestSchema): foo = fields.Integer(required=True) bar = fields.Email() nested = fields.Nested(NestedSchema) @app.route("/stuffs", methods=["POST"]) def json_body_handler(): data = get_json_body_params_or_400(schema=Schema) return response(data) return app
Example #2
Source File: test_dump.py From marshmallow-jsonschema with MIT License | 6 votes |
def test_nested_descriptions(): class TestNestedSchema(Schema): myfield = fields.String(metadata={"description": "Brown Cow"}) yourfield = fields.Integer(required=True) class TestSchema(Schema): nested = fields.Nested( TestNestedSchema, metadata={"description": "Nested 1", "title": "Title1"} ) yourfield_nested = fields.Integer(required=True) schema = TestSchema() dumped = validate_and_dump(schema) nested_def = dumped["definitions"]["TestNestedSchema"] nested_dmp = dumped["definitions"]["TestSchema"]["properties"]["nested"] assert nested_def["properties"]["myfield"]["description"] == "Brown Cow" assert nested_dmp["$ref"] == "#/definitions/TestNestedSchema" assert nested_dmp["description"] == "Nested 1" assert nested_dmp["title"] == "Title1"
Example #3
Source File: test_marshmallow.py From pydantic with MIT License | 6 votes |
def __init__(self, allow_extra): class LocationSchema(Schema): latitude = fields.Float(allow_none=True) longitude = fields.Float(allow_none=True) class SkillSchema(Schema): subject = fields.Str(required=True) subject_id = fields.Integer(required=True) category = fields.Str(required=True) qual_level = fields.Str(required=True) qual_level_id = fields.Integer(required=True) qual_level_ranking = fields.Float(default=0) class Model(Schema): id = fields.Integer(required=True) client_name = fields.Str(validate=validate.Length(max=255), required=True) sort_index = fields.Float(required=True) # client_email = fields.Email() client_phone = fields.Str(validate=validate.Length(max=255), allow_none=True) location = fields.Nested(LocationSchema) contractor = fields.Integer(validate=validate.Range(min=0), allow_none=True) upstream_http_referrer = fields.Str(validate=validate.Length(max=1023), allow_none=True) grecaptcha_response = fields.Str(validate=validate.Length(min=20, max=1000), required=True) last_updated = fields.DateTime(allow_none=True) skills = fields.Nested(SkillSchema, many=True) self.allow_extra = allow_extra # unused self.schema = Model()
Example #4
Source File: test_one_of_schema.py From marshmallow-oneofschema with MIT License | 6 votes |
def test_using_as_nested_schema_with_many(self): class SchemaWithMany(m.Schema): items = f.Nested(MySchema, many=True) schema = SchemaWithMany() result = schema.load( { "items": [ {"type": "Foo", "value": "hello world!"}, {"type": "Bar", "value": 123}, ] } ) assert {"items": [Foo("hello world!"), Bar(123)]} == result with pytest.raises(m.ValidationError) as exc_info: schema.load( {"items": [{"type": "Foo", "value": "hello world!"}, {"value": 123}]} ) assert {"items": {1: {"type": [REQUIRED_ERROR]}}} == exc_info.value.messages
Example #5
Source File: test_one_of_schema.py From marshmallow-oneofschema with MIT License | 6 votes |
def test_using_as_nested_schema(self): class SchemaWithList(m.Schema): items = f.List(f.Nested(MySchema)) schema = SchemaWithList() result = schema.load( { "items": [ {"type": "Foo", "value": "hello world!"}, {"type": "Bar", "value": 123}, ] } ) assert {"items": [Foo("hello world!"), Bar(123)]} == result with pytest.raises(m.ValidationError) as exc_info: schema.load( {"items": [{"type": "Foo", "value": "hello world!"}, {"value": 123}]} ) assert {"items": {1: {"type": [REQUIRED_ERROR]}}} == exc_info.value.messages
Example #6
Source File: schema.py From py-ipv8 with GNU Lesser General Public License v3.0 | 6 votes |
def schema(**kwargs): """ Create a schema. Mostly useful for creating single-use schemas on-the-fly. """ items = list(kwargs.items()) if len(items) != 1 or not isinstance(items[0][1], dict): raise RuntimeError('schema required 1 keyword argument of type dict') name, spec = items[0] schema_dict = {} for key, value in spec.items(): cls, description = value if isinstance(value, tuple) else (value, None) required = key.endswith('*') key = key.rstrip('*') kwargs = {'required': required, 'description': description} if isinstance(cls, SchemaMeta): schema_dict[key] = Nested(cls, required=required) elif isinstance(cls, list) and len(cls) == 1: cls = cls[0] schema_dict[key] = List(Nested(cls), **kwargs) if isinstance(cls, SchemaMeta) else List(cls, **kwargs) else: schema_dict[key] = cls.__call__(**kwargs) if callable(cls) else cls return type(name, (Schema,), schema_dict)
Example #7
Source File: test_additional_properties.py From marshmallow-jsonschema with MIT License | 6 votes |
def test_additional_properties_from_nested_meta(additional_properties_value): class TestNestedSchema(Schema): class Meta: additional_properties = additional_properties_value foo = fields.Integer() class TestSchema(Schema): nested = fields.Nested(TestNestedSchema()) schema = TestSchema() dumped = validate_and_dump(schema) assert ( dumped["definitions"]["TestNestedSchema"]["additionalProperties"] == additional_properties_value )
Example #8
Source File: test_dump.py From marshmallow-jsonschema with MIT License | 6 votes |
def test_nested_recursive(): """A self-referential schema should not cause an infinite recurse.""" class RecursiveSchema(Schema): foo = fields.Integer(required=True) children = fields.Nested("RecursiveSchema", many=True) schema = RecursiveSchema() dumped = validate_and_dump(schema) props = dumped["definitions"]["RecursiveSchema"]["properties"] assert "RecursiveSchema" in props["children"]["items"]["$ref"]
Example #9
Source File: test_dump.py From marshmallow-jsonschema with MIT License | 6 votes |
def test_nested_instance(): """Should also work with nested schema instances""" class TestNestedSchema(Schema): baz = fields.Integer() class TestSchema(Schema): foo = fields.String() bar = fields.Nested(TestNestedSchema()) schema = TestSchema() dumped = validate_and_dump(schema) nested_def = dumped["definitions"]["TestNestedSchema"] nested_obj = dumped["definitions"]["TestSchema"]["properties"]["bar"] assert "baz" in nested_def["properties"] assert nested_obj["$ref"] == "#/definitions/TestNestedSchema"
Example #10
Source File: test_dump.py From marshmallow-jsonschema with MIT License | 6 votes |
def test_respect_dotted_exclude_for_nested_schema(): """Should ignore dotted fields in 'exclude' metadata for nested schemas.""" class InnerRecursiveSchema(Schema): id = fields.Integer(required=True) baz = fields.String() recursive = fields.Nested("InnerRecursiveSchema") class MiddleSchema(Schema): id = fields.Integer(required=True) bar = fields.String() inner = fields.Nested("InnerRecursiveSchema") class OuterSchema(Schema): foo2 = fields.Integer(required=True) nested = fields.Nested("MiddleSchema", exclude=("inner.recursive",)) schema = OuterSchema() dumped = validate_and_dump(schema) inner_props = dumped["definitions"]["InnerRecursiveSchema"]["properties"] assert "recursive" not in inner_props
Example #11
Source File: test_dump.py From marshmallow-jsonschema with MIT License | 6 votes |
def test_respect_only_for_nested_schema(): """Should ignore fields not in 'only' metadata for nested schemas.""" class InnerRecursiveSchema(Schema): id = fields.Integer(required=True) baz = fields.String() recursive = fields.Nested("InnerRecursiveSchema") class MiddleSchema(Schema): id = fields.Integer(required=True) bar = fields.String() inner = fields.Nested("InnerRecursiveSchema", only=("id", "baz")) class OuterSchema(Schema): foo2 = fields.Integer(required=True) nested = fields.Nested("MiddleSchema") schema = OuterSchema() dumped = validate_and_dump(schema) inner_props = dumped["definitions"]["InnerRecursiveSchema"]["properties"] assert "recursive" not in inner_props
Example #12
Source File: test_dump.py From marshmallow-jsonschema with MIT License | 6 votes |
def test_nested_string_to_cls(): class TestNamedNestedSchema(Schema): foo = fields.Integer(required=True) class TestSchema(Schema): foo2 = fields.Integer(required=True) nested = fields.Nested("TestNamedNestedSchema") schema = TestSchema() dumped = validate_and_dump(schema) nested_def = dumped["definitions"]["TestNamedNestedSchema"] nested_dmp = dumped["definitions"]["TestSchema"]["properties"]["nested"] assert nested_dmp["type"] == "object" assert nested_def["properties"]["foo"]["format"] == "integer"
Example #13
Source File: test_dump.py From marshmallow-jsonschema with MIT License | 6 votes |
def test_deep_nested(): """Test that deep nested schemas are in definitions.""" class InnerSchema(Schema): boz = fields.Integer(required=True) class InnerMiddleSchema(Schema): baz = fields.Nested(InnerSchema, required=True) class OuterMiddleSchema(Schema): bar = fields.Nested(InnerMiddleSchema, required=True) class OuterSchema(Schema): foo = fields.Nested(OuterMiddleSchema, required=True) schema = OuterSchema() dumped = validate_and_dump(schema) defs = dumped["definitions"] assert "OuterSchema" in defs assert "OuterMiddleSchema" in defs assert "InnerMiddleSchema" in defs assert "InnerSchema" in defs
Example #14
Source File: test_dump.py From marshmallow-jsonschema with MIT License | 6 votes |
def test_list_nested(): """Test that a list field will work with an inner nested field.""" class InnerSchema(Schema): foo = fields.Integer(required=True) class ListSchema(Schema): bar = fields.List(fields.Nested(InnerSchema), required=True) schema = ListSchema() dumped = validate_and_dump(schema) nested_json = dumped["definitions"]["ListSchema"]["properties"]["bar"] assert nested_json["type"] == "array" assert "items" in nested_json item_schema = nested_json["items"] assert "InnerSchema" in item_schema["$ref"]
Example #15
Source File: paging_schema.py From beavy with Mozilla Public License 2.0 | 5 votes |
def makePaginationSchema(itemsCls, field_cls=fields.Nested): return type("{}Paging".format(itemsCls.__class__.__name__), (BasePaging, ), dict(items=field_cls(itemsCls, many=True)))
Example #16
Source File: test_jit.py From toasted-marshmallow with Apache License 2.0 | 5 votes |
def nested_schema(): class GrandChildSchema(Schema): bar = fields.String() raz = fields.String() class SubSchema(Schema): name = fields.String() value = fields.Nested(GrandChildSchema) class NestedSchema(Schema): key = fields.String() value = fields.Nested(SubSchema, only=('name', 'value.bar')) values = fields.Nested(SubSchema, exclude=('value', ), many=True) return NestedSchema()
Example #17
Source File: test_jit.py From toasted-marshmallow with Apache License 2.0 | 5 votes |
def nested_circular_ref_schema(): class NestedStringSchema(Schema): key = fields.String() me = fields.Nested('NestedStringSchema') return NestedStringSchema()
Example #18
Source File: test_field_for_schema.py From marshmallow_dataclass with MIT License | 5 votes |
def test_marshmallow_dataclass(self): class NewSchema(Schema): pass @dataclass(base_schema=NewSchema) class NewDataclass: pass self.assertFieldsEqual( field_for_schema(NewDataclass, metadata=dict(required=False)), fields.Nested(NewDataclass.Schema), )
Example #19
Source File: test_view_errors.py From flask-resty with MIT License | 5 votes |
def schemas(): class NestedSchema(Schema): value = fields.Integer() class WidgetSchema(Schema): id = fields.Integer(as_string=True) name = fields.String(required=True, allow_none=True) nested = fields.Nested(NestedSchema) nested_many = fields.Nested(NestedSchema, many=True) return {"widget": WidgetSchema()}
Example #20
Source File: fields.py From flask-resty with MIT License | 5 votes |
def _validate_missing(self, value): # Do not display detailed error data on required fields in nested # schema - in this context, they're actually not required. super(fields.Nested, self)._validate_missing(value)
Example #21
Source File: test_scheme_from_target.py From marshmallow-annotations with MIT License | 5 votes |
def test_builds_nested_many_field_when_typehint_is_scheme(registry_): class Album: name: str class Artist: name: str albums: t.List[Album] class AlbumScheme(AnnotationSchema): class Meta: registry = registry_ target = Album register_as_scheme = True class ArtistScheme(AnnotationSchema): class Meta: registry = registry_ target = Artist register_as_scheme = True artist_fields = ArtistScheme._declared_fields assert isinstance(artist_fields["albums"], fields.Nested) assert artist_fields["albums"].many
Example #22
Source File: test_type_registry.py From marshmallow-annotations with MIT License | 5 votes |
def test_can_register_scheme_for_type(): class SomeTypeScheme(Schema): pass class SomeType: pass registry = DefaultTypeRegistry() registry.register_scheme_factory(SomeType, "SomeTypeScheme") constructor = registry.get(SomeType) field = constructor(None, (), {}) assert isinstance(field, fields.Nested) assert isinstance(field.schema, SomeTypeScheme)
Example #23
Source File: registry.py From marshmallow-annotations with MIT License | 5 votes |
def scheme_factory(scheme_name: str) -> FieldFactory: """ Maps a scheme or scheme name into a field factory """ def _( converter: AbstractConverter, subtypes: Tuple[type], opts: ConfigOptions ) -> FieldABC: return fields.Nested(scheme_name, **opts) _.__name__ = f"{scheme_name}FieldFactory" _.__is_scheme__ = True # type: ignore return _
Example #24
Source File: utils.py From flask_accepts with BSD 3-Clause "New" or "Revised" License | 5 votes |
def unpack_nested_self(val, api, model_name: str = None, operation: str = "dump"): model_name = model_name or get_default_model_name(val.schema) fields = { k: map_type(v, api, model_name, operation) for k, v in (vars(val.schema).get("fields").items()) if type(v) in type_map and _check_load_dump_only(v, operation) } if val.many: return fr.List( fr.Nested( api.model(f"{model_name}-child", fields), **_ma_field_to_fr_field(val) ) ) else: return fr.Nested( api.model(f"{model_name}-child", fields), **_ma_field_to_fr_field(val) )
Example #25
Source File: utils_test.py From flask_accepts with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_unpack_nested_self_many(): app = Flask(__name__) api = Api(app) class IntegerSchema(Schema): my_int = ma.Integer() children = ma.Nested("self", exclude=["children"], many=True) schema = IntegerSchema() result = utils.unpack_nested(schema.fields.get("children"), api=api) assert type(result) == fr.List
Example #26
Source File: utils_test.py From flask_accepts with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_unpack_nested_self(): app = Flask(__name__) api = Api(app) class IntegerSchema(Schema): my_int = ma.Integer() children = ma.Nested("self", exclude=["children"]) schema = IntegerSchema() result = utils.unpack_nested(schema.fields.get("children"), api=api) assert type(result) == fr.Nested
Example #27
Source File: utils_test.py From flask_accepts with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_unpack_nested(): app = Flask(__name__) api = Api(app) class IntegerSchema(Schema): my_int: ma.Integer() result = utils.unpack_nested(ma.Nested(IntegerSchema), api=api) assert result
Example #28
Source File: decorators_test.py From flask_accepts with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_accepts_with_nested_schema(app, client): # noqa class TestSchema(Schema): _id = fields.Integer() name = fields.String() class HostSchema(Schema): name = fields.String() child = fields.Nested(TestSchema) api = Api(app) @api.route("/test") class TestResource(Resource): @accepts( "Foo", dict(name="foo", type=int, help="An important foo"), schema=HostSchema, api=api, ) def post(self): assert request.parsed_obj assert request.parsed_obj["child"] == {"_id": 42, "name": "test name"} assert request.parsed_obj["name"] == "test host" return "success" with client as cl: resp = cl.post( "/test?foo=3", json={"name": "test host", "child": {"_id": 42, "name": "test name"}}, ) assert resp.status_code == 200
Example #29
Source File: test_additional_properties.py From marshmallow-jsonschema with MIT License | 5 votes |
def test_additional_properties_nested_default(): class TestNestedSchema(Schema): foo = fields.Integer() class TestSchema(Schema): nested = fields.Nested(TestNestedSchema()) schema = TestSchema() dumped = validate_and_dump(schema) assert not dumped["definitions"]["TestSchema"]["additionalProperties"]
Example #30
Source File: marshmallow.py From lux with BSD 3-Clause "New" or "Revised" License | 4 votes |
def field2length(field): """Return the dictionary of swagger field attributes for a set of :class:`Length <marshmallow.validators.Length>` validators. :param Field field: A marshmallow field. :rtype: dict """ attributes = {} validators = [ validator for validator in field.validators if ( hasattr(validator, 'min') and hasattr(validator, 'max') and hasattr(validator, 'equal') ) ] is_array = isinstance(field, (fields.Nested, fields.List)) min_attr = 'minItems' if is_array else 'minLength' max_attr = 'maxItems' if is_array else 'maxLength' for validator in validators: if validator.min is not None: if hasattr(attributes, min_attr): attributes[min_attr] = max( attributes[min_attr], validator.min ) else: attributes[min_attr] = validator.min if validator.max is not None: if hasattr(attributes, max_attr): attributes[max_attr] = min( attributes[max_attr], validator.max ) else: attributes[max_attr] = validator.max for validator in validators: if validator.equal is not None: attributes[min_attr] = validator.equal attributes[max_attr] = validator.equal return attributes