0

I am trying to validate my data which i am posting through post request and i want to check if the data i proper or not and with that i want to do type casting and manipulate the data.

I have tried to manipulate the data but its not working and i cant understand what is wrong as i am new in flask so i would like to learn.

def check_name(self, obj):
  if not obj.first_name:
    return False
  else:
    return True

@pre_load
def process_email_address(self, in_data):
  in_data['email_address'] = in_data['email_address'].upper().strip()
return in_data

class InfoSchema(Schema):
  # first_name = fields.Function(lambda obj: 
  obj.first_name.lower().strip() if obj.first_name else False)
  # first_name = fields.Method(check_name)
  first_name = fields.Str(required=True)
  username = fields.String(required=True)
  last_name = fields.String(required=True)
  email_address = fields.String(required=True)
  phone_number = fields.String(required=True)
  password = fields.String(required=True)
  pincode = fields.String(required=True)
  city_id = fields.String(required=True)


class DataSchema(Schema):
  info = fields.Nested(InfoSchema)
  auth_token = fields.Nested(AuthTokenSchema) 

class UserAuthSchema(Schema):
  meta = fields.Nested(MetaSchema)
  data = fields.Nested(DataSchema)

# @validates('age')
# def validate_age(self, data):
#     if data < 14:
#         raise ValidationError('Too young!')

i want to check if the data is present and its not empty and also remove the spaces from it and and the most important i want to check if the data is in integer and if it is not then convert the string to integer like for the field "pincode" if possible

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Please don't use irrelevant tags. – Daniel Roseman Apr 07 '19 at 20:58
  • 1
    Also, "it's not working" is not a helpful problem description. Exactly what happens, and how does it differ from what you expect? – Daniel Roseman Apr 07 '19 at 20:59
  • @DanielRoseman Hi, thanks for replying. Well the field "first_name" is not converting to lower case and the email is not converting in upper case – saurabh sharma Apr 07 '19 at 21:12
  • 1
    [pre_load should be inside a class definition, not outside it](https://marshmallow.readthedocs.io/en/3.0/extending.html#pre-processing-and-post-processing-methods). – Fine Apr 08 '19 at 10:18

0 Answers0