{% include '_header.py.jinja' %}
# -- template partials.py.jinja --
from pydantic import BaseModel, Field, validator
from . import types, models, fields, enums


{% for name, fields in partial_models.items() %}
class {{ name }}(BaseModel):
    {% for field in fields.values() %}
    {{ field.name }}:
            {%- if field.optional -%}
                {{ ' ' }}Optional[{{ field.type }}]
            {% else -%}
                {{ ' ' }}{{ field.type }}
            {% endif %}
    {% if not field.documentation is none %}
    """{{ format_documentation(field.documentation) }}"""

    {% endif %}
    {% endfor %}

    Config = models.Config

    {% for field in fields.values() %}
    {% if field.is_list and not field.is_relational and not field.optional %}
    @validator('{{ field.name }}', pre=True, allow_reuse=True)
    @classmethod
    def _transform_required_{{ field.name }}_list(cls, value: object) -> object:
        # See the `_transform_required_list_fields()` function in `prisma/models.py` for context
        if value is None:
            return []

        return value
    {% endif %}
    {% endfor %}


{% endfor %}

# users can modify relational types which are then namespaced to partials.
# so we have to import ourselves in order to resolve forward references
from . import partials

{% for name, fields in partial_models.items() %}
{{ name }}.update_forward_refs()
{% endfor %}

# fmt: on

