Python marshmallow.fields.Function() Examples

The following are 1 code examples of marshmallow.fields.Function(). 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_dump.py    From marshmallow-jsonschema with MIT License 6 votes vote down vote up
def test_function():
    """Function fields can be serialised if type is given."""

    class FnSchema(Schema):
        fn_str = fields.Function(
            lambda: "string", required=True, _jsonschema_type_mapping={"type": "string"}
        )
        fn_int = fields.Function(
            lambda: 123, required=True, _jsonschema_type_mapping={"type": "number"}
        )

    schema = FnSchema()

    dumped = validate_and_dump(schema)

    props = dumped["definitions"]["FnSchema"]["properties"]
    assert props["fn_int"]["type"] == "number"
    assert props["fn_str"]["type"] == "string"