fastapi-svelte-template/backend/todo/schemas/common.py

28 lines
694 B
Python
Raw Normal View History

2023-05-31 16:37:56 +01:00
"""This module contains common schemas and common functions for schema validation."""
from pydantic import BaseModel, validator
def is_valid_id(id: int) -> bool:
"""Checks whether the specified id is a valid ID.
Performs a shallow check on whether the ID is a valid primary key.
"""
if not isinstance(id, int):
raise TypeError(f"Expected an integer but got: {type(id)}")
return id > 0
2023-05-31 16:37:56 +01:00
class ItemCount(BaseModel):
"""A schema used to represent an item count."""
total: int
@validator('total')
def assert_total_is_valid(cls, total):
if total < 0:
raise ValueError("An item count cannot be negative.")
return total