from marshmallow import Schema, fields, validate, EXCLUDE, pre_load class CreateCustomerSchema(Schema): """Validation schema for creating a new customer""" class Meta: unknown = EXCLUDE customer_last_name = fields.Str( required=True, validate=validate.Length(min=1, max=250), error_messages={"required": "Last name is required"} ) customer_first_name = fields.Str( required=True, validate=validate.Length(min=1, max=250), error_messages={"required": "First name is required"} ) customer_town = fields.Str( required=True, validate=validate.Length(min=1, max=140), error_messages={"required": "Town is required"} ) customer_state = fields.Int( required=True, validate=validate.Range(min=0, max=50), error_messages={"required": "State is required"} ) customer_zip = fields.Str( required=True, validate=validate.Length(min=5, max=10), error_messages={"required": "Zip code is required"} ) customer_email = fields.Email( allow_none=True, load_default=None ) customer_home_type = fields.Int( required=True, validate=validate.Range(min=0, max=10), error_messages={"required": "Home type is required"} ) customer_phone_number = fields.Str( allow_none=True, validate=validate.Length(max=25), load_default=None ) customer_address = fields.Str( required=True, validate=validate.Length(min=1, max=1000), error_messages={"required": "Address is required"} ) customer_apt = fields.Str( allow_none=True, validate=validate.Length(max=140), load_default=None ) customer_description = fields.Str( allow_none=True, validate=validate.Length(max=2000), load_default=None ) class UpdateCustomerSchema(Schema): """Validation schema for updating an existing customer""" class Meta: unknown = EXCLUDE @pre_load def normalize_data(self, data, **kwargs): # Convert empty strings to None for email if 'customer_email' in data and data['customer_email'] == "": data['customer_email'] = None # Convert boolean to int for customer_automatic if 'customer_automatic' in data: if isinstance(data['customer_automatic'], bool): data['customer_automatic'] = 1 if data['customer_automatic'] else 0 return data customer_last_name = fields.Str( validate=validate.Length(min=1, max=250) ) customer_first_name = fields.Str( validate=validate.Length(min=1, max=250) ) customer_town = fields.Str( validate=validate.Length(min=1, max=140) ) customer_state = fields.Int( validate=validate.Range(min=0, max=50) ) customer_zip = fields.Str( validate=validate.Length(min=5, max=10) ) customer_email = fields.Email( allow_none=True ) customer_home_type = fields.Int( validate=validate.Range(min=0, max=10) ) customer_phone_number = fields.Str( allow_none=True, validate=validate.Length(max=25) ) customer_address = fields.Str( validate=validate.Length(min=1, max=1000) ) customer_apt = fields.Str( allow_none=True, validate=validate.Length(max=140) ) customer_automatic = fields.Int( validate=validate.Range(min=0, max=10) ) customer_description = fields.Str( allow_none=True, validate=validate.Length(max=2000) ) customer_fill_location = fields.Str( allow_none=True, validate=validate.Length(max=250) ) class UpdateDescriptionSchema(Schema): """Validation schema for updating customer description""" class Meta: unknown = EXCLUDE description = fields.Str( allow_none=True, validate=validate.Length(max=2000) ) fill_location = fields.Int( allow_none=True )