Trouble with finding right solution for serialization in marshmallow /u/SupremeLeaderSyd Python Education

I have two issues:

  1. I would like to make a generic lowercaseAwareSchema that can take a string as an input (not a dict, a string) and load the string along with its lowercase version in a dict
  2. I would like to add validations to the above field not where I’m defining it but where I’m using it, ex: Email() requires the string to be a valid email etc.

Current implementations:

class LowercaseAwareField(Schema): """ Schema for a string field with lowercase version """ original = fields.String(required=True) lowercase = fields.String(required=True) @post_load def make_fields(self, data, **kwargs): data["lowercase"] = data["original"] return data class CountrySchema(Schema): """ Schema for country code and name """ code = fields.String(validate=[OneOf([country.name for country in Country])]) name = fields.String(required=True) @post_load def make_fields(self, data, **kwargs): data["name"] = Country[data["code"]].value return data class UserSchema(Schema): """ Schema for handling base user data """ email = fields.Pluck( LowercaseAwareField, "original", required=True, validate=[Email()] ) password = fields.String(required=True, load_only=True) username = fields.Pluck(LowercaseAwareField, "original", required=True) country = fields.Pluck(CountrySchema, "code", required=True) user_schema = UserSchema() 

My goal here is:

Input:

{ "email": "A@b.com", "username": "Tanav", "password": "sdafasad", "country": "US" } 

Post Load:

{ "country": { "code": "US", "name": "United States" }, "email": { "lowercase": "A@b.com", "original": "A@b.com" }, "password": "sdafasad", "username": { "lowercase": "Tanav", "original": "Tanav" } } 

Post Dump:

{ "country": { "code": "US", "name": "United States" }, "email": { "lowercase": "A@b.com", "original": "A@b.com" }, "username": { "lowercase": "Tanav", "original": "Tanav" } } 

submitted by /u/SupremeLeaderSyd
[link] [comments]

​r/learnpython I have two issues: I would like to make a generic lowercaseAwareSchema that can take a string as an input (not a dict, a string) and load the string along with its lowercase version in a dict I would like to add validations to the above field not where I’m defining it but where I’m using it, ex: Email() requires the string to be a valid email etc. Current implementations: class LowercaseAwareField(Schema): “”” Schema for a string field with lowercase version “”” original = fields.String(required=True) lowercase = fields.String(required=True) @post_load def make_fields(self, data, **kwargs): data[“lowercase”] = data[“original”] return data class CountrySchema(Schema): “”” Schema for country code and name “”” code = fields.String(validate=[OneOf([country.name for country in Country])]) name = fields.String(required=True) @post_load def make_fields(self, data, **kwargs): data[“name”] = Country[data[“code”]].value return data class UserSchema(Schema): “”” Schema for handling base user data “”” email = fields.Pluck( LowercaseAwareField, “original”, required=True, validate=[Email()] ) password = fields.String(required=True, load_only=True) username = fields.Pluck(LowercaseAwareField, “original”, required=True) country = fields.Pluck(CountrySchema, “code”, required=True) user_schema = UserSchema() My goal here is: Input: { “email”: “A@b.com”, “username”: “Tanav”, “password”: “sdafasad”, “country”: “US” } Post Load: { “country”: { “code”: “US”, “name”: “United States” }, “email”: { “lowercase”: “A@b.com”, “original”: “A@b.com” }, “password”: “sdafasad”, “username”: { “lowercase”: “Tanav”, “original”: “Tanav” } } Post Dump: { “country”: { “code”: “US”, “name”: “United States” }, “email”: { “lowercase”: “A@b.com”, “original”: “A@b.com” }, “username”: { “lowercase”: “Tanav”, “original”: “Tanav” } } submitted by /u/SupremeLeaderSyd [link] [comments] 

I have two issues:

  1. I would like to make a generic lowercaseAwareSchema that can take a string as an input (not a dict, a string) and load the string along with its lowercase version in a dict
  2. I would like to add validations to the above field not where I’m defining it but where I’m using it, ex: Email() requires the string to be a valid email etc.

Current implementations:

class LowercaseAwareField(Schema): """ Schema for a string field with lowercase version """ original = fields.String(required=True) lowercase = fields.String(required=True) @post_load def make_fields(self, data, **kwargs): data["lowercase"] = data["original"] return data class CountrySchema(Schema): """ Schema for country code and name """ code = fields.String(validate=[OneOf([country.name for country in Country])]) name = fields.String(required=True) @post_load def make_fields(self, data, **kwargs): data["name"] = Country[data["code"]].value return data class UserSchema(Schema): """ Schema for handling base user data """ email = fields.Pluck( LowercaseAwareField, "original", required=True, validate=[Email()] ) password = fields.String(required=True, load_only=True) username = fields.Pluck(LowercaseAwareField, "original", required=True) country = fields.Pluck(CountrySchema, "code", required=True) user_schema = UserSchema() 

My goal here is:

Input:

{ "email": "A@b.com", "username": "Tanav", "password": "sdafasad", "country": "US" } 

Post Load:

{ "country": { "code": "US", "name": "United States" }, "email": { "lowercase": "A@b.com", "original": "A@b.com" }, "password": "sdafasad", "username": { "lowercase": "Tanav", "original": "Tanav" } } 

Post Dump:

{ "country": { "code": "US", "name": "United States" }, "email": { "lowercase": "A@b.com", "original": "A@b.com" }, "username": { "lowercase": "Tanav", "original": "Tanav" } } 

submitted by /u/SupremeLeaderSyd
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *