I'm trying to define a Marshmallow Schema with a list of nested fields and use it as a validator for a Cornice Service. The Schema validation works as intended but I got "Not a valid list" error for my associations
fields.
My code :
from cornice import Service
from cornice.validators import marshmallow_body_validator
from marshmallow import Schema, fields
# My Cornice Service :
associate = Service(
name="associate",
path="/projets/{project_id}/associate",
description="Associate model to Project",
)
# My Marshmallow Schemas :
class AssociationsSchema(Schema):
left_id = fields.Int(required=True)
right_id = fields.Int(required=True)
class GenericAssociationSchema(Schema):
left_model = fields.Str(required=True)
right_model = fields.Str(required=True)
associations = fields.List(fields.Nested(AssociationsSchema))
# POST request
@associate.post(schema=GenericAssociationSchema, validators=(marshmallow_body_validator,))
def associate_post(request):
return {"success": True}
My pytest tests :
# testapp is a custom pytest fixture
def test_load_schema(testapp):
json = {
"left_model": "projet",
"right_model": "individu",
"associations": [
{
"left_id": 1,
"right_id": 5,
"additionnal_fields": {
"fonction_id": 3,
"role_id": 1
}
}
]
}
schema = GenericAssociationSchema()
result = schema.load(json)
assert result == {}
# PASSED
def test_post_associate_projectindividu_success(testapp):
response = testapp.post(
"/projets/1/associate",
{
"left_model": "projet",
"right_model": "individu",
"associations": [
{
"left_id": 1,
"right_id": 5,
"additionnal_fields": {
"fonction_id": 3,
"role_id": 1
}
}
]
},
status=400,
)
assert response.json == {"success": "true"}
# FAILED : AssertionError: assert {'status': 'error', 'errors': [{'location': 'body', 'name': 'associations', 'description': ['Not a valid list.']}]} == {'success': 'true'}
Without the Nested List of Schema, the service is working fine. Any ideas why I'm getting this error ?