-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
timestamp fields get parsed as string in the ts client #23
Comments
ahh I probably overlooked that, need an automated way to test the generated clients, doing it manually isn't great haha, they should be |
I guess we need something like this in Deno / Node since const reDate = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*))(?:Z|(\+|-)([\d|:]*))?$/
function dateParser(key, value) {
return typeof value == 'string' && reDate.test(value)
? new Date(value)
: value
}
console.log(JSON.parse(`{ "name": "Whatever", "created": "2020-07-07T18:26:34.503Z" }`, dateParser))
{ name: 'Whatever', created: 2020-07-07T18:26:34.503Z } |
Elegant. Yea TypeScript in a way is just optional typing on top of javascsript. TypeScript spec doc says this:
So I guess nothing magical is possible because that would change the semantics of Do you mind if I patch it in? |
sure that would be great! 🎉 |
This regex in not quite right. It's not detecting Found a different one here which works for all my test cases, but it's long:
It's either that or whitelisting |
hmm I somehow thought date strings were part of the spec but I guess not haha, looks like Go uses |
let me know what you think of #27
|
I think that's going to lead to more problems because the user might want to pass a timestamp in a string field for whatever reason. This is a validation issue IMO. The biggest risk here is that we fail silently because the users are going to expect that since there is a schema file we have proper typing. The proper way to do it is to implement a validator on the client. We also have to decide on a timestamp standard because different languages will do it differently. |
Yeah true it could potentially be a problem if they really want a date string, maybe you're right about flagging which fields should be dates, those are already explicitly defined. Though it would be super safe to just use what the browser does, but apparently Go disagrees haha he only tricky part there I suppose is they could be nested, so we'd need something more like the Go server |
How about we have a decoder method which receives the response and has schema info. Inside it we do a normal But yeah the tricky part might be nesting. |
Actually when I think about it a bit more, we don't really need validation on the client because we can just assume that the server is sending the right thing and we can check that with testing. Unless maybe for security reasons if you don’t trust the server. In any case I'm closing the issue because it has been fixed. But we have a new problem now and that is that string field containing timestamps are going to get parsed as Date objects. We might need a field based decoder for that so I'm going to create a separate issue. |
SGTM, what we have now seems like it should be fine for 99% of cases at least, it would be a bit weird to want a complete timestamp as a string but maybe someone will haha. As long as we don't hit any weird edge-cases with the regexp failing on certain timestamps it should be fine |
Exactly |
Currently the generated typescript client code simply uses
JSON.parse()
when parsing responses.timestamp
fields will get decoded as strings while in the generated type they're marked as aDate
object.I guess it's related to #3 since validation and decoding would be done at the same time?
The text was updated successfully, but these errors were encountered: