-
Notifications
You must be signed in to change notification settings - Fork 159
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
Date type is missing from primitive types #2
Comments
Hi, Yes, I originally included Date as a type but had decided to remove it from the library. The reason is that Date is not a JSON primitive (JSON only supports strings, numbers, booleans, objects and arrays). For the case of Date, it is typically expressed as a iso8601 string value, sometimes as a unix timestamp (number) sometimes UTC, sometimes even Also, typebox is only really concerned with structural validation, not data validation. If you imagine this as a pipeline, it might look something like.. However, I would like to have some ability to generate Cheers ! |
Well then I suppose there should be yet another logical stage in the pipeline: |
Yup, implicit conversion should be handled in additional (external) stages where its more clear whats going on. Btw, also take a look at https://github.com/pelotom/runtypes. Seems there are a few libraries of this sort springing up nowadays. One day, I do hope we get opt-in boundary run time type checking of this sort implemented in the TypeScript compiler itself. If its at all possible to have typescript tie itself to JSONSchema in the same way as .NET / C# types have analogous tooling for XML/XSD, that would be something. Would be good to organize and formalize a proposal for this sort of thing. Open to collaboration if you are interested. |
We use notepack for encoding which supports Date objects. Would it be possible to re introduce the Date type? We could add this type internally within our project but the problem is that we cannot extend TSchema. https://github.com/toby-wollit/typebox/commit/9b1171868f7ed48d2ac26f895ce050e112ddc1c5 |
I use Typebox for typing all my routes, and use Fastify framework with implicitly convert dates to string. I ended writing this custom type: export const DateKind = Symbol("DateKind");
export interface TDate extends TSchema { type: "string" ; $static: Date; kind: typeof DateKind }
export const TypeDate = Type.String({ format: "date-time" }) as TString | TDate; It may not be the perfect solution (and it's kinda cheating), but that may help some of you that have the same problem as me. Also, if you have a better solution, don't hesitate to share and tag me! |
hi its 2022 have you considered date object |
Not sure if it fits within big picture here, but Date javascript type could well be considered primitive.
The text was updated successfully, but these errors were encountered: