Skip to main content
友田 陽大

Category

marshmallow(Python シリアライズ/バリデーション)の実装ガイド

marshmallow は『Python オブジェクト ⇄ dict/JSON』の双方向変換を Schema として宣言し、信頼できない外部入力を load() で検証して堰き止め、内部の値を dump() で安全に整形する、ORM/フレームワーク非依存のシリアライズ/バリデーションライブラリです。本クラスタは、Schema/fields の基礎と境界設計(dump_only/load_only/unknown でマスアサインメントと機密漏洩を防ぐ)、業務ルールを持つ値を再利用可能な型にするカスタムフィールド、Flask×SQLAlchemy(marshmallow-sqlalchemy)での本番 REST API、スキーマ再利用・テスト・可観測性による本番品質化、marshmallow 3→4 の破壊的変更の安全な移行、そして Pydantic との使い分けまでを扱います。経済産業大臣賞を受賞した B2B SaaS のバックエンド(Flask/SQLAlchemy)で marshmallow を本番運用した知見を根拠に、型安全・セキュリティ・テスト容易性を担保した設計を、公式ドキュメントに忠実な実コードで体系化します。

6 articles in total

Foundational guide

Foundational guide (start here)

Python
marshmallow
シリアライズ
バリデーション
型安全

marshmallow Practical Guide: Robustly Designing Python Object Serialization / Validation at the Boundary (v4-Compatible)

Faithfully to the marshmallow official documentation (v4.3), explains from a practical standpoint: the bidirectional serialization of Schema/fields, boundary validation with load(), @validates/@validates_schema, Nested, the safe design of load_only/dump_only, marshmallow-sqlalchemy integration, the 3→4 migration, and how to choose between it and Pydantic.

18 min read

Related practical articles