Skip to content

Typecasting "Bytes" Fields #1268

@andrewpeters9

Description

@andrewpeters9

Description:

  • the zod plugin will create a validator like so for Bytes fields: z.union([z.string(), z.instanceof(Uint8Array)]) - i.e., a zod validator that infers to Uint8Array
  • this is problematic as the schemas are then type-casted to a type that treats Bytes as a Buffer - see
    export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
  • this then results in a type-error (see screenshot)

Solution:

  • Quick fix:
    • Remove :SchemaType from the line above
    • Change as SchemaType to as unknown as SchemaType
  • Elegant fix:
    • General background:
      • Buffers aren't supported on the frontend, and have to be serialized + deserialized into a Uint8Array or another byte array
      • Bytes fields are initially returned as a Buffer when directly interacting with the prisma client
      • Therefore, both types technically have some merit
    • A clean solution might be something like z.custom<Buffer | Uint8Array>(data => data instanceof Uint8Array)
      • NOTE: I haven't tested the above, but I believe it should work
      • The above would let us preserve those types, while not having to have a runtime Buffer object on the frontend

Screenshots
image

Environment (please complete the following information):

  • ZenStack version: [e.g., 2.0.0-beta.13]
  • Prisma version: [e.g., 5.12.1]
  • Database type: [e.g. MySQL]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions