OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to solve Invalid type when using marshmallow to validate a list of nested fields?

  • Thread starter Thread starter Vinicius Kreischer
  • Start date Start date
V

Vinicius Kreischer

Guest
I have a Flask application where I use marshmallow schemas to validate my requests, as presented below:

Code:
class HyperParameterSchema(Schema):
    name = fields.Str(required=True)
    data_type = fields.Str(required=True)
    default_value = fields.Str(required=True)


class LearnerRequestSchema(Schema):
    files = fields.Raw(type="file")
    name = fields.Str(required=True)
    description = fields.Str(required=True)
    supervision_scope = fields.Enum(SupervisionScope, by_value=True, required=True)
    task_type = fields.Enum(TaskType, by_value=True, required=True)
    tool = fields.Enum(Tool, by_value=True, required=True)
    learner_family_id = fields.Int(required=True)
    hyperparams = fields.List(
        fields.Nested(HyperParameterSchema, many=True),
    )

When I perform requests like the following:

Code:
response = requests.post(
    url="http://localhost:8080/api/learners",
    data={
        "name": "some_name",
        "description": "some_description",
        "supervision_scope": "supervised",
        "task_type": "classification",
        "tool": "scikit-learn",
        "learner_family_id": 1,
        "hyperparams": json.dumps(
            [
                {"name": "alpha", "data_type": "float", "default_value": "0.1"},
                {"name": "l1_ratio", "data_type": "float", "default_value": "0.1"},
            ]
        ),
    },
    files={
        "file": open("path_to_file", "rb")
    },
    headers=headers,
)

I keep getting as response b'{"errors":{"form":{"hyperparams":{"0":["Invalid type."]}}}}\n'.

Shouldn't the validation works the way it is implemented? Could somebody help me find out what I might be doing wrong? Thanks!

Currently, I'am using a fields.Raw class to deal with the problem related above:

Code:
class LearnerRequestSchema(Schema):
    files = fields.Raw(type="file")
    name = fields.Str(required=True)
    description = fields.Str(required=True)
    supervision_scope = fields.Enum(SupervisionScope, by_value=True, required=True)
    task_type = fields.Enum(TaskType, by_value=True, required=True)
    tool = fields.Enum(Tool, by_value=True, required=True)
    learner_family_id = fields.Int(required=True)
    hyperparams = fields.Raw()

The issue is that I have to manually deserialize the hyperparams field in order to check that desired fields are present, while in the former approach I would get the validation out of the box.
<p>I have a Flask application where I use marshmallow schemas to validate my requests, as presented below:</p>
<pre><code>class HyperParameterSchema(Schema):
name = fields.Str(required=True)
data_type = fields.Str(required=True)
default_value = fields.Str(required=True)


class LearnerRequestSchema(Schema):
files = fields.Raw(type="file")
name = fields.Str(required=True)
description = fields.Str(required=True)
supervision_scope = fields.Enum(SupervisionScope, by_value=True, required=True)
task_type = fields.Enum(TaskType, by_value=True, required=True)
tool = fields.Enum(Tool, by_value=True, required=True)
learner_family_id = fields.Int(required=True)
hyperparams = fields.List(
fields.Nested(HyperParameterSchema, many=True),
)
</code></pre>
<p>When I perform requests like the following:</p>
<pre><code>response = requests.post(
url="http://localhost:8080/api/learners",
data={
"name": "some_name",
"description": "some_description",
"supervision_scope": "supervised",
"task_type": "classification",
"tool": "scikit-learn",
"learner_family_id": 1,
"hyperparams": json.dumps(
[
{"name": "alpha", "data_type": "float", "default_value": "0.1"},
{"name": "l1_ratio", "data_type": "float", "default_value": "0.1"},
]
),
},
files={
"file": open("path_to_file", "rb")
},
headers=headers,
)
</code></pre>
<p>I keep getting as response <strong>b'{"errors":{"form":{"hyperparams":{"0":["Invalid type."]}}}}\n'</strong>.</p>
<p>Shouldn't the validation works the way it is implemented? Could somebody help me find out what I might be doing wrong? Thanks!</p>
<p>Currently, I'am using a fields.Raw class to deal with the problem related above:</p>
<pre><code>class LearnerRequestSchema(Schema):
files = fields.Raw(type="file")
name = fields.Str(required=True)
description = fields.Str(required=True)
supervision_scope = fields.Enum(SupervisionScope, by_value=True, required=True)
task_type = fields.Enum(TaskType, by_value=True, required=True)
tool = fields.Enum(Tool, by_value=True, required=True)
learner_family_id = fields.Int(required=True)
hyperparams = fields.Raw()
</code></pre>
<p>The issue is that I have to manually deserialize the hyperparams field in order to check that desired fields are present, while in the former approach I would get the validation out of the box.</p>
 

Latest posts

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top