File size: 1,320 Bytes
09da05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""Validation helpers for survey simulation models."""

from __future__ import annotations

from typing import Any, Iterable, List, Mapping

from .models import (
    PersonaConfig,
    RespondentGroup,
    RespondentProfile,
    SimulationResult,
    Survey,
    SurveyResponse,
)


def _validate_model(model_cls: Any, payload: Any) -> Any:
    if hasattr(model_cls, "model_validate"):
        return model_cls.model_validate(payload)
    return model_cls(**payload)


def validate_survey_definition(payload: Mapping[str, Any]) -> Survey:
    return _validate_model(Survey, dict(payload))


def validate_group_composition(composition: Iterable[Mapping[str, Any]]) -> List[PersonaConfig]:
    return [_validate_model(PersonaConfig, dict(item)) for item in composition]


def validate_respondent_profile(payload: Mapping[str, Any]) -> RespondentProfile:
    return _validate_model(RespondentProfile, dict(payload))


def validate_group(payload: Mapping[str, Any]) -> RespondentGroup:
    return _validate_model(RespondentGroup, dict(payload))


def validate_survey_response(payload: Mapping[str, Any]) -> SurveyResponse:
    return _validate_model(SurveyResponse, dict(payload))


def validate_simulation_result(payload: Mapping[str, Any]) -> SimulationResult:
    return _validate_model(SimulationResult, dict(payload))