Skip to content

Commit b23989a

Browse files
chore(stock-price-checker): check input (#481)
1 parent b478bb2 commit b23989a

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

apps/stock-price-checker/controllers/stockHandler.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ const request = require('request');
1212

1313
function StockHandler() {
1414
this.getData = function(stock, callback) {
15+
if (!isValidStock(stock)) {
16+
callback('stockData', { error: 'invalid symbol' });
17+
return;
18+
}
1519
request(
1620
`https://stock-price-checker-proxy.freecodecamp.rocks/v1/stock/${stock}/quote`,
1721
function(error, response, body) {
@@ -68,4 +72,10 @@ function StockHandler() {
6872
};
6973
}
7074

75+
const validTickerRegExp = /^[a-z]{1,6}$/;
76+
const isValidStock = (stock) => {
77+
const stockL = stock.toLowerCase().trim();
78+
validTickerRegExp.test(stockL);
79+
}
80+
7181
module.exports = StockHandler;

0 commit comments

Comments
 (0)