sam-goodwin / eventual

Build scalable and durable micro-services with APIs, Messaging and Workflows
https://docs.eventual.ai
MIT License
174 stars 4 forks source link

Support zod's transform feature for Date times #394

Closed sam-goodwin closed 1 year ago

sam-goodwin commented 1 year ago

We should allow users to use .transformin Zod to map serializable values into JavaScript-native types and objects.

E.g. use an ISO string in DynamoDB but mapped to JS's Date at runtime

const MyEntity = entity("MyEntity", {
  attributes: {
    createdTime: z
      .string()
      .datetime()
      .transform((d) => new Date(d))
  }
});

We are too strict with types:

Type 'ZodEffects<ZodString, Date, string>' is not assignable to type 'ZodType<AttributeValue, ZodTypeDef, AttributeValue>'.
  Types of property '_type' are incompatible.
    Type 'Date' is not assignable to type 'AttributeValue'.
      Type 'Date' is not assignable to type 'Attributes'.
        Index signature for type 'string' is missing in type 'Date'.ts(2322)

Perhaps the ZodEffects<ZodString, Date, string> here - we can validate on the string instead of the Date. We can directly see the serialized type is valid.

sam-goodwin commented 1 year ago

Wasted way too much time looking into this: https://github.com/functionless/eventual/tree/sam/zod-effects

Got all the way and then realized Zod doesn't do serialization, so the user can't provide a way to convert Date (i.e. artbitrary type) into primitive. Fucking annoying.