samchon / typia

Super-fast/easy runtime validators and serializers via transformation
https://typia.io/
MIT License
4.6k stars 158 forks source link

is there any way to auto convert string to Date ? #527

Closed wittech closed 1 year ago

wittech commented 1 year ago

frontend post string value like '2023-01-01T12:00:00Z' , is there any way to auto convert string to Date at backend by typia?

samchon commented 1 year ago

https://swagger.io/docs/specification/data-models/data-types/

What about using such comment tags?

Such format is supported feature in swagger and it seems what you want.

If you prepare regular expression for date and date-time format, I'll update typia ASAP.

export interface ISomeDto {
    /**
     * @format date
     */
    date: string;

    /**
     * @format date-time
     */
    datetime: string;
}
wittech commented 1 year ago

we use Prisma to create entity,and Prisma must use Date type not string, so we need define dto like this.

export interface ISomeDto { birthdate: Date; }

but we send birthdate: '2023-01-01T12:00:00Z' from frontend,so we want to auto cast birthdate from string to Date.

samchon commented 1 year ago

Ah, no way, not possible in typia.

If you have any idea about automatic conversion to Date, it is okay to suggest.

wittech commented 1 year ago

how about use class instead of interface ? like this

export class SomeDto { birthdate: Date; }

kakasoo commented 1 year ago

I encountered the following error in the current e2e test.

'TypeError: $pick(...) is not a function'

Now I realize that this error occurs only in the Date type. ( Maybe the error should be a little more obvious. ) In order to use typia, do I need to specify a 'string' type for the type that is a timestamp value? I'm leaving a question here because it seems to be an issue dealing with the same problem.

( I tried to implement Date, Date-time as a regular expression, but my skills were not enough because of the case where the last date of each month was 30 or 31 and the leap year. I'll try when I have time later. )

yadirhb commented 1 year ago

So, Date types are not planned to be supported???

samchon commented 1 year ago

Planning to support it through below feature, but it would be next year

https://github.com/samchon/typia/issues/683

sergio-milu commented 6 months ago

Planning to support it through below feature, but it would be next year

683

hey, any updates on this one? thanks

lucasrosa90 commented 6 months ago

Planning to support it through below feature, but it would be next year

683

hey, any updates on this one? thanks

I'm waiting for it too, it's a limitation for me to start using this library as I wanted, because the advantages of parse/stringify I don't have with my solution.


@sergio-milu I'm using with tRPC, my solution was use SuperJSON as transformer and then this library works fine.

export interface User {
  // ...

  /**
   * The email of the user
   * @format email
   */
  email: string;

  /**
   * The name of the user
   * @minLength 1
   */
  name: string;

  /**
   * The date the user was created
   */
  createdAt: Date;

  /**
   * The date the user was updated
   */
  updatedAt: Date;
  // ...
}

// Exporting `createdAt` just for testing.
export type UserCreateInput = Pick<User, 'name' | 'email' | 'createdAt'>;

// --- server
import SuperJSON from 'superjson';

this.trpc = initTRPC.create({ transformer: SuperJSON })

this.appRouter = this.trpc.router({
      typia: this.trpc.procedure
        .input(typia.createAssert<UserCreateInput>())
        .output(typia.createAssert<User>())
        .query(({ input }) => {
          console.log('typia', input.createdAt instanceof Date);
          return {
            ...typia.random<User>(),
            ...input,
          };
        })
})

// --- client
import SuperJSON from 'superjson';

export const trpcClient = trpc.createClient({
  links: [
    httpBatchLink({
      url: `${process.env.NEXT_PUBLIC_SERVER}/trpc`,
      transformer: SuperJSON
    }),
  ],
});

// ---

const createdAt = new Date();

export default function Home() {
  const { data: typia } = trpc.typia.useQuery({ name: 'Name', email: 'email@domain.dev', createdAt });

  if (typia === undefined) return '<>loading...</>';

  console.log(typia.createdAt instanceof Date); // true
  console.log(typia.updatedAt instanceof Date); // true
// ...
}

cc @samchon

RDeluxe commented 1 month ago

I second this.

I just encountered this issue with Date (I figure that's a common one), and this has been quite blocking for us. I really like Typia (the Nestia package is great), but the workaround of using string and convert them manually is kind of a pain.