Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InputRequired & DataRequired validators on same field #152

Open
0xbart opened this issue Sep 25, 2020 · 1 comment
Open

InputRequired & DataRequired validators on same field #152

0xbart opened this issue Sep 25, 2020 · 1 comment

Comments

@0xbart
Copy link

0xbart commented Sep 25, 2020

Hi there, I hope one of you can help me with the strange following question.

First of all, I've create a Model with SQLAlchemy:
(Note that the BaseModel is my BaseModel with a UUID included.

class Company(BaseModel):
    reseller = Column(UUIDType(), ForeignKey('company.uuid'))
    name = Column(VARCHAR(500), unique=True, nullable=False)

The next step is to create the actual Form.

class CompanyForm(ModelForm):
    class Meta:
        model = Company

After that, I wanna debug what kind of validators there are active on the name column:

c = CompanyForm()
print(c.name.validators)

# [<wtforms.validators.InputRequired object at 0x1068455e0>, <wtforms.validators.DataRequired object at 0x106845640>, <wtforms.validators.Length object at 0x1067bc130>, <wtforms_alchemy.validators.Unique object at 0x1067bc550>]

The next step is to call the form in the Rest API incoming response with the data given to the object. But InputRequired / DataRequired always validate to False.

# When using -D (curl) the DataRequired() is successfully, but the InputRequired failed
➜  ~ curl -XPUT -d '{"name": "bart"}' -H 'Content-Type: application/json' http://localhost:5000/api/v1/company/

# When using -F the InputRequired is sucessfully, but the dataRequired failed.
➜  ~ curl -XPUT -F "name=bart" http://localhost:5000/api/v1/company/                                           

Removing the nullable=False will fixed this problem. Is there someone who can help me? :-)

@0xbart
Copy link
Author

0xbart commented Sep 26, 2020

This works:

Create BaseForm and bypass InputRequired() from Meta class:

class BaseForm(ModelForm):
    class Meta:
        not_null_validator_type_map = ClassMap(
            [(String, [validators.DataRequired()])]
        )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant