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;
This commit is contained in:
Alexander Cerutti
2019-01-22 00:35:23 +01:00
parent dcd73c0cb4
commit 02a932ce7a
3 changed files with 12 additions and 28 deletions

View File

@@ -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_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.", INVALID_CERT_PATH: "Invalid certificate loaded. %s does not exist.",
TRSTYPE_REQUIRED: "Cannot proceed with pass creation. transitType field is required for boardingPasses.", 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 = { const debugMessages = {

View File

@@ -370,7 +370,7 @@ class Pass {
let valid = data let valid = data
.map(o => schema.getValidated(o, "barcode")) .map(o => schema.getValidated(o, "barcode"))
.filter(o => o instanceof Object); .filter(o => !!Object.keys(o).length);
if (valid.length) { if (valid.length) {
this._props["barcode"] = valid[0]; this._props["barcode"] = valid[0];
@@ -669,8 +669,11 @@ class Pass {
} }
let modelPath = path.resolve(options.model) + (!!options.model && !path.extname(options.model) ? ".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 { return {
model: modelPath, model: modelPath,

View File

@@ -135,45 +135,25 @@ function isValid(opts, schemaName) {
} }
/** /**
* Keeps only the opts elements that are compliant with the selected schema. * Executes the validation in verbose mode, exposing the value or an empty object
* @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
* @param {object} opts - to be validated * @param {object} opts - to be validated
* @param {*} schemaName - selected schema * @param {*} schemaName - selected schema
* @returns {any} false or the returned value * @returns {object} the filtered value or empty object
*/ */
function getValidated(opts, schemaName) { function getValidated(opts, schemaName) {
let resolvedSchema = resolveSchemaName(schemaName); let resolvedSchema = resolveSchemaName(schemaName);
let validation = Joi.validate(opts, resolvedSchema); let validation = Joi.validate(opts, resolvedSchema, { stripUnknown: true });
if (validation.error) { if (validation.error) {
return !validation.error; debug(`Validation failed in getValidated due to error: ${validation.error.message}`);
return {};
} }
return validation.value; return validation.value;
}; }
module.exports = { module.exports = {
isValid, isValid,
filter,
getValidated getValidated
}; };