Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom fields serialisation & deserialisation #456

Open
nikhildigde opened this issue Apr 23, 2024 · 1 comment
Open

Custom fields serialisation & deserialisation #456

nikhildigde opened this issue Apr 23, 2024 · 1 comment

Comments

@nikhildigde
Copy link

nikhildigde commented Apr 23, 2024

Hi,
I am trying to create a custom field to implement client side encryption. I am able to perform serialisation during save, however I dont see a way to deserialise the data after a find (decrypt it). Is this possible by some means?

This is how I created the custom field

`class EncryptedFieldType(str):

@classmethod
def __get_validators__(cls):
    yield cls.validate

@classmethod
def validate(cls, v):
    if isinstance(v, bytes):  # Handle data coming from MongoDB
        print("In isinstance(v, bytes) ...")
        return "hello"
    if not isinstance(v, str):
        raise TypeError("string required")
    if not v.isascii():
        raise ValueError("Only ascii characters are allowed")
    return v

@classmethod
def __bson__(cls, v) -> str:
    print("In __bson__")
    return "*******"

class CollectionWithEncField(Model):
encrypted_field: EncryptedFieldType`

@nikhildigde
Copy link
Author

I just figured that I can use the below

def validate(cls, v):
return decrypt(v)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant