Skip to content

Parse a schema that contains both file and form data #713

@greyli

Description

@greyli

If I understand right, currently we have to call use_args twice to accept files and form:

from flask import Flask
from webargs import fields
from webargs.flaskparser import use_args

app = Flask(__name__)


class UploadShema(Schema):
    photo = fields.Field(
        validate=lambda f: f.mimetype in ["image/jpeg", "image/png"],
		metadata={'type': 'file'}
    )


class FormShema(Schema):
    name = fields.String(required=True, validate=Length(0, 10))


@app.route('/', methods=['post'])
@use_args(UploadSchema, location='files')
@use_args(FormSchema, location='form')
def index(args):
    ...

Imagining we have a schema that contains a file field and a string field:

class FooShema(Schema):
    photo = fields.Field(
        validate=lambda f: f.mimetype in ["image/jpeg", "image/png"],
		metadata={'type': 'file'}
    )
	name = fields.String(required=True, validate=Length(0, 10))


@app.route('/', methods=['post'])
@use_args(FooShema, location='form')
def index(args):
    ...

Is it possible to parse this with one use_args call with form location or something like form_and_files? This will be more convenient and intuitive.

I'm trying to add upload support to APIFlask (apiflask/apiflask#123), then I find I have to separate the schema for this situation. So I'm thinking if it's possible to merge the schema and parse them at once. Any ideas will be helpful, thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions