mirror of
https://github.com/kennethreitz/neon-api-python.git
synced 2026-06-05 22:50:18 +00:00
37 lines
1017 B
Python
37 lines
1017 B
Python
import inspect
|
|
from typing import List, Dict, Any, Union, Optional
|
|
from pydantic import BaseModel
|
|
|
|
|
|
def squash(obj):
|
|
new_obj = {}
|
|
for k, v in obj.items():
|
|
if isinstance(v, dict):
|
|
new_obj.update(squash(v))
|
|
else:
|
|
if v:
|
|
new_obj[k] = v
|
|
|
|
|
|
def validate_obj_model(parameter_name: str, model: BaseModel):
|
|
"""A decorator that validates the 'obj' argument against the specified model."""
|
|
|
|
def decorator(func):
|
|
def wrapper(*args, **kwargs):
|
|
# Get the 'obj' argument.
|
|
obj = kwargs.get(parameter_name)
|
|
|
|
# If the 'obj' argument is not provided, raise an exception.
|
|
if obj is None:
|
|
raise ValueError(f"Missing required argument '{parameter_name}'.")
|
|
|
|
# Validate the 'obj' argument against the specified model.
|
|
model.model_validate(obj)
|
|
|
|
# Call the wrapped function.
|
|
return func(*args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
return decorator
|