`choices` not validated after update
AlfaBetaBeta opened this issue · 0 comments
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
.