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.

Nested schema with a list field does not abide required

See original GitHub issue

Have a nested schema using a list field and trying to make it required. A normal field within the same nested schema will enforce required just fine. The documentation states the list field inherits the same kwargs.

Here’s a stripped down version:

class Links(Schema):
    states = fields.List(fields.String(), required=True) # Does not work
    region = fields.Integer(required=True) # Works fine

class Job(Schema):
    id = fields.Integer()
    links = fields.Nested(Links)

I’ve also tried putting the required kwarg at the list string field level too.

Hope I have not overlooked something. The more I dug the more it started to look like a bug. Thank you!

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

12reactions
rastikerdarcommented, Mar 12, 2015

If i understand the problem right:

from marshmallow import Schema, fields, validate

class Links(Schema):                                                        
        states = fields.List(fields.String(), required=True, validate=validate.Length(min=1))
        region = fields.Integer(required=True)

class Job(Schema):
    id = fields.Integer()
    links = fields.Nested(Links)

Job().load({                                                                                
    "links": {                                 
        "states": [],
        "region": 1
    }                                                          
})

# UnmarshalResult(data={'links': None}, errors={'links': {'states': [u'Shorter than minimum length 1.']}})
4reactions
sloriacommented, Mar 14, 2015

Thanks @rastikerdar – that is the right approach; We probably don’t need to introduce the allow_empty parameter, as you can achieve the same validation using validate.Length

not_empty = validate.Length(min=1, error='Field cannot be empty.')

states = fields.List(fields.Str(), required=True, validate=not_empty)

@morgan Does that meet your use case?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubles with validating nested values - Stack Overflow
And no need for many=True since you're putting the Nested field in a List field. Try this: class JsonSchema(BaseSchema): mode = fields.
Read more >
5 Basic Materialized Views - Database - Oracle Help Center
Before starting to define and use the various components of summary management, you should review your schema design to abide by the following...
Read more >
Google JavaScript Style Guide
1 Introduction. This document serves as the complete definition of Google's coding standards for source code in the JavaScript programming language.
Read more >
Common Mistakes Developers Make In Their RAML Files
Plenty of developers upload only their main RAML file but do not provide the other referenced files. In general, a ZIP is required...
Read more >
Valence
database error messages (such as a required field being missing, ... Not only are there nested lists of records (arrays), there are ......
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