Closed sceccotti89 closed 8 months ago
Hmm I think it really depends on the context. Consider those two cases:
value
should be requiredvalue
should be optionalSo I think it should be configured through an option: FromSchema<mySchema, { someOption: boolean }>
The question now is what is default behavior of FromSchema
? I feel like it's more frequent to use FromSchema
for validated values rather than inputs, so the option should be { keepDefaultedValuesOptional: true }
? That's a breaking change but a rather small one so I would be OK with implementing it.
WDYT ?
I think it totally make sense. I tried to do it myself but typings is somewhat a bit dark magic to me. It would be great you can take care of that, but I'm also curious about the changes that you'll make to implement it.
Much needed feature guys, let me know if it is on the roadmap or whether you are open to pull requests.
Feature is now available in the v3: https://github.com/ThomasAribart/json-schema-to-ts/issues/153 🙌
Wasn't so long to implement, sorry for the delay !
I feel like it's more frequent to use FromSchema for validated values rather than inputs
Interesting take. I think the use case of pre/post validation would be 50/50. The interface/function doing the validation of the raw data would need default
-less types.
In my specific case I don't need to validate the data with defaults applied, since I'm calling into another API that will apply necessary defaults for me.
Of course this change doesn't really causes any issues, since the keepDefaultedPropertiesOptional
option was also added, I just wanted to offer additional perspective on how this (awesome!) library is being used out in the wild. 🚀
Currently a schema like this:
results in this type:
I believe that a property with a defined default value should be treated as not optional, resulting in a type like the following:
What do you think?