简体   繁体   中英

Python Marshmallow json validation with nested object array

New user to Python Flask API and Marshmallow schema validation. Quick question here, maybe misunderstant by myself.

Got an Python object (class) who contains variables and others objects in an array like:

class NestedObj2(Schema):
     field_a = fields.String(required=True, validate=validate.Length(max=4))
     
     class Meta:
         fields = (['field_a'])

class NestedObj1(Schema):
     field_b = fields.String(required=True, validate=validate.Length(max=10))
     field_c = fields.Integer(required=True)
     field_d = fields.Nested(NestedObj2(), required=True, many=True)

     class Meta:
         fields = ('field_b', 'field_c', 'field_d')

class RequestObject(Schema):
     field_d = fields.String(required=True, validate=validate.Length(max=10)
     field_e = fields.Number(required=True)
     field_f = fields.Boolean(required=True)
     field_g = fields.Nested(NestedObj1(), required=True, many=True)
 
     class Meta:
         fields = ('field_d', 'field_e', 'field_f', 'field_g')

And I want to validate a JSON payload (sent by Postman for example) with command: RequestObject().validate(request.get_json())

I'm doing this validation method on others simples API without array but here, Marshmallow gaves me error on

{
    "field_g": {
        "_schema": [
            "Invalid input type."
        ]
    }
}

Payload to validate:

{
    "field_d": "my_string",
    "field_e": "100.0",
    "field_f": True,
    "field_g": [
        {
            "field_b": "2nd_str",
            "field_c": "200.0",
            "field_d": [
                {
                    "field_a": "3rds"
                }
            ]
        }
    ]
}

Dunno what I'm doing wrong...

If anybody has explanation:)

Kind regards

We use Python Flask API with webargs.flaskparser for validation (which uses the marshmallow fields), so maybe your problem is due to the same reason as one we had. We had some arguments that were arrays of dictionaries, like

speakers = [
  {'name': 'some name', 'info': 'some info'}, 
  {'name': 'name2', 'info': 'info2'}
]

We had set up the validation as

fields.List(fields.Dict(), required=False)

and kept getting a validation error. We found out this was because as part of the json encoding that was done to send the request to our Python API, the dictionaries were getting encoded as strings. So really the arguments were

speakers = [
      "{'name': 'some name', 'info': 'some info'}", 
      "{'name': 'name2', 'info': 'info2'}"
    ]

We ended up changing the validation to

fields.List(fields.Str(), required=False)

getting the dictionary structure using json.loads:

import json
speakers = json.loads(args['speakers'])

and then performing a validation on our own without webargs/marshmallow. Maybe something similar will help in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM