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.

Schema validators can only report a single issue.

See original GitHub issue

Some times surfacing multiple validation issues with the data is necessary. For example in this case (implementation of pairwise is omitted):

from marshmallow import Schema, fields, validates_schema, ValidationError

class NumberSchema(Schema):
    number = fields.Integer()

class ListSchema(Schema):
    data = fields.Nested(NumberSchema, many=True)

    @validates_schema(pass_many=True)
    def validate_numbers(self, data, many):
        if not many:
            return
        for i, (n1, n2) in enumerate(pairwise(data)):
            if n1 >= n2:
                raise ValidationError('this number must be smaller than the previous one', '{}.number'.format(i+1))

if the list of numbers has multiple issues, only the first encountered issue is reported. This is sometimes not ideal for user interfaces (the user would appreciate all issues displayed at once).

I would suggest to allow validators return a list of ValidationError to indicate multiple issues.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
nickw444commented, Oct 12, 2016

After reading this discussion, I set out to try solve this myself. I implemented a POC function

def merge_schema_errors(errors):
    if errors and errors['_schema']:
        schema_errors = errors['_schema']
        del errors['_schema']

        for f in schema_errors:
            for field, field_errs in f.items():
                if field not in errors:
                    errors[field] = []

                errors[field].extend(field_errs)

    return errors

Which did what I wanted it to, provided I called ValidationError like this:

raise ValidationError({'fieldName': ['Error message here']})

This has a terrible interface, but my use case required it…

But, whilst finishing off writing my validation errors, I noticed the docstring for ValidationError specifies that you can actually provide a tuple or list of field of where to apply the message specified as the first argument of ValidationError

So without any extra code, I got this working by using

ValidationError('This is my validation error', field_names=['myField', 'myField2'])

I hope this clears things up for anyone looking here in the future.

1reaction
maximkulkincommented, Jul 29, 2016

@zhaostu This is a long struggle between me and library authors (See this issue and this pullrequest).

At my current work we needed support for this advanced validation, so I did table flip and now we use my fork of Marshmallow with all features WE need.

Also, I decided to start my own validation library - lollipop - pretty much similar to marshmallow but IMO does some things better. It still misses some planned features but already meets our needs, so I recommend giving it a try.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix Schema Validation Errors
The most common schema validation error is "either "offers", "review", or "aggregate rating" should be specified." Here's how to fix it.
Read more >
How do I avoid Schema Validation Errors in my e-filed returns ...
A schema validation error is an error in the required formatting of the electronic file established by the taxing authority which can be...
Read more >
Understanding schema errors | HESA
Schema errors prevent the validation being run in full because the file cannot be read. This means that errors cannot be traced to...
Read more >
Validate() behavior not consistent with documetnation
Problem : The mongo documentation explains schema validation and the ... The output of the validate function would then theoretically report ...
Read more >
Cannot validate recursive schema - json - Stack Overflow
There are a few issues, so let's walk through deriving this schema one step at a time. Let's start with the object representation...
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