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

28 lines
694 B
Python

"""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
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