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 for "type": "date-time" #183

Open
ellis opened this issue Sep 7, 2018 · 8 comments
Open

Support for "type": "date-time" #183

ellis opened this issue Sep 7, 2018 · 8 comments

Comments

@ellis
Copy link

ellis commented Sep 7, 2018

Could you please add support for the types date-time, date, and time?

@bcherny
Copy link
Owner

bcherny commented Sep 17, 2018

That's a great suggestion @ellis! Since these go in the format field and don't refer to types, the best we can do is generate type aliases (we can think about making them optionally opaque down the line when TypeScript adds support for nominal types). Something like:

/**
 * This represents a string in RFC3339 date format.
 * Eg. `2018 09 18`
 */
type DateString = string

/**
 * This represents a string in RFC3339 date format.
 * Eg. `24 59 58`
 */
type TimeString = string

// After
interface MyInterface {
  startDate: DateString
  endTime: TimeString
}

// Before
interface MyInterface {
  startDate: string
  endTime: string
}

Contributions are welcome.

If you'd like to take this on:

@maximelkin
Copy link

For now nominal types can be expressed via

type DateString = string & {__tag: 'DateString'};

Can we implement it like this?
(Also typescript inside use such solution)

@benjamine
Copy link

benjamine commented Oct 18, 2019

most people will use use a parser/reviver that supports actual Date objects,
maybe a flexible solution would be make these configurable?:

compile(user, 'User', {
   formatTypes: {
      'date': 'Date',
      'date-time': 'Date',
   },
});

I managed to get the above using the tsType property

import {map} from 'ramda';

const prepareSchema = (schema: any) =>
  typeof schema === 'object'
    ? schema.format === 'date' || schema.format === 'date-time'
      ? { ...schema, tsType: 'Date' }
      : map(prepareSchema, schema)
    : schema;

compile(prepareSchema(user), 'User');

@pikadun
Copy link

pikadun commented Jul 30, 2020

Is there a support plan now?

@michalsisak
Copy link

3 years since the issue has been created. Can you please share whether this is not on a roadmap or can we expect it at some point in the future?

Thank you for your work

@khushal-sahni
Copy link

Guys is it available yet? I have to change format to date from date-time manually every time I run this script :(

@aravindvakil
Copy link

We can use the tsType property in our JSON schema to override the type. tsType Overrides the type that's generated from the schema. Helpful in forcing a type to any or when using non-standard JSON schema extensions.

For Example, the JSON schema given below generates a type birthday with date as it's type.

const birthday = {
  name: `birthday `,
  type: `string`,
  tsType: `Date`,
  format: `date-time`,
}

export type birthday = Date;

@seiyab
Copy link

seiyab commented Sep 18, 2024

I prefer approaches like #183 (comment).
This package only generates TypeScript types. Neither validator nor converter. JSON that satisfiy schemas should just hold string, not Date. So just marking that string has specific format looks safer.
Of course, opt-in configurable solution like #183 (comment) should be helpful and welcome.

Feel free to correct me if I'm misunderstanding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants