How to use the pydantic.validator function in pydantic

To help you get started, we’ve selected a few pydantic examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github samuelcolvin / pydantic / tests / test_edge_cases.py View on Github external
        @validator('something')
        def check_something(cls, v):
            val_calls.append(v)
            return v
github checktheroads / hyperglass / hyperglass / configuration / models / routers.py View on Github external
    @validator("structured_output", pre=True, always=True)
    def validate_structured_output(cls, value, values):
        """Validate structured output is supported on the device & set a default.

        Raises:
            ConfigError: Raised if true on a device that doesn't support structured output.

        Returns:
            {bool} -- True if hyperglass should return structured output for this device.
        """
        if value is True and values["nos"] not in SUPPORTED_STRUCTURED_OUTPUT:
            raise ConfigError(
                "The 'structured_output' field is set to 'true' on device '{d}' with "
                + "NOS '{n}', which does not support structured output",
                d=values["name"],
                n=values["nos"],
            )
github checktheroads / hyperglass / hyperglass / parsing / models / juniper.py View on Github external
    @validator("validation_state", pre=True, always=True)
    def validate_rpki_state(cls, value):
        """Convert string RPKI state to standard integer mapping."""
        return RPKI_STATE_MAP.get(value, 3)
github MolSSI / QCFractal / qcfractal / interface / models / torsiondrive.py View on Github external
    @validator("initial_molecule", pre=True)
    def check_initial_molecules(cls, v):
        if isinstance(v, (str, dict, Molecule)):
            v = [v]
        return v
github kinecosystem / kin-python-bootstrap / bootstrap / src / requets_models.py View on Github external
    @validator('amount')
    def validate_amount(cls, value):
        if value > 0:
            return value
        raise errors.InvalidParamError('Amount for payment must be bigger than 0')
github checktheroads / hyperglass / hyperglass / configuration / models.py View on Github external
    @validator("credential", "proxy", "location")
    def clean_name(cls, v):  # noqa: N805
        """Remove or replace unsupported characters from field values"""
        return clean_name(v)
github samuelcolvin / harrier / harrier / config.py View on Github external
    @validator('default_template')
    def theme_templates(cls, v, values, **kwargs):
        templates_dir = values['theme_dir'] / 'templates'
        if not v or templates_dir.is_dir():
            return v
        else:
            raise ValueError(f'default-template set but template directory "{templates_dir}" does not exist')
github checktheroads / hyperglass / hyperglass / configuration / models / params.py View on Github external
    @validator("site_description")
    def validate_site_description(cls, value, values):
        """Format the site descripion with the org_name field.

        Arguments:
            value {str} -- site_description
            values {str} -- Values before site_description

        Returns:
            {str} -- Formatted description
        """
        return value.format(org_name=values["org_name"])
github MolSSI / QCFractal / qcfractal / interface / models / common_models.py View on Github external
    @validator("method")
    def _check_method(cls, v):
        return v.lower()
github MolSSI / QCFractal / qcfractal / interface / models / gridoptimization.py View on Github external
    @validator("steps")
    def check_steps(cls, v):
        if not (all(x < y for x, y in zip(v, v[1:])) or all(x > y for x, y in zip(v, v[1:]))):
            raise ValueError("Steps are not strictly monotonically increasing or decreasing.")

        v = recursive_normalizer(v)

        return v