class {{ schema_runtime_validation_base_class_name }}(BaseModel):
{%- if has_pattern_properties %}
    __json_schema_pattern_properties__: ClassVar[tuple[Any, ...]] = ()
{%- endif %}
{%- if has_required_groups %}
    __json_schema_one_of_required_groups__: ClassVar[tuple[Any, ...]] = ()
    __json_schema_any_of_required_groups__: ClassVar[tuple[Any, ...]] = ()
{%- endif %}
{%- if has_conditional_required %}
    __json_schema_conditional_required__: ClassVar[tuple[Any, ...]] = ()
{%- endif %}

    @model_validator(mode='before')
    @classmethod
    def _validate_json_schema_runtime_rules(cls, data: Any) -> Any:
{%- if has_pattern_properties %}
        data = cls._validate_json_schema_pattern_properties(data)
{%- endif %}
{%- if has_required_groups %}
        data = cls._validate_json_schema_required_groups(
            data,
            required_group_rules=cls.__json_schema_one_of_required_groups__,
            require_exactly_one=True,
        )
        data = cls._validate_json_schema_required_groups(
            data,
            required_group_rules=cls.__json_schema_any_of_required_groups__,
            require_exactly_one=False,
        )
{%- endif %}
{%- if has_conditional_required %}
        data = cls._validate_json_schema_conditional_required(data)
{%- endif %}
        return data
{%- if has_pattern_properties %}

    @classmethod
    def _validate_json_schema_pattern_properties(cls, data: Any) -> Any:
        if not isinstance(data, dict):
            return data
        values = dict(data)
        for rule in cls.__json_schema_pattern_properties__:
            for key, value in data.items():
                if key in rule['declared_properties']:
                    continue
                if any(
                    re.search(pattern, key) for pattern in rule['rejected_patterns']
                ):
                    raise ValueError(
                        f'Property {key!r} is not allowed by patternProperties'
                    )
                matched = False
                for pattern, value_type in rule['pattern_properties']:
                    if not re.search(pattern, key):
                        continue
                    matched = True
                    value = TypeAdapter(value_type).validate_python(value)
                if matched:
                    values[key] = value
                    continue
                if rule['additional_property_type'] is not None:
                    values[key] = TypeAdapter(
                        rule['additional_property_type']
                    ).validate_python(value)
                    continue
                if not rule['allow_unmatched']:
                    raise ValueError(f'Unexpected property {key!r}')
        return values
{%- endif %}
{%- if has_required_groups %}

    @classmethod
    def _validate_json_schema_required_groups(
        cls,
        data: Any,
        *,
        required_group_rules: tuple[Any, ...],
        require_exactly_one: bool,
    ) -> Any:
        if not required_group_rules or not isinstance(data, dict):
            return data
        for required_groups in required_group_rules:
            matches = sum(
                all(any(name in data for name in names) for names in group)
                for group in required_groups
            )
            if require_exactly_one:
                if matches != 1:
                    raise ValueError(
                        'Expected exactly one required property group to be present'
                    )
            elif matches == 0:
                raise ValueError(
                    'Expected at least one required property group to be present'
                )
        return data
{%- endif %}
{%- if has_conditional_required %}

    @classmethod
    def _validate_json_schema_conditional_required(cls, data: Any) -> Any:
        if not isinstance(data, dict):
            return data
        for rule in cls.__json_schema_conditional_required__:
            condition_matches = all(
                any(name in data and data[name] in expected for name in names)
                for names, expected in rule['condition']
            )
            required_groups = (
                rule['then_required_groups']
                if condition_matches
                else rule['else_required_groups']
            )
            for group in required_groups:
                if all(any(name in data for name in names) for names in group):
                    continue
                raise ValueError('Conditional required properties are missing')
        return data
{%- endif %}
{{ '\n\n' }}
