mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-16 01:25:30 +00:00
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:
@@ -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
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user