Skip to Content
Pythonによるアーキテクチャパターン
book

Pythonによるアーキテクチャパターン

by Harry Percival, Bob Gregory
March 2025
Intermediate to advanced
304 pages
4h 33m
Japanese
O'Reilly Media, Inc.
Content preview from Pythonによるアーキテクチャパターン

付録E. バリデーション

、これらのテクニックについて教えたり話したりしていると、何度も出てくる質問がある。それはドメインモデルのビジネスロジックに属するのか、それともインフラの問題なのか?

どんなアーキテクチャの質問でもそうだが、答えは「場合による」だ!

最も重要な考慮点は、システムの各部分がシンプルになるように、コードをうまく分離しておくことだ。無関係な細部でコードを乱雑にしたくないのだ。

そもそもバリデーションとは何なのか?

バリデーションという言葉を使う場合、通常は、ある演算子の入力が特定の基準に合致するかどうかをテストするプロセスを意味する。 基準に合致する入力は有効、合致しない入力は無効とみなされる。

入力が無効な場合、演算子は続行できず、何らかのエラーで終了しなければならない。言い換えれば、バリデーションとは前提条件を作成することである。前提条件をシンタックス、セマンティクス、プラグマティクスの3つのサブタイプに分けることが有効であることを発見した。

構文を検証する

言語学において構文とは、文法的な文の構造を支配する規則セットのことである。例えば英語では、「Allocate three units ofTASTELESS-LAMP to order twenty-seven」という文は文法的に正しいが、「hat hat hat hat wibble」というフレーズは文法的に正しくない。私たちは文法的に正しい文を、よく形成された文と表現することができる。

これは我々のアプリケーションにどのようにマッピングされるのだろうか?構文規則の例をいくつか挙げてみよう:

  • Allocate コマンドは、注文ID、SKU、数量を持たなければならない。

  • 整数は正の整数である。

  • SKUは文字列である。

これらは受信データの形状と構造に関する規則である。SKUやオーダーIDのないAllocateコマンドは有効なメッセージではない。これは "Allocate three to. "というフレーズと等価性である。

私たちは、システムのエッジでこれらの規則を検証する傾向がある。私たちの経験則では、メッセージ・ハンドラーは常に、整形式で必要な情報をすべて含むメッセージだけを受信すべきである。

一つの選択肢は、バリデーション・ロジックをメッセージ・タイプそのものに置くことだ:

メッセージクラスのバリデーション(src/allocation/commands.py)

from schema import And, Schema, Use


@dataclass
class Allocate(Command):

    _schema = Schema({  1
        'orderid': int,
         sku: str,
         qty: And(Use(int), lambda n: n > 0)
     }, ignore_extra_keys=True)

    orderid: str
    sku: str
    qty: int

    @classmethod
    def from_json(cls, data):  
       data = json.loads(data)
       return cls(**_schema.validate(data))

schema ライブラリーを

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

技術リーダーシップのための14のヒント

技術リーダーシップのための14のヒント

島田 浩二
Kubernetesで実践するクラウドネイティブDevOps

Kubernetesで実践するクラウドネイティブDevOps

John Arundel, Justin Domingus, 須田 一輝, 渡邉 了介
AWS上のシステム設計

AWS上のシステム設計

Jayanth Kumar, Mandeep Singh

Publisher Resources

ISBN: 9798341624863