Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pydantic validators #26

Open
KristofferSkare opened this issue Mar 12, 2024 · 0 comments
Open

Add pydantic validators #26

KristofferSkare opened this issue Mar 12, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@KristofferSkare
Copy link
Collaborator

Using pydanitc classes for the json schema as we are doing it is possible to write custom validators for the classes to ensure that the data is valid when used in the code later.

Examples where this can be useful is to ensure that all the ranges in agent_output_indexes are valid ranges (either an integer or integers with ":" in between).

This can be added by using the @validator decorator. This can be done as shown below:

class InternalState(BaseModelConfig):
...
    @validator("name", "start_value", "initialization_variable")
    def check_only_one_initialization(self, v: Any, values: Dict[str, Any]):
        if "initialization_variable" in values and ("start_value" in values or "name" in values):
            raise ValueError(
                "Only one state initialization method is allowed to be used at a time: initialization_variable cannot be set if either start_value or name is set."
            )
        if "start_value" not in values and "name" in values:
            raise ValueError(
                "name is set without start_value being set. Both fields needs to be set for the state initialization to be valid"
            )
        if "start_value" in values and "name" not in values:
            raise ValueError(
                "start_value is set without name being set. Both fields needs to be set for the state initialization to be valid"
            )
        return v

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant