Improve "required" validation; allow_none and allow_blank
See original GitHub issueCurrently, 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:
- Created 9 years ago
- Comments:12 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.
@vesauimonen An update on this issue: I’ve added the
allow_none
parameter to field classes, which makes validation ofNone
consistent across fields.With a slight modification of your code above, validation works as expected.