title: Flask sqlAlchemy validation issue with flask_Marshmallow
body: <p>Using flask_marshmallow for input validation, with scheme.load() , I'm unable to capture the errors generated by the @validates decorator in the model</p>
  
  <p>I captured the result and errors in the resource but errors are sent directly to the users</p>
  
  <h1>==========model.py==========</h1>
  
  <pre><code>```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) &lt; 4 or len(name) &gt; 120:
              raise AssertionError('Company  name must be between 3 and 120 characters')
  
          return name
  
  ```
  </code></pre>
  
  <h3>==========schemas_company.py==============</h3>
  
  <pre><code>```python
  from ma import ma
  from models.model import Company
  
  
  class CompanySchema(ma.ModelSchema):
  
      class Meta:
          model = Company
  ```
  </code></pre>
  
  <h1>=============resources_company.py</h1>
  
  <pre><code>```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
  
  ```
  </code></pre>
  
  <h1>===========REQUEST==========</h1>
  
  <p>This is the POST request coming from the user</p>
  
  <pre><code>{
      "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"
  }
  </code></pre>
  
  <p><strong>====ISSUE DESCRIPTION======</strong></p>
  
  <p>The  above POST request generates an AssertionError exception as per validate_name function in model.py as under:</p>
  
  <pre><code>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 -
  </code></pre>
  
  <p>So the response that returns to the user is this useless error messages</p>
  
  <pre><code>{
      "message": "Internal Server Error"
  }
  </code></pre>
  
  <p><strong>My question is:</strong> </p>
  
  <p><strong>What I have to do so the raised AssertionError message is sent to the users instead of this ugly   error message?</strong></p>
  
  <pre><code>AssertionError message
  {
     "message": "Company  name must be between 3 and 120 characters" 
  }
  
  Exception 
  {
      "message": "Internal Server Error"
  }
  </code></pre>
  
  <p>I thought the error would capture the exception generated by @validates('name'), but looks like it is not the case.</p>