-
Notifications
You must be signed in to change notification settings - Fork 1
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
Static string validation of PlainDate, PlainTime and PlainDateTime #22
Comments
Thanks! If I understand correctly, the main advantage of this API would be validating the string without constructing a Temporal.(Whatever) instance, and the reason it's not ideal to do in user code is because you either have to construct the instance, or you write your own regex and then you have no guarantee that it corresponds 100% to what |
Yes, that is correct. The main points are to put it simply, for
AFAIK the two points are identical for |
On second thought, |
String
validation of date, time or date with time is currently error prone and/or memory hungry. With this proposal,String
validation will have native performance and be correct.String
is very useful for serialization and communication between services but require validation when expressing date, time or date with time.By adding a static
.test
method toTemporal.PlainDate
,Temporal.PlainTime
andTemporal.PlainDateTime
, validation would be performant, easy and correct.API:
Temporal.PlainDate.test (String)
WhereString
=YYYY-MM-DD
or±YYYY-MM-DD
Temporal.PlainTime.test (String)
WhereString
=hh:mm:ss
Temporal.PlainDateTime.test (String)
WhereString
=YYYY-MM-DDThh:mm:ss
YYYY
refers to a year between 0001 and 9999, both included.MM
refers to a a zero-padded month between 01 and 12, both included.DD
refers to a a zero-padded month day between 01 and 31, both included, in the Gregorian calendar.hh
refers to a zero-padded hour between 00 and 23, both included.mm
refers to a zero-padded minute between 00 and 59, both included.ss
refers to a zero-padded second between 00 and 60 (where 60 is only used to denote an added leap second), both included.Ref: https://en.wikipedia.org/wiki/ISO_8601
Advantages:
One use-case is to send a valid date as a
String
to a database and while the database will validate the date, we would want to check the date before opening a database connection and wait for the response. This can obviously be done both client-side and server-side before communicating with the database.With Temporal proposal 1, it can be implemented like so:
An optimization would be to have
Temporal.PlainDate.test
so that the static.test
method can bypass the instantiation process and return aBoolean
. Perhaps bringing it on par with the performance of the regex solution.Concerns:
Communicating between services sometimes also means different formats. This is covered in #2 which in contrast to this suggestion, creates an object instance. Similarly to #2, it might be useful to be able to validate other serializable formats than the ones Temporal's
from
method accept.For outputting different serializable formats from Temporal, there is #5.
I have not gone much into
Temporal.PlainTime.test
andTemporal.PlainDateTime.test
. Further examination of use-cases might reveal that it's more complicated than stated here.Prior art:
There does not seem to be any static date parsing libraries in the current JS ecosystem (as of 2021).
All four libraries below will create instance of
Date
.moment("2022-02-29").isValid()
DateTime.fromISO("2022-02-29").isValid
isValid (parseISO('2022-02-29'))
isValid (parse ('YYYY-MM-DD', '2022-02-29'))
Constraints / corner cases:
Temporal.PlainDate.test ('2020-01-01T00:00Z')
MUST returnfalse
. SeePlainDate.from('2020-01-01T00:00Z')
is problematic tc39/proposal-temporal#1751 for a discussion about why that is important.The text was updated successfully, but these errors were encountered: