-
Notifications
You must be signed in to change notification settings - Fork 62
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
Should result validation be optional #16
Comments
Well, it's all about your last line:
Say I have some typical code like: function calculateStockHoldingValue(symbol) {
const { qty } = await db.holdings.findOne({ symbol });
const { price } = await yahooFinance.quoteSummary(symbol, { modules: "price" });
return qty * price.regularMarketPrice;
} Say
In the best case scenario, this will crash my app somewhere else. In the worst case scenario, the NaN will be used with other calculations, which will also be NaNs, which could then crash all other unrelated parts of my app. Or with dates:
Having said all that, if these options happen frequently, we could at least make all fields optional, and allow additional properties (at least then typescript won't allow you to use a property that could be undefined without a check). My preference however is to have a bit more pain now and then have pain-free development and production ever after - but let's see what the next few days look like :) Don't forget, the user can and should be catching these errors: let result;
try {
result = await yahooFinance.search('gold');
} catch(e) {
// do nothing with invalid result
return;
}
// Everything below here will be safe and won't throw unexpected errors
$('input').value(result.Result[0].name); However, I will add a way for the user to still get access to the result and errors to figure out if they want to regardless, understanding of the risk, say: (not possible yet) let result;
try {
result = await yahooFinance.search('gold');
} catch(e) {
// i'll take responsibility for this
result = e.result; // maybe
}
// and will do my own validation
if (result && isArray(result.Result) && result.Result[0] && typeof result.Result[0].name === 'string')
$('input').value(result.Result[0].name); |
The Error object will now include "result" and "error" properties. Note, func itself can turn off logging, but no way to use this yet.
Hey @gadicc, thanks a lot for creating a separate issue for this and nice writeup. I agree with you that strict schema validation is great since it would prevent errors as in the examples you provided above. But I don't think I'm a big fan of having strict schema validation on the response from a third party that in theory could change / add to their response whenever. Let me give you an example of where strict schema validation on the response is unwanted, in my humble opinion. Example of where strict schema validation on the response is unwantedLet's say that I want to get the stock symbol for a listed company, but I only have the company name at hand. My code could look something like this:
I forked your CodeSandbox demo and added my code, but the proxy gives me 403, you can copy the code from here though. What could happen that's "out of our control" that would break this code when not necessary?First, let's set the scene for this particular use case
So, let's ask ourselves; what could happen to the response from Yahoo that would case our validation to throw an error when in reality it wasn't needed?
The first issue I believe can be avoided by setting However, the other two issues, where Yahoo could possibly decide to change (or add) to quotes or news are harder to account for. For example, they could possibly add another type of "News Object", which doesn't affect my code at all, but would still cause the request to throw an error. Closing thoughts
Eager to hear your thoughts on this @gadicc 💯 |
@gadicc also, here's my thoughts on your suggestion to allow the developer to opt out of the validation.
At a first glance I don't like this approach, because in the developers opinion it's not an error. It's an active choice to simply not validate the response. I'm not sure what the best way is, but one way would be to pass an argument to the request itself, pseudo code below.
|
Hey @pudgereyem Thanks for writing all this up. I'm up for making it possible to opt-out out of validation. I don't believe on forcing anything on developers as long as the risks are clear. Your proposal looks good, let me play around a bit and see what it looks like. |
@gadicc awesome! Is there anything I could help out with? I had a quick look at #8 and I'm definitely up for adding another module. I was thinking that my approach could be:
Thoughts? |
Think we're good! You can go check it out. Just note from https://github.com/gadicc/node-yahoo-finance2/blob/devel/docs/validation.md that it's a const result = await yahooFinance.search('gold', {}, { validateResponse: false}); |
That would be amazing!! You could probably just take a stab at the code and submit a PR... it's pretty modular now with |
@gadicc sweet! Will try it out later tonight and look into a new module as well! 💯 |
# [1.5.0](v1.4.2...v1.5.0) (2021-02-05) ### Bug Fixes * **search:** change longname to be an optional property ([9b8128d](9b8128d)) * **validate:** honor _opts: { validation: { logErrors: true }} ([1e0ebae](1e0ebae)) * **validate:** show errors by default ([ab87ad3](ab87ad3)) ### Features * **modules:** allow { validateResult: false } to not throw ([#16](#16)) ([dc199b5](dc199b5))
If { validateResult: false } we already don't throw from earlier fix. However, we still logged the errors, which doesn't really make sense. Although the user could turn off error logging in general for all requests, this fix will turn off error logging "automatically" just for the specific request where { validateResult: false }.
## [1.5.2](v1.5.1...v1.5.2) (2021-02-06) ### Bug Fixes * **modules:** don't log errors when {validateResult:false} ([#16](#16)) ([29f23dc](29f23dc))
@pudgereyem thanks, let me know if you have any issues with it and i'll re-open. And of course feel free to open issues on anything else. |
@gadicc sweet! I spent the weekend offline mostly but will have a look now! Hope you had a nice weekend. |
Yes, sure, no pressure at all from my side! Just wanted to tidy up a bit :) I aimed to spend the weekend offline but didn't have strong enough willpower to avoid working on a few more things 😅 But it's probably for the best as I'll have a lot less time to work on this this week. Thanks! |
Originally posted by @pudgereyem in #13 (comment)
The text was updated successfully, but these errors were encountered: