From ab80e81ccce2f678d051229fd66b6c1be75199e7 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Wed, 28 Feb 2018 16:09:23 +0100 Subject: [PATCH] validate more input --- format/products-bitmask.js | 5 +++-- index.js | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/format/products-bitmask.js b/format/products-bitmask.js index f28e5a9a..b3baab87 100644 --- a/format/products-bitmask.js +++ b/format/products-bitmask.js @@ -1,11 +1,12 @@ 'use strict' -const createFormatBitmask = (modes) => { +const createFormatBitmask = (allProducts) => { const formatBitmask = (products) => { if(Object.keys(products).length === 0) throw new Error('products filter must not be empty') let bitmask = 0 for (let product in products) { - if (products[product] === true) bitmask += modes[product].bitmask + if (!allProducts[product]) throw new Error('unknown product ' + product) + if (products[product] === true) bitmask += allProducts[product].bitmask } return bitmask } diff --git a/index.js b/index.js index 91c9cf50..1a7ce2a5 100644 --- a/index.js +++ b/index.js @@ -7,12 +7,14 @@ const validateProfile = require('./lib/validate-profile') const defaultProfile = require('./lib/default-profile') const _request = require('./lib/request') +const isObj = o => o !== null && 'object' === typeof o && !Array.isArray(o) + const createClient = (profile, request = _request) => { profile = Object.assign({}, defaultProfile, profile) validateProfile(profile) const departures = (station, opt = {}) => { - if ('object' === typeof station) station = profile.formatStation(station.id) + if (isObj(station)) station = profile.formatStation(station.id) else if ('string' === typeof station) station = profile.formatStation(station) else throw new Error('station must be an object or a string.') @@ -108,7 +110,9 @@ const createClient = (profile, request = _request) => { } const locations = (query, opt = {}) => { - if ('string' !== typeof query) throw new Error('query must be a string.') + if ('string' !== typeof query || !query) { + throw new Error('query must be a non-empty string.') + } opt = Object.assign({ fuzzy: true, // find only exact matches? results: 10, // how many search results? @@ -158,7 +162,7 @@ const createClient = (profile, request = _request) => { } const nearby = (location, opt = {}) => { - if ('object' !== typeof location || Array.isArray(location)) { + if (!isObj(location)) { throw new Error('location must be an object.') } else if (location.type !== 'location') { throw new Error('invalid location object.') @@ -200,6 +204,12 @@ const createClient = (profile, request = _request) => { } const journeyLeg = (ref, lineName, opt = {}) => { + if ('string' !== typeof ref || !ref) { + throw new Error('ref must be a non-empty string.') + } + if ('string' !== typeof lineName || !lineName) { + throw new Error('lineName must be a non-empty string.') + } opt = Object.assign({ passedStations: true // return stations on the way? }, opt)