Skip to content
Advertisement

Flask validates decorator multiple fields simultaneously

I have been using the @validates decorator in sqlalchemy.orm from flask to validate fields, and all has gone well as long as all of the fields are independent of one another such as:

@validates('field_one')
def validates_field_one(self, key, value):
   #field one validation

@validates('field_two')
def validates_field_two(self, key, value):
   #field two validation

However, now I need to do some validation that will require access to field_one and field_two simultaneously. It looks like validates accepts multiple arguments to the validates decorator, however, it will simply run the validation function once for each argument, as such:

@validates('field_one', 'field_two')
def validates_fields(self, keys, values):
   #field validation

Results in a work flow of validate field_one and then validate field_two. However, I would like to validate both at the same time(a trivial example of which would be assert that the value of field_one is not the value of field_two, an example of which would be disallowing self-loops in a graph where field_one and field_two refer to nodes and it is performing validation on an edge). How would be the best way to go about doing that?

Advertisement

Answer

Order the fields in the order they were defined on the model. Then check if the last field is the one being validated. Otherwise just return the value unchecked. If the validator is validating one of the earlier fields, some of them will not be set yet.

@validates('field_one', 'field_two')
def validates_fields(self, key, value):
    if key == 'field_two':
        assert self.field_one != value
    return value

See this example.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement