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

Allow serDes for types other than string #654

Open
rattrayalex opened this issue Sep 2, 2021 · 4 comments
Open

Allow serDes for types other than string #654

rattrayalex opened this issue Sep 2, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@rattrayalex
Copy link

rattrayalex commented Sep 2, 2021

Is your feature request related to a problem? Please describe.

Right now, serDes only works for fields that are type: 'string', but I have found a few cases where I would like to perform some serialization or deserialization on types other than strings.

One example is turning Date to number; I want type: 'number' in the spec (the API has seconds since epoch), but on the backend I want Date types.

  {
    format: "my-timestamp",
    deserialize: (seconds: number): Date => new Date(seconds * 1000),
    serialize: (date: Date): number => (date as Date).getTime(),
  },

However, this doesn't seem to work – serDes only works when type: 'string', even though you can declare custom formats for type: 'number'.

Another example is booleans in query strings; I would like to be able to transform all type: 'boolean' objects into an actual boolean when they appear as strings, like so:

{
  type: 'boolean',
  format: 'querystring-boolean'
  deserialize: (bool: boolean | 'true' | 'false'): boolean => (bool === true || bool === 'true'),
  serialize: (bool: boolean): boolean => bool,
}

Describe the solution you'd like
I would like serDes to allow any field with a format to be serialized and deserialized with a custom function, not just strings.

Describe alternatives you've considered
The only workarounds I can think of right now is to use type: 'string' everywhere, or to manually convert them in all my function calls, neither of which is palatable.

@rattrayalex rattrayalex changed the title Allow serDes for type: number with custom formats Allow serDes for types other than string Sep 17, 2021
@rattrayalex
Copy link
Author

Updated the description and title with more generic (and I hope compelling) examples.

@rattrayalex
Copy link
Author

Note that currently, the output of any custom deserialize function seems to stringified after the fact, so you can't hack around this right now.

@cdimascio cdimascio added the enhancement New feature or request label Sep 23, 2021
@pilerou
Copy link
Contributor

pilerou commented Dec 20, 2021

Hello @rattrayalex

I'm looking at this enhancement.
Should we make a very generic mecanism or is there other literal types ?
I think we can deal with number, boolean and string but I can't see more.

For example, I don't think BigInt is universal enough.

Am I right ?
If it is OK to you, I can try to make the changes

@rattrayalex
Copy link
Author

Hi @pilerou,

Thanks for looking into this!

My hunch is that it should be fully generic. For example, there may even be a type: object that gets serialized into a class instantiation or something.

I'm not quite sure what you mean about BigInt but I could also imagine someone using a custom datatype for those too (like a big decimal datatype for example).

I hope this helps?

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

No branches or pull requests

3 participants