# Copyright (c) 2024 CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com
from pydantic import BaseModel, ConfigDict, ValidationError

from czc.api.v2.exceptions import BadRequestError


def _humanize_pydantic_errors(error: ValidationError) -> str:
    return '\n'.join(
        f"Field '{' -> '.join(str(loc) for loc in err.get('loc', []))}': {err.get('msg', 'Unknown error')}"
        for err in error.errors()
    )


class CustomBaseModel(BaseModel):
    def __init__(self, **data):
        try:
            super().__init__(**data)
        except ValidationError as e:
            raise BadRequestError(_humanize_pydantic_errors(e)) from e

    model_config = ConfigDict(extra='ignore')
