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.

Remove allow_blank parameter from String fields?

See original GitHub issue

The allow_blank was added to address #76 . It is used to determine whether to allow the empty string as a valid value.

This parameter is technically redundant, since the same validation can be achieved using validate.Length.

from marshamallow import fields, validate
not_blank = validate.Length(min=1, error='Field cannot be blank')
f = fields.Str(validate=not_blank)
f.deserialize('')  # => Error: Field may not be blank.

I propose to remove the allow_blank parameter, as it is unnecessary API.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
martinsteincommented, Jun 22, 2015

There is a valid use case for an option to disallow empty strings:

When you want to show a special error message for missing values including empty strings:

name = fields.String(
    required="Please enter your user name.",
    validate=[
        validate.Length(min=4, max=20,
                           error="User name must have between {min} and {max} characters."),
        validate.Regexp(r"[a-zA-Z0-9_\-]*$",
                           error="User name must not contain special characters"
                                 "(except _ und -)")
    )

In that case, adding a Length(min=1) validator like this is problematic:

name = fields.String(
    required="Please enter your user name.",
    validate=[
        validate.Length(min=1, error="Please enter your user name."),
        validate.Length(min=4, max=20,
                           error="User name must have between {min} and {max} characters."),
        validate.Regexp(r"[a-zA-Z0-9_\-]*$",
                           error="User name must not contain special characters"
                                 "(except _ and -)")
    )

because an empty string would trigger two error messages (from both length validators):

# you would get 2 'name'-errors:
# 'name': ['Please enter your user name.', 'User name must have between 4 and 20 characters.']
1reaction
martinsteincommented, Jul 3, 2015

I’ve just had a similar problem with an Integer-field. How would you solve the task to show two different messages for these two cases:

  1. The user enters nothing, which means the browser will submit an empty string. That means you want to show a message like “Please enter a number.”
  2. The user enters something that is not a valid number, e.g. "12abc&$". You would like to show an error-message along the lines of “Only number-values are allowed.”

Currently I don’t see a nice way to distinguish missing input from invalid numerical input, because marshmallow treats the empty string as non-missing. This is not a problem for JSON-submitted data (e.g. from Angular, …) of course. But for standard browser forms that are POSTed, this will be relevant.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Not allow a blank character / space in a input form [duplicate]
Is it possible to not allow a blank character / space in a input form like below? <input type="text" value="" maxlength="30" name="email" ...
Read more >
Solved: Re: allowBlank on select in multifield (Touch UI) ... - Adobe ...
It turns out there is an issue with the way AEM Touch UI multifield components update values. If you remove the last item,...
Read more >
Parameters - GrapeCity
Allow Blank Value field : Select this check box if you want to allow blank values to be passed for the parameter. It...
Read more >
Allow blank value(empty String) to be saved for a Custom Label
Then go to the label and in the value input field of the label press Ctrl+V to paste the Zero Width Space. Click...
Read more >
AllowBlank="true" does not work - Ext.NET Forums
The text box where are thos Fields that has AllowBlank="true" become ... value of AllowBlank is "true", so it would be best to...
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 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