ThomasAribart / json-schema-to-ts

Infer TS types from JSON schemas 📝
MIT License
1.47k stars 31 forks source link

JSON schema "format" property #26

Closed stalniy closed 2 years ago

stalniy commented 3 years ago

JSONSchema supports format on type = string and format date-time I want FromSchema to return Date object instead of string.

There may be other custom formats like object-id (MongoDB special type), so what I suggest is to allow users to specify mapping of format value to some scalar type:

type User = FromSchema<typeof schema, {
  format: {
    "date-time": Date,
    "object-id": ObjectId
  }
}>
stalniy commented 3 years ago

Never mind. I decided to go with custom solution

ThomasAribart commented 3 years ago

I still think it could be an interesting feature. json-schema-to-ts would definitely benefit from handling custom deserialization.

However, it would not be a simple road. I reopen the PR add it and as a milestone.

leoblum commented 3 years ago

+1. Also wants this feature to use it with mongodb.

isakstarlander commented 3 years ago

+1. We have quite a few places in our code base that would benefit of proper Date handling. How would you recommend going about this for the time being?

ThomasAribart commented 3 years ago

@isakstarlander @leoblum @stalnyi Alright, it seems this feature is the next on the list :) I'll try to implement it by the end of this month.

For the moment, there's not better way than replacing by hand the property type from the parsed result, for instance with ts-toolbelt Update utility: https://millsp.github.io/ts-toolbelt/modules/object_p_update.html

ThomasAribart commented 2 years ago

@stalniy @leoblum @isakstarlander It took some time but the feature is now available in v2.3.0 ✨ (done in https://github.com/ThomasAribart/json-schema-to-ts/pull/64)

See an example in the README: https://github.com/ThomasAribart/json-schema-to-ts#deserialization