-
-
Notifications
You must be signed in to change notification settings - Fork 629
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
RFC: Remove fields
attribute from ValidationError
#840
Comments
field_name
and fields
attributes from ValidationErrorfields
attribute from ValidationError
Actually, we should keep I've modified proposal to only remove |
@sloria I use from flask import Flask, Response
from flask_apispec import use_kwargs
from marshmallow import fields
app = Flask(__name__)
@app.errorhandler(422)
def handle_422(error):
errors = []
for field in error.exc.fields:
error_msg = f'Invalid field {field.name}'
location = field.metadata.get('location')
if location:
error_msg += f' at {location}'
errors.append(error_msg)
return Response('\n'.join(errors))
@app.route('/')
@use_kwargs({'x': fields.Int(location='query')})
def index(x):
return x Here we have global Flask error handler for 422 error and abstract kwargs schema. There can be a lot of schemes across project and the handler should be universal and return information from invalid fields metadata. So I can't instantiate these schemes, pass them to the handler and then get metadata from propagated instance. If you don't want to keep field instances, maybe it worth to keep schema the error relates to ( |
@decaz I think your use case can (should?) be solved by a change in webargs. We could attach the schema to the HTTPException. This could look something like: @app.errorhandler(422)
def handle_422(error):
errors = []
schema = error.data['schema']
for field_name in error.exc.field_names:
field = schema.fields[field_name]
error_msg = f'Invalid field {field.name}'
location = field.metadata.get('location')
if location:
error_msg += f' at {location}'
errors.append(error_msg)
return Response('\n'.join(errors)) |
@sloria I guess Unfortunately |
@decaz In the code example, Note: There's not yet a way to achieve the above without a change to webargs. I was just posting it for feedback. |
@sloria ok, I understand and agree - |
@decaz If you'd like, feel free to send a PR to webargs implementing this. If you don't have time now, I'll try to find some time over the weekend. |
* @error_handler and handle_error receive schema instance * In Flask, the Schema instance is attached to HTTPExceptions and is accessible from error.data["schema"] See marshmallow-code/marshmallow#840 (comment)
@decaz The above change is released in webargs 4.0.0. |
@sloria great, thanks! When will it be released at PyPI? |
@decaz It's released now. |
Bumps [webargs](https://github.com/sloria/webargs) from 3.0.2 to 4.0.0. <details> <summary>Changelog</summary> *Sourced from [webargs's changelog](https://github.com/sloria/webargs/blob/dev/CHANGELOG.rst).* > 4.0.0 (2018-07-15) > ****************** > > Features: > > * *Backwards-incompatible*: Custom error handlers receive the > `marshmallow.Schema` instance as the third argument. Update any > functions decorated with `Parser.error_handler` to take a ``schema`` > argument, like so: > > .. code-block:: python > > # 3.x > [**parser**](https://github.com/parser).error_handler > def handle_error(error, req): > raise CustomError(error.messages) > > > # 4.x > [**parser**](https://github.com/parser).error_handler > def handle_error(error, req, schema): > raise CustomError(error.messages) > > > See `marshmallow-code/marshmallow#840 (comment) <https://github-redirect.dependabot.com/marshmallow-code/marshmallow/issues/840#issuecomment-403481686>`_ > for more information about this change. > > Bug fixes: > > * *Backwards-incompatible*: Rename ``webargs.async`` to > ``webargs.asyncparser`` to fix compatibility with Python 3.7 > (:issue:`240`). Thanks :user:`Reskov` for the catch and patch. > > > Other changes: > > * *Backwards-incompatible*: Drop support for Python 3.4 (:pr:`243`). Python 2.7 and > >=3.5 are supported. > * *Backwards-incompatible*: Drop support for marshmallow<2.15.0. > marshmallow>=2.15.0 and >=3.0.0b12 are officially supported. > * Use `black <https://github.com/ambv/black>`_ with `pre-commit <https://pre-commit.com/>`_ > for code formatting (:pr:`244`). </details> <details> <summary>Commits</summary> - [`a864099`](marshmallow-code/webargs@a864099) Bump version and update changelog - [`90b822f`](marshmallow-code/webargs@90b822f) Merge pull request [#250](https://github-redirect.dependabot.com/sloria/webargs/issues/250) from sloria/flask-attach-schema-to-error - [`3ead80a`](marshmallow-code/webargs@3ead80a) Pass schema instance to error handlers - [`ee5ee8c`](marshmallow-code/webargs@ee5ee8c) Use :doc: instead of :ref: to refer to pages... - [`a721931`](marshmallow-code/webargs@a721931) Merge pull request [#249](https://github-redirect.dependabot.com/sloria/webargs/issues/249) from sloria/pyup-update-sphinx-issues-0.4.0-to-1.0.0 - [`c432c2c`](marshmallow-code/webargs@c432c2c) Update sphinx-issues from 0.4.0 to 1.0.0 - [`dcd67a9`](marshmallow-code/webargs@dcd67a9) Merge pull request [#248](https://github-redirect.dependabot.com/sloria/webargs/issues/248) from sloria/pyup-update-tox-3.1.1-to-3.1.2 - [`802c434`](marshmallow-code/webargs@802c434) Merge pull request [#247](https://github-redirect.dependabot.com/sloria/webargs/issues/247) from sloria/pyup-update-invoke-1.0.0-to-1.1.0 - [`6c946cc`](marshmallow-code/webargs@6c946cc) Update tox from 3.1.1 to 3.1.2 - [`1c54e1b`](marshmallow-code/webargs@1c54e1b) Update invoke from 1.0.0 to 1.1.0 - Additional commits viewable in [compare view](marshmallow-code/webargs@3.0.2...4.0.0) </details> <br /> [![Dependabot compatibility score](https://api.dependabot.com/badges/compatibility_score?dependency-name=webargs&package-manager=pip&previous-version=3.0.2&new-version=4.0.0)](https://dependabot.com/compatibility-score.html?dependency-name=webargs&package-manager=pip&previous-version=3.0.2&new-version=4.0.0) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot ignore this [patch|minor|major] version` will close this PR and stop Dependabot creating any more for this minor/major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) - `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language - `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language - `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language - `@dependabot badge me` will comment on this PR with code to add a "Dependabot enabled" badge to your readme Additionally, you can set the following in your Dependabot [dashboard](https://app.dependabot.com): - Update frequency (including time of day and day of week) - Automerge options (never/patch/minor, and dev/runtime dependencies) - Pull request limits (per update run and/or open at any time) - Out-of-range updates (receive only lockfile updates, if desired) - Security updates (receive only security updates, if desired) Finally, you can contact us by mentioning @dependabot. </details>
Bumps [webargs](https://github.com/sloria/webargs) from 1.9.0 to 4.1.1. <details> <summary>Changelog</summary> *Sourced from [webargs's changelog](https://github.com/sloria/webargs/blob/dev/CHANGELOG.rst).* > 4.1.1 (2018-10-25) > ****************** > > Bug fixes: > > * Fix bug in ``AIOHTTPParser`` that caused a ``JSONDecode`` error > when parsing empty payloads (:issue:`229`). Thanks :user:`explosic4` > for reporting and thanks user :user:`kochab` for the PR. > > 4.1.0 (2018-09-17) > ****************** > > Features: > > * Add ``webargs.testing`` module, which exposes ``CommonTestCase`` > to third-party parser libraries (see comments in :pr:`287`). > > 4.0.0 (2018-07-15) > ****************** > > Features: > > * *Backwards-incompatible*: Custom error handlers receive the > `marshmallow.Schema` instance as the third argument. Update any > functions decorated with `Parser.error_handler` to take a ``schema`` > argument, like so: > > .. code-block:: python > > # 3.x > [**parser**](https://github.com/parser).error_handler > def handle_error(error, req): > raise CustomError(error.messages) > > > # 4.x > [**parser**](https://github.com/parser).error_handler > def handle_error(error, req, schema): > raise CustomError(error.messages) > > > See `marshmallow-code/marshmallow#840 (comment) <https://github-redirect.dependabot.com/marshmallow-code/marshmallow/issues/840#issuecomment-403481686>`_ > for more information about this change. > > Bug fixes: > > * *Backwards-incompatible*: Rename ``webargs.async`` to > ``webargs.asyncparser`` to fix compatibility with Python 3.7 > (:issue:`240`). Thanks :user:`Reskov` for the catch and patch. > ></table> ... (truncated) </details> <details> <summary>Commits</summary> - [`969f2fc`](marshmallow-code/webargs@969f2fc) Bump version and update changelog - [`db3c459`](marshmallow-code/webargs@db3c459) Update pre-commit hooks - [`80b6bc4`](marshmallow-code/webargs@80b6bc4) Update changelog and add kochab to authors - [`1019518`](marshmallow-code/webargs@1019518) Merge pull request [#297](https://github-redirect.dependabot.com/sloria/webargs/issues/297) from kochab/fix-229 - [`2c265a8`](marshmallow-code/webargs@2c265a8) Merge pull request [#310](https://github-redirect.dependabot.com/sloria/webargs/issues/310) from sloria/dependabot/pip/pre-commit-1.12.0 - [`2cb4ddd`](marshmallow-code/webargs@2cb4ddd) Bump pre-commit from 1.11.2 to 1.12.0 - [`7454c35`](marshmallow-code/webargs@7454c35) test_aiohttpparser: Add a test to ensure correct behaviour on empty JSON bodies - [`8327796`](marshmallow-code/webargs@8327796) Bump pytest from 3.9.1 to 3.9.2 - [`5d8fbe7`](marshmallow-code/webargs@5d8fbe7) Merge pull request [#306](https://github-redirect.dependabot.com/sloria/webargs/issues/306) from sloria/dependabot/pip/pytest-3.9.1 - [`b0e55a4`](marshmallow-code/webargs@b0e55a4) Bump pytest from 3.8.2 to 3.9.1 - Additional commits viewable in [compare view](marshmallow-code/webargs@1.9.0...4.1.1) </details> <br /> [![Dependabot compatibility score](https://api.dependabot.com/badges/compatibility_score?dependency-name=webargs&package-manager=pip&previous-version=1.9.0&new-version=4.1.1)](https://dependabot.com/compatibility-score.html?dependency-name=webargs&package-manager=pip&previous-version=1.9.0&new-version=4.1.1) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot ignore this [patch|minor|major] version` will close this PR and stop Dependabot creating any more for this minor/major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) - `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language - `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language - `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language - `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language - `@dependabot badge me` will comment on this PR with code to add a "Dependabot enabled" badge to your readme Additionally, you can set the following in your Dependabot [dashboard](https://app.dependabot.com): - Update frequency (including time of day and day of week) - Automerge options (never/patch/minor, and dev/runtime dependencies) - Pull request limits (per update run and/or open at any time) - Out-of-range updates (receive only lockfile updates, if desired) - Security updates (receive only security updates, if desired) Finally, you can contact us by mentioning @dependabot. </details>
Bumps [webargs](https://github.com/sloria/webargs) from 3.0.2 to 4.0.0. <details> <summary>Changelog</summary> *Sourced from [webargs's changelog](https://github.com/sloria/webargs/blob/dev/CHANGELOG.rst).* > 4.0.0 (2018-07-15) > ****************** > > Features: > > * *Backwards-incompatible*: Custom error handlers receive the > `marshmallow.Schema` instance as the third argument. Update any > functions decorated with `Parser.error_handler` to take a ``schema`` > argument, like so: > > .. code-block:: python > > # 3.x > [**parser**](https://github.com/parser).error_handler > def handle_error(error, req): > raise CustomError(error.messages) > > > # 4.x > [**parser**](https://github.com/parser).error_handler > def handle_error(error, req, schema): > raise CustomError(error.messages) > > > See `marshmallow-code/marshmallow#840 (comment) <https://github-redirect.dependabot.com/marshmallow-code/marshmallow/issues/840#issuecomment-403481686>`_ > for more information about this change. > > Bug fixes: > > * *Backwards-incompatible*: Rename ``webargs.async`` to > ``webargs.asyncparser`` to fix compatibility with Python 3.7 > (:issue:`240`). Thanks :user:`Reskov` for the catch and patch. > > > Other changes: > > * *Backwards-incompatible*: Drop support for Python 3.4 (:pr:`243`). Python 2.7 and > >=3.5 are supported. > * *Backwards-incompatible*: Drop support for marshmallow<2.15.0. > marshmallow>=2.15.0 and >=3.0.0b12 are officially supported. > * Use `black <https://github.com/ambv/black>`_ with `pre-commit <https://pre-commit.com/>`_ > for code formatting (:pr:`244`). </details> <details> <summary>Commits</summary> - [`a864099`](marshmallow-code/webargs@a864099) Bump version and update changelog - [`90b822f`](marshmallow-code/webargs@90b822f) Merge pull request [gae-init#250](https://github-redirect.dependabot.com/sloria/webargs/issues/250) from sloria/flask-attach-schema-to-error - [`3ead80a`](marshmallow-code/webargs@3ead80a) Pass schema instance to error handlers - [`ee5ee8c`](marshmallow-code/webargs@ee5ee8c) Use :doc: instead of :ref: to refer to pages... - [`a721931`](marshmallow-code/webargs@a721931) Merge pull request [gae-init#249](https://github-redirect.dependabot.com/sloria/webargs/issues/249) from sloria/pyup-update-sphinx-issues-0.4.0-to-1.0.0 - [`c432c2c`](marshmallow-code/webargs@c432c2c) Update sphinx-issues from 0.4.0 to 1.0.0 - [`dcd67a9`](marshmallow-code/webargs@dcd67a9) Merge pull request [gae-init#248](https://github-redirect.dependabot.com/sloria/webargs/issues/248) from sloria/pyup-update-tox-3.1.1-to-3.1.2 - [`802c434`](marshmallow-code/webargs@802c434) Merge pull request [gae-init#247](https://github-redirect.dependabot.com/sloria/webargs/issues/247) from sloria/pyup-update-invoke-1.0.0-to-1.1.0 - [`6c946cc`](marshmallow-code/webargs@6c946cc) Update tox from 3.1.1 to 3.1.2 - [`1c54e1b`](marshmallow-code/webargs@1c54e1b) Update invoke from 1.0.0 to 1.1.0 - Additional commits viewable in [compare view](marshmallow-code/webargs@3.0.2...4.0.0) </details> <br /> [![Dependabot compatibility score](https://api.dependabot.com/badges/compatibility_score?dependency-name=webargs&package-manager=pip&previous-version=3.0.2&new-version=4.0.0)](https://dependabot.com/compatibility-score.html?dependency-name=webargs&package-manager=pip&previous-version=3.0.2&new-version=4.0.0) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot ignore this [patch|minor|major] version` will close this PR and stop Dependabot creating any more for this minor/major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) - `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language - `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language - `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language - `@dependabot badge me` will comment on this PR with code to add a "Dependabot enabled" badge to your readme Additionally, you can set the following in your Dependabot [dashboard](https://app.dependabot.com): - Update frequency (including time of day and day of week) - Automerge options (never/patch/minor, and dev/runtime dependencies) - Pull request limits (per update run and/or open at any time) - Out-of-range updates (receive only lockfile updates, if desired) - Security updates (receive only security updates, if desired) Finally, you can contact us by mentioning @dependabot. </details>
I'm afraid @decaz's use case is broken again. The latest error management rework removed I just realized from broken webargs tests that it also removes the possibility to store the names of faulty fields. The faulty inputs are still available in the messages, but the keys are not the field names if Note that the example provided above was a bit wrong IMHO because it returned field names from object space, not client space (it ignored From the schema and the error messages containing client space names, it should be possible to find the fields, but this requires a bit of trickery. For each key, you'll need to find the field that has the same name or |
Good catch, @lafrech . I'll get your webargs patch merged and released. Once that's in, I think @decaz 's use case can be met with something like from flask import Flask, jsonify
from webargs.flaskparser import use_kwargs
from marshmallow import fields
app = Flask(__name__)
@app.errorhandler(422)
def handle_422(error):
errors = []
schema = error.data['schema']
client_keys_to_fields = {
field.data_key or field_name: field
for field_name, field in schema.fields.items()
if not field.dump_only
}
for field_name in error.exc.messages:
field = client_keys_to_fields[field_name]
error_msg = f'Invalid field {field.name}'
location = field.metadata.get('location')
if location:
error_msg += f' at {location}'
errors.append(error_msg)
return jsonify(errors)
@app.route('/')
@use_kwargs({'x': fields.Int(location='query')})
def index(x):
return jsonify(x)
if __name__ == "__main__":
app.debug = True
app.run() Edit: clean up |
Currently,
ValidationError
stores the field name and field instances associated with the error.I propose removing the
fields
because it unnecessarily complicates the validation logic.fields
andfield_names
were added at a time (version 1.1.0 😓 ) when it was impossible to access all the error messages for a schema (see #63). It wasn't until 2.0 that it became possible to get the full error messages dict when using strict mode.At this point, I don't see a use case for accessing
ValidationError.fields
.The text was updated successfully, but these errors were encountered: