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

Foreign key error message in WTForms validation #43

Open
AnastassiyaP opened this issue Jul 12, 2018 · 8 comments
Open

Foreign key error message in WTForms validation #43

AnastassiyaP opened this issue Jul 12, 2018 · 8 comments

Comments

@AnastassiyaP
Copy link

When I try to post a new object with foreign key field that doesn't match to any object in the database, I get the same message, as when I don't pass this field at all:

{
   "region": [
     "This field is required."
   ]
}

It looks like a bug.

@coleifer
Copy link
Owner

Shouldn't you ensure the object exists before assigning a value to a foreign-key field, though?

@AnastassiyaP
Copy link
Author

I am talking about validation of incoming POST data that of course should be checked in the frontend, but I cannot guarantee this.
I do a kind of REST API and I would like to send more comprehensive descriptions of errors to the frontend.

@coleifer
Copy link
Owner

I don't understand what you're asking for or what the problem is.

@AnastassiyaP
Copy link
Author

I am trying to make validator for incoming params using wtf-peewee

Supposing, I have this objects in database:

class Project(BaseModel):
    name = CharField()
    region = ForeignKeyField(column_name='region_id', field='id', model=Region)

class Region(BaseModel):
    name = CharField()

and I made a REST api interface to it

curl -H 'Content-Type: application/json' http://127.0.0.1:5000/api/v1/project/ -d '{"region_id":1,"name":"name"}'
{
  "id": 1,
  "name": "name",
  "region_id": 1,
}

Now I want to validate incoming params when creating project, so I added validator:

project_validator = model_form(Project, exclude = exclude)
form = self.validator(data)
if form.validate():
    return self.project_create()
return self.response_bad_request(form.errors)

And when I send wrong region_id that doesn't exist in database, form.errors contains:

curl -H 'Content-Type: application/json' http://127.0.0.1:5000/api/v1/project/ -d '{"region_id":10,"name":"name"}'

{
    "region_id": [
      "This field is required."
    ]
}

As well as when I doesn't send region_id at all:

curl -H 'Content-Type: application/json' http://127.0.0.1:5000/api/v1/project/ -d '{"name":"name"}'

{
    "region_id": [
      "This field is required."
    ]
}

The question is - can I somehow get error message something like "No region with id = 10" in case when region_id is passed, but object is not found?

Thanks!

@coleifer
Copy link
Owner

Not sure, the field should be raising the message "Not a valid choice".

@AnastassiyaP
Copy link
Author

It should, but for me it seems that exception raises when sql query does not succeed, on this row: https://github.com/coleifer/wtf-peewee/blob/master/wtfpeewee/fields.py#L264

I don't understand why, with exists() call it should return true or false.

@coleifer
Copy link
Owner

Is this the same as the issue you just opened, #46 ? Are they related?

@AnastassiyaP
Copy link
Author

They are related, but I am not sure that this is the same case.

In the case of required param with wrong foreign key validation fails as expected but the error message is incorrect.
In the case of an optional param, validation succeeds.

But maybe Exception on DoesNotExist in the get_model() will fix both cases, I don't know.

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

2 participants