Common: Add CredentialComponentSchema
Extract common _strip_credential_type() post_load function from PasswordSchema and UsernameSchema into a parent class.
This commit is contained in:
parent
037b4ef8c5
commit
a8747c9d5d
|
@ -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
|
|
@ -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):
|
||||||
|
|
|
@ -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):
|
||||||
|
|
Loading…
Reference in New Issue