Fake-volatile validation_exceptions not working for multiple columns in count sqlalchemy models

I am using Flask-Restless to create / api / v1 / candidate. There I usedvalidation_exceptions=[MyValidationError]

# ... code snippet from my models.py ....

class MyValidationError(Exception):
    pass

def validate_required_field(method):
    def wrapper(self, key, string):
        if not string:
            exception = MyValidationError()
            exception.errors = {key: 'must not be empty'}
            raise exception
        return method(self, key, string)
    return wrapper

class Candidate(db.Model):

    __tablename__ = 'candidate'

    # ... snip ...
    first_name = db.Column(db.String(100), nullable=False)  
    phone = db.Column(db.String(20), nullable=False, unique=True)
    # ... snip ...

    @orm.validates('first_name')
    @validate_required_field
    def validate_first_name(self, key, string):
        return string

    @orm.validates('phone')
    @validate_required_field
    def validate_first_name(self, key, string):
        return string

      

Note. I wrote a decorator validate_required_field

to avoid repetitive code.

When I post the POST data to the phone/api/v1/candidate

with an empty column , it checks it correctly and gives me the error

{
    "validation_errors": {
        "phone": "must not be empty"
    }
}

      

But when I pass an empty column first_name the same doesn't happen :(

What am I doing wrong? Please, help

+3


source to share


1 answer


You have duplicated your function validate_first_name

for fields phone

and first_name

.



0


source







All Articles