UNPKG

test-openapi

Version:
152 lines (111 loc) 3.22 kB
"use strict";require("core-js/modules/es.array.iterator");require("core-js/modules/es.string.replace");Object.defineProperty(exports,"__esModule",{value:true});exports.validateFromSchema=void 0; var _ajv=_interopRequireDefault(require("ajv")); var _moize=_interopRequireDefault(require("moize")); var _lodash=require("lodash"); var _underscore=require("underscore.string"); var _json_pointer=require("../utils/json_pointer.js"); var _path=require("../utils/path.js"); var _instance=require("./instance.js");function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}} const validateFromSchema=function({ schema, value, valueProp, schemaProp, message, instance=_instance.defaultInstance}) { const passed=instance.validate(schema,value); if(passed){ return; } const[error]=instance.errors; const errorA=getError({ error, schema, value, schemaProp, valueProp, message}); return errorA; }; const getError=function({ error, schema, value, schemaProp, valueProp, message}) { const messageA=getMessage({error,message,valueProp}); const errorPath=getErrorPath({error}); const valueA=getValue({errorPath,value}); const valuePath=getValuePath({errorPath,valueProp}); const schemaParts=getSchemaParts({error}); const schemaA=getSchema({schemaParts,schema}); const schemaPath=getSchemaPath({schemaParts,schemaProp}); const errorA={ message:messageA, value:valueA, schema:schemaA, valuePath, schemaPath}; const errorB=(0,_lodash.omitBy)(errorA,valueB=>valueB===undefined); return errorB; }; const getMessage=function({error,message,valueProp}){ const messagePrefix=getMessagePrefix({message,valueProp}); const errorMessage=_ajv.default.prototype. errorsText([error],{dataVar:""}). replace(FIRST_CHAR_REGEXP,""); return`${messagePrefix}: ${errorMessage}`; }; const FIRST_CHAR_REGEXP=/^[ \.]/u; const getMessagePrefix=function({message,valueProp}){ if(message!==undefined){ return(0,_underscore.capitalize)(message); } if(valueProp!==undefined){ return`'${valueProp}' is invalid`; } return"Value is invalid"; }; const getErrorPath=function({error:{dataPath}}){ return dataPath.replace(FIRST_DOT_REGEXP,""); }; const FIRST_DOT_REGEXP=/^\./u; const getValue=function({errorPath,value}){ if(errorPath===""){ return value; } return(0,_lodash.get)(value,errorPath); }; const getValuePath=function({errorPath,valueProp}){ return concatProp(valueProp,errorPath); }; const getSchemaParts=function({error:{schemaPath}}){ return(0,_json_pointer.jsonPointerToParts)(schemaPath); }; const getSchema=function({schemaParts,schema}){ const key=schemaParts[schemaParts.length-1]; const value=(0,_lodash.get)(schema,schemaParts); return{[key]:value}; }; const getSchemaPath=function({schemaParts,schemaProp}){ const schemaPath=(0,_path.getPath)(schemaParts); const schemaPathA=concatProp(schemaProp,schemaPath); return schemaPathA; }; const concatProp=function(prop,path){ if(prop===undefined){ return path; } if(path===""){ return prop; } if(path.startsWith("[")){ return`${prop}${path}`; } return`${prop}.${path}`; }; const mValidateFromSchema=(0,_moize.default)(validateFromSchema,{isDeepEqual:true});exports.validateFromSchema=mValidateFromSchema; //# sourceMappingURL=validate.js.map