-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add coerce for Date #11
Comments
@borracciaBlu There are no such coercion mechanisms for object types in JS language, and this library doesn't attempt to break that. I assume your after some form of |
@medikoo I see your point. The thing i'm working on involve an API from which i should get a string ISO date or null ,
Actually as possible workaround i think i'll glue Thanks anyway 🙏 |
Indeed, Actually it's an interesting use case, why do you want to silently allow invalid date string? If you would want to just to permit const date = isValue(dateString) ? ensureDate(Date.parse(dateString)) : null; |
I was thinking something more like: function (value) {
if (!isValue(value)) return null;
const coercedValue = new Date(value);
if (isDate(coercedValue)) return coercedValue;
return null;
}; As mentioned
Why do you want to silently allow invalid date string?Well, not exactly silently actually. Let's say that it's acceptable to be wrong as far as the app is not broken and the user can still consume the rest of the information. There is a step of validation and possible errors will be logged and used to provide feedback for BE. In this particular case the priorities are:
Just to give you a sense let resp = callAPI(); // Return a Promise
resp
.then(validateAndLogError)
resp
.then(sanitizeData)
.then(setData);
// in react then is easy enough to handle null
{
data &&
<p> {data}</p>
} With something like this having |
@borracciaBlu Thanks for clarifications, I think following could be more self-explanatory in this case: function (value) {
if (!isValue(value)) return null;
try {
return ensureDate(new Date(value));
} catch (error) {
// Report invalid input value
return null;
}
}; |
Hi @medikoo
I've been using
type
in a new project but discovered that there's nocoerce
function fordate
.Is there a particular reason for this?
If not, would that be possible to have a
coerce
function forDate
please?Happy to give a hand if you want to
The text was updated successfully, but these errors were encountered: