Skip to content

Validators

Validator

Bases: BoundMethodWrapper[TOwner, [MongoQuery | None, TInsertOrUpdate], InsertUpdateConfig]

Validator method wrapper.

Validator methods receive an insert or update object, and execute some - potentially async - operations to make sure the inserted or updated data is valid.

Source code in fastapi_motor_oil/validator.py
class Validator(BoundMethodWrapper[TOwner, [MongoQuery | None, TInsertOrUpdate], InsertUpdateConfig]):
    """
    Validator method wrapper.

    Validator methods receive an insert or update object, and execute some - potentially
    async - operations to make sure the inserted or updated data is valid.
    """

    __slots__ = ()

    exception = ValidationError

validator(config='insert-update')

Service method decorator factory that converts the decorated method into a Validator instance.

Example:

class SVC(MongoService):
    @validator("update")
    def check_something(self, data: InsertData | CreateData) -> None:
        raise ValueError("Always fail.")

Parameters:

Name Type Description Default
config InsertUpdateConfig

Validatator config.

'insert-update'
Source code in fastapi_motor_oil/validator.py
def validator(
    config: InsertUpdateConfig = "insert-update",
) -> Callable[
    [Callable[[TOwner, MongoQuery | None, TInsertOrUpdate], Coroutine[None, None, None]]],
    "Validator[TOwner, TInsertOrUpdate]",
]:
    """
    Service method decorator factory that converts the decorated method into a `Validator` instance.

    Example:

    ```python
    class SVC(MongoService):
        @validator("update")
        def check_something(self, data: InsertData | CreateData) -> None:
            raise ValueError("Always fail.")
    ```

    Arguments:
        config: Validatator config.
    """

    def decorator(
        func: Callable[[TOwner, MongoQuery | None, TInsertOrUpdate], Coroutine[None, None, None]], /
    ) -> "Validator[TOwner, TInsertOrUpdate]":
        return Validator(func=func, config=config)

    return decorator