Open dotnetCarpenter opened 2 years ago
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 from()
would accept. Is that accurate?
Yes, that is correct. The main points are to put it simply, for Temporal.PlainDate
,
test
function that can validate an ISO date string is valid, without relaying on try...catch
and creating Temporal JS instances.AFAIK the two points are identical for Temporal.PlainTime
and Temporal.PlainDateTime
.
On second thought, Temporal.PlainTime.test (String)
can easily be coded in regex. But omitting that, would break polymorphism for Temporal objects.
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 https://github.com/js-temporal/proposal-temporal-v2/issues/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 https://github.com/js-temporal/proposal-temporal-v2/issues/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
. See https://github.com/tc39/proposal-temporal/issues/1751 for a discussion about why that is important.