python
48 lines · 8 steps
How a Flask blueprint validates and creates users
A Flask blueprint pairs a Marshmallow schema with a POST route to validate input, reject duplicates, and persist a new user.
Explained by
highlit
1from flask import Blueprint, request, jsonify
2from marshmallow import Schema, fields, validate, ValidationError, EXCLUDE
3
4from .models import db, User
5from .services import send_welcome_email
6
7bp = Blueprint("users", __name__, url_prefix="/api/users")
8
9
10class CreateUserSchema(Schema):
11 class Meta:
12 unknown = EXCLUDE
13
14 email = fields.Email(required=True)
15 username = fields.Str(
16 required=True,
17 validate=validate.Length(min=3, max=32),
18 )
19 password = fields.Str(
20 required=True,
21 load_only=True,
22 validate=validate.Length(min=8),
23 )
24 age = fields.Int(validate=validate.Range(min=13, max=120))
25
26
27create_user_schema = CreateUserSchema()
28
29
30@bp.post("")
31def create_user():
32 try:
33 data = create_user_schema.load(request.get_json(force=True))
34 except ValidationError as err:
35 return jsonify(errors=err.messages), 422
36
37 if User.query.filter_by(email=data["email"]).first():
38 return jsonify(errors={"email": ["already registered"]}), 409
39
40 user = User(email=data["email"], username=data["username"], age=data.get("age"))
41 user.set_password(data["password"])
42
43 db.session.add(user)
44 db.session.commit()
45
46 send_welcome_email.delay(user.id)
47
48 return jsonify(id=user.id, email=user.email, username=user.username), 201
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Validating request bodies with a schema keeps parsing, type coercion, and error handling out of your route logic.
- 2Checking for existing records before insert lets you return a meaningful conflict status instead of a database error.
- 3Offloading side effects like emails to a background task keeps the request path fast and responsive.
Related explainers
python
from fastapi import FastAPI, WebSocket, WebSocketDisconnect app = FastAPI()
Building a WebSocket chat with FastAPI
websockets
broadcast
connection-management
Intermediate
9 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
python
import time import uuid from django.utils.deprecation import MiddlewareMixin
Attaching per-request context in Django
middleware
request lifecycle
multi-tenancy
Intermediate
7 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
python
import random from typing import Iterator, List
How reservoir sampling picks k items
reservoir-sampling
streaming
randomness
Intermediate
5 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-a-flask-blueprint-validates-and-creates-users-explained-python-7386/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.