wag-managment-api-service-v.../ApiLayers/ApiValidations/handler.py

59 lines
1.8 KiB
Python

"""
Base validation models and utilities.
"""
from pydantic import BaseModel, ConfigDict
def rewrite_input_data(data):
"""Remove empty and None values from input data."""
return {
item[0]: item[1]
for item in data.items()
if not item[1] == "" and item[1] is not None
}
class BaseModelRegular(BaseModel):
"""Base model for all validation models with proper schema handling."""
model_config = ConfigDict(
json_schema_extra={"example": {}} # Will be populated by subclasses
)
def __init__(self, **kwargs):
super().__init__(**rewrite_input_data(kwargs))
def excluded_dump(self):
return self.model_dump(exclude_unset=True, exclude_none=True)
def dump(self):
return self.model_dump()
@classmethod
def model_json_schema(cls, *args, **kwargs):
"""Generate JSON schema with proper examples."""
schema = super().model_json_schema(*args, **kwargs)
# Add examples based on field types
if "properties" in schema:
example = {}
for field_name, field_schema in schema["properties"].items():
field_type = field_schema.get("type")
if field_type == "string":
example[field_name] = f"example_{field_name}"
elif field_type == "integer":
example[field_name] = 0
elif field_type == "number":
example[field_name] = 0.0
elif field_type == "boolean":
example[field_name] = False
elif field_type == "array":
example[field_name] = []
elif field_type == "object":
example[field_name] = {}
schema["example"] = example
return schema