Using flask_marshmallow for input validation, with scheme.load() , I'm unable to capture the errors generated by the @validates decorator in the model
I captured the result and errors in the resource but errors are sent directly to the users
```python
from sqlalchemy.orm import validates
from sqlalchemy import Column, ForeignKey, Integer, String, DateTime
from sqlalchemy.orm import relationship, backref
from sqlalchemy import create_engine
from sqlalchemy.sql import func
from flask_marshmallow import Marshmallow
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from sqlalchemy.orm import joinedload
db = SQLAlchemy()
ma = Marshmallow()
class Company(db.Model):
__tablename__ = "company"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(250), nullable=False)
addressLine1 = db.Column(db.String(250), nullable=False)
addressLine2 = db.Column(db.String(250), nullable=True)
city = db.Column(db.String(250), nullable=False)
state = db.Column(db.String(250), nullable=False)
zipCode = db.Column(db.String(10), nullable=False)
logo = db.Column(db.String(250), nullable=True)
website = db.Column(db.String(250), nullable=False)
recognition = db.Column(db.String(250), nullable=True)
vision = db.Column(db.String(250), nullable=True)
history = db.Column(db.String(250), nullable=True)
mission = db.Column(db.String(250), nullable=True)
jobs = relationship("Job", cascade="all, delete-orphan")
def save_to_db(self):
db.session.add(self)
db.session.commit()
@validates('name')
def validate_name(self, key, name):
print("=====inside validate_name=======")
if not name:
raise AssertionError('No Company name provided')
if Company.query.filter(Company.name == name).first():
raise AssertionError('Company name is already in use')
if len(name) < 4 or len(name) > 120:
raise AssertionError('Company name must be between 3 and 120 characters')
return name
```
```python
from ma import ma
from models.model import Company
class CompanySchema(ma.ModelSchema):
class Meta:
model = Company
```
```python
from schemas.company import CompanySchema
company_schema = CompanySchema(exclude='jobs')
COMPANY_ALREADY_EXIST = "A company with the same name already exists"
COMPANY_CREATED_SUCCESSFULLY = "The company was sucessfully created"
@api.route('/company')
class Company(Resource):
def post(self, *args, **kwargs):
""" Creating a new Company """
data = request.get_json(force=True)
schema = CompanySchema()
if data:
logger.info("Data got by /api/test/testId methd %s" % data)
# Validation with schema.load() OPTION_2
company, errors = schema.load(data)
print(company)
print(errors)
if errors:
return {"errors": errors}, 422
company.save_to_db()
return {"message": COMPANY_CREATED_SUCCESSFULLY}, 201
```
This is the POST request coming from the user
{
"name": "123",
"addressLine1": "400 S Royal King Ave",
"addressLine2": "Suite 356",
"city": "Miami",
"state": "FL",
"zipCode": "88377",
"logo": "This is the logo",
"website": "http://www.python.com",
"recognition": "Most innovated company in the USA 2018-2019",
"vision": "We want to change for better all that needs to be changed",
"history": "Created in 2016 with the objective of automate all needed process",
"mission": " Our mission is to find solutions to old problems"
}
====ISSUE DESCRIPTION======
The above POST request generates an AssertionError exception as per validate_name function in model.py as under:
File "code/models/model.py", line 95, in validate_name
raise AssertionError('Company name must be between 3 and 120 characters')
AssertionError: Company name must be between 3 and 120 characters
127.0.0.1 - - [30/Dec/2018 13:44:58] "POST /api/company HTTP/1.1" 500 -
So the response that returns to the user is this useless error messages
{
"message": "Internal Server Error"
}
My question is:
What I have to do so the raised AssertionError message is sent to the users instead of this ugly error message?
AssertionError message
{
"message": "Company name must be between 3 and 120 characters"
}
Exception
{
"message": "Internal Server Error"
}
I thought the error would capture the exception generated by @validates('name'), but looks like it is not the case.
I found a solution to my problem. I changed the schema as under:
from ma import ma
from models.model import Company
from marshmallow import fields, validate
class CompanySchema(ma.ModelSchema):
name = fields.Str(required=True, validate=[validate.Length(min=4, max=250)])
addressLine1 = fields.Str(required=True, validate=[validate.Length(min=5, max=250)])
addressLine2 = fields.Str(required=False, validate=[validate.Length(max=250)])
city = fields.Str(required=True, validate=[validate.Length(min=5, max=100)])
state = fields.Str(required=True, validate=[validate.Length(min=2, max=10)])
zipCode = fields.Str(required=True, validate=[validate.Length(min=5, max=250)])
logo = fields.Str(required=False, validate=[validate.Length(max=250)])
website = fields.Str(required=True, validate=[validate.Length(min=5, max=250)])
recognition = fields.Str(required=False, validate=[validate.Length(max=250)])
vision = fields.Str(required=False, validate=[validate.Length(max=250)])
history = fields.Str(required=False, validate=[validate.Length(max=250)])
mission = fields.Str(required=False, validate=[validate.Length(max=250)])
class Meta:
model = Company
Now I do not validate anything in my model so my model is just
class Company(db.Model):
__tablename__ = "company"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(250), nullable=False)
addressLine1 = db.Column(db.String(250), nullable=False)
addressLine2 = db.Column(db.String(250), nullable=True)
city = db.Column(db.String(250), nullable=False)
state = db.Column(db.String(250), nullable=False)
zipCode = db.Column(db.String(10), nullable=False)
logo = db.Column(db.String(250), nullable=True)
website = db.Column(db.String(250), nullable=False)
recognition = db.Column(db.String(250), nullable=True)
vision = db.Column(db.String(250), nullable=True)
history = db.Column(db.String(250), nullable=True)
mission = db.Column(db.String(250), nullable=True)
jobs = relationship("Job", cascade="all, delete-orphan")
def save_to_db(self):
print("=====inside save_to_db=======")
db.session.add(self)
db.session.commit()
So in the resource(view) endpoint, I have:
@api.route('/company')
class Company(Resource):
def post(self, *args, **kwargs):
""" Creating a new Company """
data = request.get_json(force=True)
schema = CompanySchema()
if data:
logger.info("Data got by /api/test/testId method %s" % data)
# Validation with schema.load() OPTION_2
company, errors = schema.load(data)
print(company)
if errors:
return {"errors": errors}, 422
company.save_to_db()
return {"message": COMPANY_CREATED_SUCCESSFULLY}, 201
So now when a user makes a wrong request with a name under 4 characters long, I'm able to return a beautiful error response to the user as under
{
"errors": {
"name": [
"Length must be between 4 and 250."
]
}
}
But if you noted why I did and the "pattern" I used you will see the following details
Thanks