You can download this code by clicking the button below.
This code is now available for download.
This function creates a Marshmallow Schema that is used for validating user data. The Schema includes basic user information fields and performs necessary validations on these fields.
Technology Stack : Python, Marshmallow
Code Type : Python Function
Code Difficulty : Intermediate
from marshmallow import Schema, fields, ValidationError, validates, ValidationError
def validate_length(data, field):
if len(data) < 5:
raise ValidationError("The field must be at least 5 characters long.")
def create_user_schema():
class UserSchema(Schema):
id = fields.Int(required=True)
name = fields.Str(required=True)
email = fields.Email(required=True)
age = fields.Int(required=True, validate=lambda x: x > 0)
bio = fields.Str(validate=validate_length)
return UserSchema()
# JSON Explanation