mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 19:25:23 +00:00
Converted schemas arrow functions to functions and added descriptive comments
This commit is contained in:
@@ -106,11 +106,18 @@ let schemas = {
|
|||||||
supportedOptions
|
supportedOptions
|
||||||
};
|
};
|
||||||
|
|
||||||
let resolveSchemaName = (name) => {
|
function resolveSchemaName(name) {
|
||||||
return schemas[name] || "";
|
return schemas[name] || "";
|
||||||
};
|
}
|
||||||
|
|
||||||
let isValid = (opts, schemaName) => {
|
/**
|
||||||
|
* Checks if the passed options are compliant with the indicated schema
|
||||||
|
* @param {any} opts - options to be checks
|
||||||
|
* @param {string} schemaName - the indicated schema (will be converted)
|
||||||
|
* @returns {boolean} - result of the check
|
||||||
|
*/
|
||||||
|
|
||||||
|
function isValid(opts, schemaName) {
|
||||||
let resolvedSchema = resolveSchemaName(schemaName);
|
let resolvedSchema = resolveSchemaName(schemaName);
|
||||||
|
|
||||||
if (!resolvedSchema) {
|
if (!resolvedSchema) {
|
||||||
@@ -125,25 +132,36 @@ let isValid = (opts, schemaName) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return !validation.error;
|
return !validation.error;
|
||||||
};
|
}
|
||||||
|
|
||||||
let filter = (opts, schemaName) => {
|
/**
|
||||||
let isObject = opts instanceof Object;
|
* Keeps only the opts elements that are compliant with the selected schema.
|
||||||
let list = isObject ? Object.keys(opts) : opts;
|
* @param {object} opts
|
||||||
|
* @param {string} schemaName - the selected schema.
|
||||||
|
*/
|
||||||
|
|
||||||
return list.reduce((acc, current, index) => {
|
function filter(opts, schemaName) {
|
||||||
let ref = isObject ? current : index;
|
let list = Object.keys(opts);
|
||||||
let check = isObject ? { [current]: opts[current] } : [opts[index]];
|
|
||||||
|
return list.reduce((acc, current) => {
|
||||||
|
let check = { [current]: opts[current] };
|
||||||
|
|
||||||
if (isValid(check, schemaName)) {
|
if (isValid(check, schemaName)) {
|
||||||
acc[ref] = opts[ref];
|
acc[current] = opts[current];
|
||||||
}
|
}
|
||||||
|
|
||||||
return acc;
|
return acc;
|
||||||
}, isObject ? {} : []);
|
}, {});
|
||||||
};
|
};
|
||||||
|
|
||||||
let getValidated = (opts, schemaName) => {
|
/**
|
||||||
|
* Executes the validation in verbose mode, exposing the value or
|
||||||
|
* @param {object} opts - to be validated
|
||||||
|
* @param {*} schemaName - selected schema
|
||||||
|
* @returns {any} false or the returned value
|
||||||
|
*/
|
||||||
|
|
||||||
|
function getValidated(opts, schemaName) {
|
||||||
let resolvedSchema = resolveSchemaName(schemaName);
|
let resolvedSchema = resolveSchemaName(schemaName);
|
||||||
let validation = Joi.validate(opts, resolvedSchema);
|
let validation = Joi.validate(opts, resolvedSchema);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user