-
Notifications
You must be signed in to change notification settings - Fork 144
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
Consistency between string and objects args in plainDate.from() #2840
Comments
Finally found the previous discussions of this: |
My opinion: strings are different from property bags, because ISO 8601 and RFCs 3339 and 9557 define what's an acceptable string and what isn't. So far, we've always said that ISO strings should not be treated as if they can be "assembled" from parts, because that's what the property bag is for. Constraining an invalid ISO string like With the larger issue I agree, overflow not working on strings is a weird corner of the API. I'd be open to finding a solution to make it less weird, but I'd want to do that in a web-compatible way, in a follow up proposal. |
Not sure it's needed, but I'd like to second the above on strings being different from property bags. An "overflowed" string doesn't exist. It's just an invalid date/time string to specification vs. the property bag, which is ambiguous. A fun answer to the probably rhetorical question above. In theory, it would be |
One other point that's probably captured in the links Philip posted above, but maybe worth calling out here: there's a pretty useful case for specifying an abnormally large month or day, which is to ergonomically get the last day of a month. Temporal.PlainDate.from({ year: 2024, month: 2, day: 1000 });
// => 2024-02-29 The alternatives are much more verbose, e.g. get the first day of the month, add one month, and subtract one day. IIRC, the existence of this ability was used as a justification for not adding an Interestingly, I noticed that this throws an exception: Temporal.PlainDate.from({ year: 2024, month: 2, day: Infinity });
// => RangeError: invalid number value This is expected, right? I seem to remember that at one point it worked, but I may be imagining things.
I'm not sure this solution can exist without allowing invalid RFC 9557 strings (which I don't think we want to for exactly the arguments above). The only reasonable change I could see us making here would be to change default behavior for objects to |
The ISO string throws because it is an invalid ISO string. The constructor also throws as it is an invalid ISO date. Temporal.PlainDate.from('2024-02-30') // throws
new Temporal.PlainDate(2024, 2, 30) // throws The bag of fields does not throw because it uses the calendar protocol, which by default uses a constrain behavior as a best effort to construct a calendared date from the fields. It is just a thin wrapper over |
Personally I was more surprised that the object bag form was not strict by default. I'm happy that the string form is strict.
This doesn't seem too verbose (if the default was strict)? Temporal.PlainDate.from({ year: 2024, month: 2, day: 1000 }, { overflow: 'constrain'}); |
@justingrant Temporal won't have |
Correct. Because there's a relatively easy workaround (as detailed in this thread), we did not add an |
Some feedback I've received and noticed myself is the inconsistent handling of strings and objects in the
plainDate.from()
method. I know we have discussed this in the past and the documentation does specify this behavior but I would like some clarity on it (as we've made many changes since then).A string is checked for a valid date and can throw if the date is not valid, where as an object will constrain the value. Here is an example:
In this example
date2
will throw due to the date being invalid. (This is rejected as the string is parsed)My understanding is the rational for this came from string potentially coming in from an external source and less likely to be checked, thus the validation. However objects are more likely to be constructed by the developer and could be checked already.
I don't think this is a strong argument though. A developer could assume their input is being validated and one day switch to constructing an object (not realising the object is no longer being validated).
Do we need to ignore overflow for strings?
There's the opposite case where a developer may want to migrate from passing in objects to strings but still want the validation "turned off." This currently isn't possible as the overflow property is ignored
This will still throw. Currently we throw at string parsing so the overflow option isn't considered. Will this be expected behavior from developers, should we allow opting out of this like you can with objects?
Questions to answer
The text was updated successfully, but these errors were encountered: