Schemas in Flask
The whole request parser part of Flask-RESTful is slated for removal and will be replaced by documentation on how to integrate with other packages that do the input/output stuff better (such as marshmallow). This means that it will be maintained until 2.0 but consider it deprecated. Don’t worry, if you have code using that now and wish to continue doing so, it’s not going to go away any time soon.
In the Python Flask web framework, a "schema" is a way to define the structure and data types of the values that are stored in an object or a database. Schemas are used to validate the data and ensure that it conforms to the expected format and rules.
One popular library for working with schemas in Flask is Flask-RESTful, which provides a set of tools and utilities for building RESTful APIs. Flask-RESTful integrates with the Marshmallow library, which provides a powerful and easy-to-use schema framework for Flask.
In Flask-RESTful, a "schema" is defined as a Python class that inherits from the Marshmallow Schema
class. The schema class defines the structure and data types of the values that are stored in the object or database. For example, consider a schema for a simple user model that has a name
and email
field:
from flask_restful import fields
from marshmallow import Schema
class UserSchema(Schema):
name = fields.Str()
email = fields.Email()
The schema class uses the Flask-RESTful fields
module to specify the data types of the name
and email
fields. In this case, the name
the field is a string and the email
the field is an email address.
To validate an object against the schema, we can use the load
method of the schema class. The load
the method takes an object as input and returns a dictionary with the values of the object. If the object is invalid according to the schema, the load
the method will raise an error.
For example, consider an object that represents a user with a name
and email
field:
user = {
"name": "John Doe",
"email": "johndoe@example.com"
}
To validate this object against the user schema, we can use the load
method like this:
schema = UserSchema()
result = schema.load(user)
print(result) # Output: { "name": "John Doe", "email": "johndoe@example.com" }
The load
the method returns a dictionary with the values of the user object, indicating that the object is valid according to the schema.
In Flask-RESTful, schemas are often used in conjunction with resources, which are classes that represent a specific RESTful endpoint. For example, consider a Flask-RESTful resource that exposes a user object as a RESTful API:
from flask_restful import Resource, reqparse
from flask_restful import fields, marshal
class UserResource(Resource):
schema = UserSchema()
def get(self):
user = {
"name": "John Doe",
"email": "johndoe@example.com"
}
return marshal(user, UserResource.schema)
The UserResource
class defines a get
the method that returns a user object as a JSON response. Before returning the user object, the marshal
function of Flask-RESTful is used to validate the object against the UserSchema
schema. If the object is invalid, the marshal
function will raise an error.