Concept Flow - List and set validation
Receive input data
Check if data is list or set
Yes
Validate each item type
Accept data
End
The flow checks if input is a list or set, then validates each item type, accepting or rejecting accordingly.
from fastapi import FastAPI from pydantic import BaseModel from typing import List, Set class DataModel(BaseModel): items_list: List[int] items_set: Set[str]
| Step | Input Data | Validation Check | Result | Action |
|---|---|---|---|---|
| 1 | {"items_list": [1, 2, 3], "items_set": ["a", "b"]} | Is items_list a list? Yes | Pass | Check each item is int |
| 2 | [1, 2, 3] | Are all items int? Yes | Pass | items_list valid |
| 3 | ["a", "b"] | Is items_set a set? No | Fail | Return validation error for items_set |
| 4 | ["a", "b"] | Are all items str? Yes | Pass | items_set valid |
| 5 | {"items_list": [1, "two", 3], "items_set": ["a", "b"]} | Is items_list a list? Yes | Pass | Check each item is int |
| 6 | [1, "two", 3] | Are all items int? No | Fail | Return validation error for items_list |
| 7 | {"items_list": [1, 2, 3], "items_set": ["a", 2]} | Is items_set a set? No | Fail | Return validation error for items_set |
| 8 | ["a", 2] | Are all items str? No | Fail | Return validation error for items_set |
| Variable | Start | After Step 2 | After Step 4 | After Step 6 | After Step 8 |
|---|---|---|---|---|---|
| items_list | None | [1, 2, 3] | [1, 2, 3] | [1, "two", 3] | [1, 2, 3] |
| items_set | None | ["a", "b"] | ["a", "b"] | ["a", "b"] | ["a", 2] |
FastAPI validates lists and sets by checking the container type first. Then it validates each item inside for the correct type. If any item fails, validation returns an error. Use List[type] for ordered collections, Set[type] for unique items. This ensures input data matches expected structure and types.