Skip to content

Decorators

Decorators to help validating resolvers input.

validate(model, input_kwarg='input')

Validate input data using the given pydantic model.

If a ValueError is raised during validation, the decorated resolver won't be called. Instead, the decorator will return a GraphQL response containing error messages from pydantic in the error field defined by the ERROR_FIELD setting.

Parameters:

Name Type Description Default
model Type[pydantic.main.BaseModel]

The pydantic model used to validate data

required
input_kwarg str

Name of the keyword argument that contains input data.

'input'
Source code in turbulette/validation/decorators.py
def validate(
    model: Type[BaseModel],
    input_kwarg: str = "input",
):
    """Validate input data using the given pydantic model.

    If a `ValueError` is raised during validation, the decorated resolver
    won't be called. Instead, the decorator will return a GraphQL response
    containing error messages from pydantic in the error field defined by
    the `ERROR_FIELD` setting.

    Args:
        model: The pydantic model used to validate data
        input_kwarg: Name of the keyword argument that contains input data.
    """

    def wrap(func: FunctionType):
        async def wrapped_func(obj, info, **kwargs) -> Union[FunctionType, dict]:
            try:
                kwargs[conf.settings.VALIDATION_KWARG_NAME] = model(
                    **kwargs[input_kwarg]
                ).dict()
                return await func(obj, info, **kwargs)
            except ValidationError as exception:
                return PydanticsValidationError(exception).dict()

        return wrapped_func

    return wrap

Last update: 2021-02-18