question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFC: Remove `fields` attribute from ValidationError

See original GitHub issue

Currently, ValidationError stores the field name and field instances associated with the error.

from marshmallow import Schema, fields

class MySchema(Schema):
    foo = fields.Int()

schema = MySchema()
try:
    schema.load({'foo': 'invalid'})
except Exception as error:
    print(error.fields)  # [<fields.Integer(...)>]

I propose removing the fields because it unnecessarily complicates the validation logic.

fields and field_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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
sloriacommented, Dec 2, 2018

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

1reaction
sloriacommented, Jul 16, 2018

@decaz It’s released now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Form fields — Django 4.1.4 documentation
ValidationError : ['This field is required.'] >>> f.clean(None) Traceback (most ... Set the Form.use_required_attribute attribute to False to disable it.
Read more >
removing field name from validation error message [duplicate]
in rails 3 i don't want to show field names in error messages. Anyone know how to do that ? validates_presence_of :title, :message...
Read more >
marshmallow - Read the Docs
marshmallow is an ORM/ODM/framework-agnostic library for converting complex datatypes, such as objects, to and from native Python datatypes.
Read more >
Model instance reference - Django documentation
This method will validate all fields on your model. The optional exclude argument lets you provide a set of field names to exclude...
Read more >
Validation - Laravel - The PHP Framework For Web Artisans
The field under validation will be excluded from the request data returned by the validate and validated methods if the anotherfield field is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found