本文整理汇总了Python中marshmallow.fields.String方法的典型用法代码示例。如果您正苦于以下问题:Python fields.String方法的具体用法?Python fields.String怎么用?Python fields.String使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类marshmallow.fields
的用法示例。
在下文中一共展示了fields.String方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_metadata
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import String [as 别名]
def test_metadata():
"""Metadata should be available in the field definition."""
class TestSchema(Schema):
myfield = fields.String(metadata={"foo": "Bar"})
yourfield = fields.Integer(required=True, baz="waz")
schema = TestSchema()
dumped = validate_and_dump(schema)
props = dumped["definitions"]["TestSchema"]["properties"]
assert props["myfield"]["foo"] == "Bar"
assert props["yourfield"]["baz"] == "waz"
assert "metadata" not in props["myfield"]
assert "metadata" not in props["yourfield"]
# repeat process to assert idempotency
dumped = validate_and_dump(schema)
props = dumped["definitions"]["TestSchema"]["properties"]
assert props["myfield"]["foo"] == "Bar"
assert props["yourfield"]["baz"] == "waz"
示例2: test_nested_descriptions
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import String [as 别名]
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"
示例3: test_respect_only_for_nested_schema
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import String [as 别名]
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
示例4: test_respect_dotted_exclude_for_nested_schema
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import String [as 别名]
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
示例5: test_nested_instance
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import String [as 别名]
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"
示例6: test_unknown_typed_field
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import String [as 别名]
def test_unknown_typed_field():
class Colour(fields.Field):
def _jsonschema_type_mapping(self):
return {"type": "string"}
def _serialize(self, value, attr, obj):
r, g, b = value
r = hex(r)[2:]
g = hex(g)[2:]
b = hex(b)[2:]
return "#" + r + g + b
class UserSchema(Schema):
name = fields.String(required=True)
favourite_colour = Colour()
schema = UserSchema()
dumped = validate_and_dump(schema)
assert dumped["definitions"]["UserSchema"]["properties"]["favourite_colour"] == {
"type": "string"
}
示例7: test_regexp
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import String [as 别名]
def test_regexp():
ipv4_regex = (
r"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}"
r"([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
)
class TestSchema(Schema):
ip_address = fields.String(validate=validate.Regexp(ipv4_regex))
schema = TestSchema()
dumped = validate_and_dump(schema)
assert dumped["definitions"]["TestSchema"]["properties"]["ip_address"] == {
"title": "ip_address",
"type": "string",
"pattern": ipv4_regex,
}
示例8: test_arguments_are_added_to_request_with_Resource_and_schema
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import String [as 别名]
def test_arguments_are_added_to_request_with_Resource_and_schema(app, client): # noqa
class TestSchema(Schema):
_id = fields.Integer()
name = fields.String()
api = Api(app)
@api.route("/test")
class TestResource(Resource):
@accepts(
"Foo",
dict(name="foo", type=int, help="An important foo"),
schema=TestSchema,
api=api,
)
def post(self):
assert request.parsed_obj
assert request.parsed_obj["_id"] == 42
assert request.parsed_obj["name"] == "test name"
return "success"
with client as cl:
resp = cl.post("/test?foo=3", json={"_id": 42, "name": "test name"})
assert resp.status_code == 200
示例9: test_accepts_with_postional_args_query_params_schema_and_header_schema
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import String [as 别名]
def test_accepts_with_postional_args_query_params_schema_and_header_schema(app, client): # noqa
class QueryParamsSchema(Schema):
query_param = fields.List(fields.String(), required=True)
class HeadersSchema(Schema):
Header = fields.Integer(required=True)
@app.route("/test")
@accepts(
dict(name="foo", type=int, help="An important foo"),
query_params_schema=QueryParamsSchema,
headers_schema=HeadersSchema)
def test():
assert request.parsed_args["foo"] == 3
assert request.parsed_query_params["query_param"] == ["baz", "qux"]
assert request.parsed_headers["Header"] == 3
return "success"
with client as cl:
resp = cl.get("/test?foo=3&query_param=baz&query_param=qux", headers={"Header": "3"})
assert resp.status_code == 200
示例10: test_responds
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import String [as 别名]
def test_responds(app, client): # noqa
class TestSchema(Schema):
_id = fields.Integer()
name = fields.String()
api = Api(app)
@api.route("/test")
class TestResource(Resource):
@responds(schema=TestSchema, api=api)
def get(self):
obj = {"_id": 42, "name": "Jon Snow"}
return obj
with client as cl:
resp = cl.get("/test")
obj = resp.json
assert obj["_id"] == 42
assert obj["name"] == "Jon Snow"
示例11: test_respond_schema_instance_respects_exclude
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import String [as 别名]
def test_respond_schema_instance_respects_exclude(app, client): # noqa
class TestSchema(Schema):
_id = fields.Integer()
name = fields.String()
api = Api(app)
@api.route("/test")
class TestResource(Resource):
@responds(schema=TestSchema(exclude=("_id",)), api=api)
def get(self):
obj = {"_id": 42, "name": "Jon Snow"}
return obj
with client as cl:
resp = cl.get("/test")
obj = resp.json
assert "_id" not in obj
assert obj["name"] == "Jon Snow"
示例12: test_respond_schema_respects_many
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import String [as 别名]
def test_respond_schema_respects_many(app, client): # noqa
class TestSchema(Schema):
_id = fields.Integer()
name = fields.String()
api = Api(app)
@api.route("/test")
class TestResource(Resource):
@responds(schema=TestSchema, many=True, api=api)
def get(self):
obj = [{"_id": 42, "name": "Jon Snow"}]
return obj
with client as cl:
resp = cl.get("/test")
obj = resp.json
assert obj == [{"_id": 42, "name": "Jon Snow"}]
示例13: test_respond_schema_instance_respects_many
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import String [as 别名]
def test_respond_schema_instance_respects_many(app, client): # noqa
class TestSchema(Schema):
_id = fields.Integer()
name = fields.String()
api = Api(app)
@api.route("/test")
class TestResource(Resource):
@responds(schema=TestSchema(many=True), api=api)
def get(self):
obj = [{"_id": 42, "name": "Jon Snow"}]
return obj
with client as cl:
resp = cl.get("/test")
obj = resp.json
assert obj == [{"_id": 42, "name": "Jon Snow"}]
示例14: test_responds_regular_route
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import String [as 别名]
def test_responds_regular_route(app, client): # noqa
class TestSchema(Schema):
_id = fields.Integer()
name = fields.String()
@app.route("/test", methods=["GET"])
@responds(schema=TestSchema)
def get():
obj = {"_id": 42, "name": "Jon Snow"}
return obj
with client as cl:
resp = cl.get("/test")
obj = resp.json
assert obj["_id"] == 42
assert obj["name"] == "Jon Snow"
示例15: test_responds_passes_raw_responses_through_untouched
# 需要导入模块: from marshmallow import fields [as 别名]
# 或者: from marshmallow.fields import String [as 别名]
def test_responds_passes_raw_responses_through_untouched(app, client): # noqa
class TestSchema(Schema):
_id = fields.Integer()
name = fields.String()
api = Api(app)
@api.route("/test")
class TestResource(Resource):
@responds(schema=TestSchema, api=api)
def get(self):
from flask import make_response, Response
obj = {"_id": 42, "name": "Jon Snow"}
return Response("A prebuild response that won't be serialised", 201)
with client as cl:
resp = cl.get("/test")
assert resp.status_code == 201