Skip to content

Type serialization does not work inside valuesrules or valueschema #1511

@absassi

Description

@absassi

Cerberus 1.3 deprecates keyschema and valueschema in favor of keysrules and valuesrules. It also modifies any schema passed to it to rename the deprecated names with the corresponding new names.

However, Eve still uses valueschema in eve.methods.common.serialize (eve/methods/common.py line 523) and doesn't look for valuesrules. This means that if the domain configuration contains a valuesrules (or valueschema - it doesn't matter, because Cerberus renames them) with a type that requires conversion (such as datetime, for instance), that conversion will not happen.

Unfortunately, downgrading to Cerberus 1.2 is not an option for me, as it doesn't work in Python 3.11.


Expected Behavior

The following minimal example program and request should successfully insert a new document in the "example" collection:

from eve import Eve

app = Eve(
    settings={
        "DOMAIN": {
            "example": {
                "resource_methods": ["POST"],
                "schema": {
                    "field1": {
                        "type": "dict",
                        "valueschema": {"type": "datetime"},
                    },
                    "field2": {
                        "type": "dict",
                        "valuesrules": {"type": "datetime"},
                    },
                },
            },
        },
    })
app.run()
curl -XPOST -H 'Content-type: application/json' --data '{"field1": {"a": "Fri, 04 Aug 2023 12:26:15 GMT"}, "field2": {"a": "Fri, 04 Aug 2023 12:26:15 GMT"}}' http://localhost:5000/example

Actual Behavior

Eve does not convert the strings to datetime before passing them on to validation, as it would if the "type": "datetime" were not inside valuesrules. The request then fails with:

{"_status": "ERR", "_issues": {"field1": {"a": "must be of datetime type"}, "field2": {"a": "must be of datetime type"}}, "_error": {"code": 422, "message": "Insertion failure: 1 document(s) contain(s) error(s)"}}

Environment

  • Python version: 3.11
  • Eve version: 2.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions