From 02a932ce7a9eac0b444ba0367f861bdbb701c880 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Tue, 22 Jan 2019 00:35:23 +0100 Subject: [PATCH] Schema: removed filter function for getValidated to return empty object in case of error; Added OVV_KEYS_BADFORMAT message to throw in case of error; --- src/messages.js | 1 + src/pass.js | 7 +++++-- src/schema.js | 32 ++++++-------------------------- 3 files changed, 12 insertions(+), 28 deletions(-) diff --git a/src/messages.js b/src/messages.js index 0532ca4..fccbefc 100644 --- a/src/messages.js +++ b/src/messages.js @@ -7,6 +7,7 @@ const errors = { INVALID_CERTS: "Invalid certificate(s) loaded: %s. Please provide valid WWDR certificates and developer signer certificate and key (with passphrase). Refer to docs to obtain them.", INVALID_CERT_PATH: "Invalid certificate loaded. %s does not exist.", TRSTYPE_REQUIRED: "Cannot proceed with pass creation. transitType field is required for boardingPasses.", + OVV_KEYS_BADFORMAT: "Cannot proceed with pass creation due to bad keys format in overrides. Debug the app through `DEBUG=* node yourapp.js` to get more information about the error." }; const debugMessages = { diff --git a/src/pass.js b/src/pass.js index 2cfe43d..1182ad0 100644 --- a/src/pass.js +++ b/src/pass.js @@ -370,7 +370,7 @@ class Pass { let valid = data .map(o => schema.getValidated(o, "barcode")) - .filter(o => o instanceof Object); + .filter(o => !!Object.keys(o).length); if (valid.length) { this._props["barcode"] = valid[0]; @@ -669,8 +669,11 @@ class Pass { } let modelPath = path.resolve(options.model) + (!!options.model && !path.extname(options.model) ? ".pass" : ""); + const filteredOpts = schema.getValidated(options.overrides, "supportedOptions"); - const filteredOpts = schema.filter(options.overrides, "supportedOptions"); + if (!Object.keys(filteredOpts).length) { + throw new Error(formatMessage("OVV_KEYS_BADFORMAT")) + } return { model: modelPath, diff --git a/src/schema.js b/src/schema.js index 53dfa75..1af41ea 100644 --- a/src/schema.js +++ b/src/schema.js @@ -135,45 +135,25 @@ function isValid(opts, schemaName) { } /** - * Keeps only the opts elements that are compliant with the selected schema. - * @param {object} opts - * @param {string} schemaName - the selected schema. - */ - -function filter(opts, schemaName) { - let list = Object.keys(opts); - - return list.reduce((acc, current) => { - let check = { [current]: opts[current] }; - - if (isValid(check, schemaName)) { - acc[current] = opts[current]; - } - - return acc; - }, {}); -}; - -/** - * Executes the validation in verbose mode, exposing the value or + * Executes the validation in verbose mode, exposing the value or an empty object * @param {object} opts - to be validated * @param {*} schemaName - selected schema - * @returns {any} false or the returned value + * @returns {object} the filtered value or empty object */ function getValidated(opts, schemaName) { let resolvedSchema = resolveSchemaName(schemaName); - let validation = Joi.validate(opts, resolvedSchema); + let validation = Joi.validate(opts, resolvedSchema, { stripUnknown: true }); if (validation.error) { - return !validation.error; + debug(`Validation failed in getValidated due to error: ${validation.error.message}`); + return {}; } return validation.value; -}; +} module.exports = { isValid, - filter, getValidated };