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

choices not validated after update #2805

Open
AlfaBetaBeta opened this issue Mar 15, 2024 · 0 comments
Open

choices not validated after update #2805

AlfaBetaBeta opened this issue Mar 15, 2024 · 0 comments

Comments

@AlfaBetaBeta
Copy link

Hi everyone, I was trying to figure out how to properly use choices in a given field when defining a schema and I'm not sure if I've stumbled into something or if I'm misunderstanding the intended behaviour.

Say I have something as simple as this:

class MockJob(Document):
    job_status = StringField(required=True, choices=['foo', 'bar'])

If I now create and save a valid document:

mock_document = MockJob(job_status='foo')
mock_document.save(validate=True)

all goes as expected, validation succeeds because 'foo' is one of the admissible choices and I can check in the database that the document is there with the job_status field populated.

Now the part that puzzles me. If I update the field to an invalid value:

mock_document.update(job_status='whatever')

then the update succeeds without any validation errors, and I can see in the database that the document has indeed changed to have an invalid status.

So my first question would be: I've noticed that when updating the document there is a call to the validate method of the field's subclass (in the example StringField.validate) but I'm wondering if it would be preferable to call BaseField._validate instead? It seems that this way it would call BaseField._validate_choices along the way?

But this leads to the second question. When manually enforcing this validation:

mock_document.validate()

no errors are raised either, and the reason is that mock_document._data.get('job_status') is still 'foo'. How come there is a mismatch in this field between the value as stored in mock_document and the value as stored in the database?

I hope the example makes sense, I've kept it as bare-bones as possible. Any help with this would be greatly appreciated!

PS: I am using mongoengine v0.27.0.

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