moment / luxon

⏱ A library for working with dates and times in JS
https://moment.github.io/luxon
MIT License
15.23k stars 730 forks source link

Provide additional information on Invalid dates #1442

Open LiamMacP opened 1 year ago

LiamMacP commented 1 year ago

Is your feature request related to a problem? Please describe. There are some instances where the reason that a date is invalid may need to be acted upon to show custom error messages, of which the luxon defaults aren't appropriate or fit within specific organisational design standards.

For example if the the following date is passed in 2011-02-29 into luxon, it correctly identifies that the invalid reason is unit out of range and explanation is you specified 29 (of type number) as a day, which is invalid. This invalid object does not however give any helpful single pointer as to which unit has failed (albeit regex matching the invalid explanation would be a painful solution). It is required that if an invalid day is provided then a error message should be provided with <FIELD> must contain a real day.

Describe the solution you'd like A simple way of quickly identifying additional information about why a specific date is invalid (out of range). This could be an additional property on the invalid object for meta data. Which could provide the unit as a enum based value that is accessible and can be switched on.

Describe alternatives you've considered Another Invalid class, such as InvalidUnit, could be created that would just hold the erroneous unit. This would allow a getter to be simply added on this new class to retrieve the unit, rather than having to do meta object parsing to retrieve specific values.

icambron commented 1 year ago

Yeah, the errors could certainly be more detailed. As a workaround in the meantime, see fromFormatExplain