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.

Improve "required" validation; allow_none and allow_blank

See original GitHub issue

Currently, setting required=True on Field only returns an error if the field’s value is missing from the input data.

class UserSchema(Schema):
    name = fields.Str(required=True)

s = UserSchema()
s.validate({})  # {'name': ['Missing data for required field.']}
s.validate({'name': None})  #  No errors
s.validate({'name': ''})  # No errors

This may not meet the typical use case, where you want an error returned for None and the empty string (with the option to allow these values).

Solution

By default, return an error if None or '' are passed to a required field. Add allow_none and allow_blank options to allow these values to pass validation.

UserSchema().validate({'name': None})  #  {'name': ['Missing data for required field.']}

class LenientSchema(Schema):
    name = fields.Str(required=True, allow_none=True)

LenientSchema().validate({'name': None})  # No errors

Note: This idea was yoinked from Django Rest Framework. =)

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
deckar01commented, Dec 4, 2018

@loic001 It is as standard marshalling design to load unstructured data and dump structured data. The schema should be designed to match the data structure you want your application to work with. Any data that does not comply with that structure needs to be loaded. Any data that does comply with that structure can be dumped.

2reactions
sloriacommented, Jan 18, 2015

@vesauimonen An update on this issue: I’ve added the allow_none parameter to field classes, which makes validation of None consistent across fields.

With a slight modification of your code above, validation works as expected.

from marshmallow import fields, Schema

class User(object):
    def __init__(self, name, activated_at=None):
        self.name = name
        self.activated_at = activated_at


class UserSchema(Schema):
    name = fields.String(required=True)
    activated_at = fields.DateTime(allow_none=True)


user = User(name='John')

s = UserSchema()

serialized_result, errors = s.dump(user)
# no errors
assert errors == {}

deserialized_result, errors = s.load(serialized_result) 
# no errors
assert errors == {}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Marshmallow allow retaining none for a value, but not clearing ...
New users are validated against a marshmallow schema that disallows None. country = fields.String(validate=OneOf(COUNTRY_CODES), allow_none= ...
Read more >
Don't want TextField AllowBlank to display validation error on ...
When the page loads, the textbox is already in an invalid state because there are no text in it. However, from ExtJS's demo...
Read more >
Active Record Validations - Ruby on Rails Guides
Active Record ValidationsThis guide teaches you how to validate the state of objects before they go into the database using Active Record's validations...
Read more >
Validators — WTForms Documentation (2.3.x)
Validators¶. A validator simply takes an input, verifies it fulfills some criterion, such as a maximum length for a string and returns.
Read more >
DATA VALIDATION - DON'T ALLOW BLANK CELLS
Hello, I'm trying to create a data validation error message, ... I want the user to only be able to enter text within...
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