UNPKG

test-openapi

Version:
101 lines (65 loc) 2.59 kB
"use strict";require("core-js/modules/es.array.iterator");require("core-js/modules/es.string.replace");Object.defineProperty(exports,"__esModule",{value:true});exports.wrapTemplateVars=void 0;var _lodash=require("lodash"); var _error=require("../../../errors/error.js"); var _cardinal=require("../../../utils/cardinal.js"); var _path=require("../../../utils/path.js"); var _check=require("../../../validation/check.js"); const wrapTemplateVars=function({vars,plugin}){ const templateConfig=getTemplateConfig({plugin}); const varsA=(0,_lodash.mapValues)(templateConfig,(schema,name)=> wrapTemplateVar({value:vars[name],name,schema,plugin})); const varsB=(0,_lodash.omitBy)(varsA,value=>value===undefined); return{...vars,...varsB}; };exports.wrapTemplateVars=wrapTemplateVars; const getTemplateConfig=function({plugin:{config}}){ const templateConfig=(0,_lodash.pickBy)(config,(value,key)=> key.startsWith(TEMPLATE_CONFIG_PREFIX)); const templateConfigA=(0,_lodash.mapKeys)(templateConfig,(value,key)=> key.replace(TEMPLATE_CONFIG_PREFIX,"")); return templateConfigA; }; const TEMPLATE_CONFIG_PREFIX="template."; const wrapTemplateVar=function({value,name,schema,plugin}){ if(value===undefined){ return; } const schemaProp=(0,_path.getPath)(["plugin","config",`template.${name}`]); validateTemplateConfig({value,name,schemaProp,plugin}); const valueA=templateVarWrapper.bind(null,{value,name,schema}); return valueA; }; const validateTemplateConfig=function({value,name,schemaProp,plugin}){ if(typeof value==="function"){ return; } throw new _error.BugError( `'plugin.config["template.${name}"]' can only be defined if 'plugin.template.${name}' is a function`, {value,property:schemaProp,module:`plugin-${plugin.name}`}); }; const templateVarWrapper=function({value,name,schema},...args){ const schemas=Array.isArray(schema)?schema:[schema]; schemas.forEach((schemaA,index)=> checkVar({schema:schemaA,value:args[index],name,index})); return value(...args); }; const checkVar=function({schema,value,name,index}){ const message=getMessage({name,index}); if(value===undefined){ return checkVarUndefined({schema,message}); } (0,_check.checkSchema)({schema,value,message}); }; const getMessage=function({name,index}){ const cardinal=(0,_cardinal.numberToCardinal)(index+1); const message=`${cardinal} argument to '${name}'`; return message; }; const checkVarUndefined=function({ schema:{"x-optional":isOptional=false}, message}) { if(isOptional){ return; } throw new _error.TestOpenApiError(`${message} must be defined`); }; //# sourceMappingURL=check.js.map