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

Support date strings #25

Open
omar-dulaimi opened this issue Aug 8, 2022 · 2 comments
Open

Support date strings #25

omar-dulaimi opened this issue Aug 8, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@omar-dulaimi
Copy link
Owner

Problem

payload {
      data: {
        name: 'Blanca Hyatt',
        createdAt: 2022-08-09T03:58:40.668Z,
        updatedAt: 2022-08-08T18:58:18.021Z
      }
    }

result [
      {
        code: 'invalid_type',
        expected: 'date',
        received: 'string',
        path: [ 'data', 'createdAt' ],
        message: 'Expected date, received string'
      },
      {
        code: 'invalid_type',
        expected: 'date',
        received: 'string',
        path: [ 'data', 'updatedAt' ],
        message: 'Expected date, received string'
      }
    ]

Suggested solution

const dateSchema = z.preprocess((arg) => {
  if (typeof arg == "string" || arg instanceof Date) return new Date(arg);
}, z.date());
type DateSchema = z.infer<typeof dateSchema>;
// type DateSchema = Date

dateSchema.safeParse(new Date("1/12/22")); // success: true
dateSchema.safeParse("2022-01-12T00:00:00.000Z"); // success: true

Alternatives

new Date('2022-08-09T03:58:40.668Z');

Additional context

Taken from this issue: #22

@omar-dulaimi omar-dulaimi added the enhancement New feature or request label Aug 8, 2022
@omar-dulaimi omar-dulaimi added this to To do in Issues Progress Tracking via automation Aug 11, 2022
@potikhanovsergey
Copy link

it would be really cool if it was implemented 👍

@fredericrous
Copy link

hey, not sure this should be covered by prisma-trpc-generator. Instead it should be documented imo

create a file trpcOptions.ts

import superjson from "superjson";

const trpcOptions = {
  transformer: superjson,
}
export default trpcOptions;

import that file both inside your prisma.schema

generator trpc {
  provider          = "prisma-trpc-generator"
  output            = "./trpc"
  isGenerateSelect  = true
  isGenerateInclude = true
  withMiddleware    = false
  withShield        = true
  contextPath       = "../server/trpcContext"
  trpcOptionsPath   = "../server/trpcOptions"  <-- there
}

and inside your server's config, either createClient or createTRPCNext

export const api = createTRPCNext<AppRouter>({
  config() {
    return {
       ...trpcOptions,

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
No open projects
Development

No branches or pull requests

3 participants