Common: Add CredentialComponentSchema

Extract common _strip_credential_type() post_load function from
PasswordSchema and UsernameSchema into a parent class.
This commit is contained in:
Mike Salvatore 2022-07-06 10:58:53 -04:00
parent 037b4ef8c5
commit a8747c9d5d
3 changed files with 16 additions and 18 deletions

View File

@ -0,0 +1,10 @@
from marshmallow import Schema, post_load
from common.utils.code_utils import del_key
class CredentialComponentSchema(Schema):
@post_load
def _strip_credential_type(self, data, **kwargs):
del_key(data, "credential_type")
return data

View File

@ -1,24 +1,18 @@
from dataclasses import dataclass, field from dataclasses import dataclass, field
from marshmallow import Schema, fields, post_load, validate from marshmallow import fields, validate
from marshmallow_enum import EnumField from marshmallow_enum import EnumField
from common.utils.code_utils import del_key
from . import CredentialComponentType, ICredentialComponent from . import CredentialComponentType, ICredentialComponent
from .credential_component_schema import CredentialComponentSchema
class PasswordSchema(Schema): class PasswordSchema(CredentialComponentSchema):
credential_type = EnumField( credential_type = EnumField(
CredentialComponentType, validate=validate.Equal(CredentialComponentType.PASSWORD) CredentialComponentType, validate=validate.Equal(CredentialComponentType.PASSWORD)
) )
password = fields.Str() password = fields.Str()
@post_load
def _strip_credential_type(self, data, **kwargs):
del_key(data, "credential_type")
return data
@dataclass(frozen=True) @dataclass(frozen=True)
class Password(ICredentialComponent): class Password(ICredentialComponent):

View File

@ -1,24 +1,18 @@
from dataclasses import dataclass, field from dataclasses import dataclass, field
from marshmallow import Schema, fields, post_load, validate from marshmallow import fields, validate
from marshmallow_enum import EnumField from marshmallow_enum import EnumField
from common.utils.code_utils import del_key
from . import CredentialComponentType, ICredentialComponent from . import CredentialComponentType, ICredentialComponent
from .credential_component_schema import CredentialComponentSchema
class UsernameSchema(Schema): class UsernameSchema(CredentialComponentSchema):
credential_type = EnumField( credential_type = EnumField(
CredentialComponentType, validate=validate.Equal(CredentialComponentType.USERNAME) CredentialComponentType, validate=validate.Equal(CredentialComponentType.USERNAME)
) )
username = fields.Str() username = fields.Str()
@post_load
def _strip_credential_type(self, data, **kwargs):
del_key(data, "credential_type")
return data
@dataclass(frozen=True) @dataclass(frozen=True)
class Username(ICredentialComponent): class Username(ICredentialComponent):