-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathmarshmallow_validator_example.py
More file actions
44 lines (30 loc) · 981 Bytes
/
marshmallow_validator_example.py
File metadata and controls
44 lines (30 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""An example of Marshmallow validators within flask_accepts"""
from dataclasses import dataclass
from marshmallow import fields, Schema, post_load, validate
from flask import Flask, jsonify, request
from flask_accepts import accepts, responds
@dataclass
class Widget:
foo: str
baz: int
class WidgetSchema(Schema):
foo = fields.String(validate=validate.Length(min=3))
baz = fields.Integer()
@post_load
def make(self, data, **kwargs):
return Widget(**data)
def create_app(env=None):
from flask_restx import Api, Namespace, Resource
app = Flask(__name__)
api = Api(app)
@api.route("/restx/make_a_widget")
class WidgetResource(Resource):
@accepts(schema=WidgetSchema, api=api)
@responds(schema=WidgetSchema, api=api)
def post(self):
from flask import jsonify
return request.parsed_obj
return app
app = create_app()
if __name__ == "__main__":
app.run(debug=True)