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

Extending custom validations #100

Closed
Sult opened this issue Jul 29, 2020 · 2 comments · Fixed by #142
Closed

Extending custom validations #100

Sult opened this issue Jul 29, 2020 · 2 comments · Fixed by #142

Comments

@Sult
Copy link

Sult commented Jul 29, 2020

Hello,

I cant seem to figure out how to extend Validations with custom validations.
Whenever I raise something, i (default?) error.

Example (for someplicity)

@operations.register
async def register_user(request: web.Request) -> web.Response:
    with openapi_context(request) as context:
        # something in context fails on more detailed validation
        raise ValidationError.from_request_errors([OpenAPIParameterError("Something is wrong"), ], base_loc=["fieldname"])

I end up with this error:
{"detail": [{"loc": ["response", 0], "message": "'detail' is not of type object"}]}

I assume that whenever i use with openapi_context(request) as context: i get into the reponse layer.
Sadly i cant figure out how to combine it with my extra validations

playpauseandstop added a commit that referenced this issue Aug 6, 2020
And do not include hashes into docs requirements, as they are not
installable in Read The Docs :(

Issue: #100
@playpauseandstop
Copy link
Owner

Hi,

There is a ValidationError.from_dict class method, which is designed to simplify supplying custom validation errors for end user.

async def add_comment(request: web.Request) -> web.Response:
    """Add comment, but only if text do not contain stop words"""
    # Get validated data from request, this is a shortcut to,
    #
    # .. code-block:: python
    #
    #     context = get_openapi_context(request)
    #     data = context.data 
    #
    # In that moment request data **already** validated via OpenAPI Schema
    data = get_validated_data(request)
    
    # Now call custom validator to check whether comment text contains stop words
    if has_stop_words(data["text"]):
        # If text contain stop words - do not allow user add a comment
        raise ValidationError.from_dict(body={"text": "Please use kind language"})

    ...

I just updated docs to contain at least ValidationError API docs and will provide more docs & examples later.


As of openapi_context, this is just a syntax sugar for getting valid request data, you can use it as context manager, but if you don't prefer extra indent layer, you can use get_openapi_context function instead.


Hope you find my answers helpful, if not I will be happy to provide more information on the topic.

@Sult
Copy link
Author

Sult commented Aug 7, 2020

Thanks a lot!.
nice package btw 👍

playpauseandstop added a commit that referenced this issue Jan 5, 2021
Introduce `validation_error_context` to allow reuse validators and
raising validation errors within `aiohttp.web` applications.

Also allow to combine (merge, add) `ValidationError` exceptions.

Finally provide documentation for raising custom errors from OpenAPI
handlers.

Fixes: #100
Fixes: #132
playpauseandstop added a commit that referenced this issue Jan 5, 2021
Introduce `validation_error_context` to allow reuse validators and
raising validation errors within `aiohttp.web` applications.

Also allow to combine (merge, add) `ValidationError` exceptions.

Finally provide documentation for raising custom errors from OpenAPI
handlers.

Fixes: #100
Fixes: #132
badabump-release-bot bot pushed a commit that referenced this issue Jan 5, 2021
Features:
---------

- [#133] (**openapi**) Allow to pass kwargs to validate email func (#135)
- [#100, #132] (**openapi**) Improve validation errors (#142)

Other:
------

- Update dev Python version to 3.9.1 (#136)
- (**deps-dev**) bump pytest from 6.1.2 to 6.2.1 (#131)
- (**deps-dev**) bump coverage from 5.3 to 5.3.1 (#130)
- (**deps**) bump actions/checkout from v2.3.3 to v2.3.4 (#141)
- (**deps**) bump actions/cache from v2.1.2 to v2.1.3 (#138)
- (**deps**) bump actions/setup-python from v2.1.4 to v2.2.1 (#139)
- (**deps**) bump peter-evans/create-pull-request from v3.4.1 to v3.6.0 (#140)
- (**deps**) bump tibdex/github-app-token from v1.1.0 to v1.3 (#137)
playpauseandstop added a commit that referenced this issue Jan 5, 2021
Features:
---------

- [#133] (**openapi**) Allow to pass kwargs to validate email func (#135)
- [#100, #132] (**openapi**) Improve validation errors (#142)

Other:
------

- Update dev Python version to 3.9.1 (#136)
- (**deps-dev**) bump pytest from 6.1.2 to 6.2.1 (#131)
- (**deps-dev**) bump coverage from 5.3 to 5.3.1 (#130)
- (**deps**) bump actions/checkout from v2.3.3 to v2.3.4 (#141)
- (**deps**) bump actions/cache from v2.1.2 to v2.1.3 (#138)
- (**deps**) bump actions/setup-python from v2.1.4 to v2.2.1 (#139)
- (**deps**) bump peter-evans/create-pull-request from v3.4.1 to v3.6.0 (#140)
- (**deps**) bump tibdex/github-app-token from v1.1.0 to v1.3 (#137)

Co-authored-by: playpauseandstop <playpauseandstop@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants