"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from __future__ import annotations
from novu_py.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
import pydantic
from pydantic import model_serializer
from typing import Any, Dict, List, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


Value5TypedDict = TypeAliasType(
    "Value5TypedDict", Union[str, float, bool, Dict[str, Any]]
)


Value5 = TypeAliasType("Value5", Union[str, float, bool, Dict[str, Any]])


class Value4TypedDict(TypedDict):
    pass


class Value4(BaseModel):
    pass


PayloadValidationErrorDtoValueTypedDict = TypeAliasType(
    "PayloadValidationErrorDtoValueTypedDict",
    Union[Value4TypedDict, str, float, bool, List[Nullable[Value5TypedDict]]],
)
r"""The actual value that failed validation"""


PayloadValidationErrorDtoValue = TypeAliasType(
    "PayloadValidationErrorDtoValue",
    Union[Value4, str, float, bool, List[Nullable[Value5]]],
)
r"""The actual value that failed validation"""


class PayloadValidationErrorDtoTypedDict(TypedDict):
    field: str
    r"""Field path that failed validation"""
    message: str
    r"""Validation error message"""
    value: NotRequired[Nullable[PayloadValidationErrorDtoValueTypedDict]]
    r"""The actual value that failed validation"""
    schema_path: NotRequired[str]
    r"""JSON Schema path where the validation failed"""


class PayloadValidationErrorDto(BaseModel):
    field: str
    r"""Field path that failed validation"""

    message: str
    r"""Validation error message"""

    value: OptionalNullable[PayloadValidationErrorDtoValue] = UNSET
    r"""The actual value that failed validation"""

    schema_path: Annotated[Optional[str], pydantic.Field(alias="schemaPath")] = None
    r"""JSON Schema path where the validation failed"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["value", "schemaPath"]
        nullable_fields = ["value"]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m
