2

I am creating an API that allows a user to export details of specified products from the database. I need addition validation on the 'product' field other than the default validation that checks for datatype and length of string.

I have defined the namespace model as shown below

export_params = ns.model('exec_params', {
    'product': fields.String(max_length=50, required=False),
    'product_type': fields.String(max_length=50, required=True),
}))

The code for the API is shown below

@ns.route('/product_export/')
class Export(Resource):
    @ns.expect(exec_params, validate=True)
    def post(self):
        <business logic goes here>
        return <api response>

I want to prevent all users from passing the string "xyz" in the 'product' field. In other words, if a user requests for a product named 'xyz' the api should return a validation error.

How do i add a custom validation for the 'product' field given that I have to use ns.model? I don't want to skip the default validation completely. Just want to add one extra check for the field 'product'.

Vinay
  • 699
  • 4
  • 22

0 Answers0