UNPKG

openapi-examples-validator

Version:
2 lines 11.4 kB
(()=>{var e={115:e=>{"use strict";e.exports=require("yaml")},149:e=>{"use strict";e.exports=require("lodash.flatten")},184:e=>{"use strict";e.exports=require("errno")},258:e=>{"use strict";e.exports=require("json-pointer")},273:(e,t,r)=>{const o=r(984),{setAllPropertiesRequired:a}=r(299),{setNoAdditionalProperties:s}=r(305);function n(){return['$..examples[?(@property && typeof @property === "string" && @property.match(/[/+]json/))]']}e.exports={buildValidationMap:function(e){return e.reduce(((e,t)=>{const r=function(e){const t=e.split("/"),r=t.lastIndexOf("examples");return t.splice(r,t.length-r,"schema"),t.join("/")}(t);return e[r]=(e[r]||new Set).add(t),e}),{})},getJsonPathsToExamples:n,prepare:function(e,{noAdditionalProperties:t,allPropertiesRequired:r}={}){const n=o(e);return t&&s(n,['$..examples[?(@property && typeof @property === "string" && @property.match(/[/+]json/))]']),r&&a(n,['$..examples[?(@property && typeof @property === "string" && @property.match(/[/+]json/))]']),n}}},285:e=>{e.exports={parent:"parent",parentProperty:"parentProperty",path:"path",pointer:"pointer",value:"value"}},299:(e,t,r)=>{const{applyCallbackToAllObjectModels:o}=r(935);e.exports={setAllPropertiesRequired:function(e,t=[]){o(e,t,(()=>e=>{e.hasOwnProperty("properties")&&(e.required=Object.keys(e.properties))}))}}},305:(e,t,r)=>{const{applyCallbackToAllObjectModels:o}=r(935);e.exports={setNoAdditionalProperties:function(e,t=[]){const r=new RegExp("(?<!\\['properties'\\])\\['(?:"+a.join("|")+")'\\]");o(e,t,(e=>t=>{r.test(e)?console.warn(`"additionalProperties" flag not set for ${e} because it has a parent with a JSON-schema combiner keyword.`):a.some((e=>t.hasOwnProperty(e)))?console.warn(`"additionalProperties" flag not set for ${e} because it contains JSON-schema combiner keyword.`):t.hasOwnProperty("additionalProperties")||(t.additionalProperties=!1)}))}};const a=["oneOf","allOf","anyOf","not"]},402:e=>{"use strict";e.exports=require("ajv-formats")},410:e=>{"use strict";e.exports=require("glob")},427:(e,t,r)=>{const o=r(928),a=r(694);e.exports={createValidationResponse:function({errors:e,statistics:t={}}){return{valid:!e.length,statistics:t,errors:e}},dereferenceJsonSchema:async function(e,t){const r=process.cwd();process.chdir(o.dirname(e));const s=await a.dereference(t);return process.chdir(r),s}}},474:(e,t,r)=>{const o=r(984),{ApplicationError:a,ErrorType:s}=r(476),{setAllPropertiesRequired:n}=r(299),{setNoAdditionalProperties:i}=r(305),c='$..responses..content[?(@property && typeof @property === "string" && @property.match(/[/+]json/))]',p='$..requestBody.content[?(@property && typeof @property === "string" && @property.match(/[/+]json/))]',l=".example",u=".examples.*.value",d=`${c}${l}`,m=`${c}${u}`,h=`${p}${l}`,f=`${p}${u}`,y="single";function x(){return[d,m,"$..parameters..example","$..parameters..examples.*.value",h,f]}e.exports={buildValidationMap:function(e){const t=new Map;return e.reduce(((e,r)=>{const{pathSchemaAsArray:o,exampleType:n}=function(e){const t=e.split("/"),r=t.lastIndexOf("example"),o=r>-1?y:"multi",a=o===y?r:t.lastIndexOf("examples");return t.splice(a,t.length-a,"schema"),{exampleType:o,pathSchemaAsArray:t}}(r),i=o.join("/"),c=t.get(i);return c&&c!==n&&function(e){const t=e.slice(0,e.length-1);throw a.create({type:s.errorAndErrorsMutuallyExclusive,message:'Properties "error" and "errors" are mutually exclusive',params:{pathContext:t.join("/")}})}(o),t.set(i,n),e[i]=(e[i]||new Set).add(r),e}),{})},getJsonPathsToExamples:x,prepare:function(e,{noAdditionalProperties:t,allPropertiesRequired:r}={}){const a=o(e);return t&&i(a,x()),r&&n(a,x()),a}}},476:(e,t,r)=>{const o=r(711),{ENOENT:a}=r(184).code,s={jsENOENT:a.code,jsonPathNotFound:"JsonPathNotFound",errorAndErrorsMutuallyExclusive:"ErrorErrorsMutuallyExclusive",parseError:"ParseError",validation:"Validation"};class n{static create(e){const{code:t,message:r,path:a,cause:i}=e,c=t||e.type||s.validation,p={message:r};return s.validation===c||s.errorAndErrorsMutuallyExclusive===c?o(p,e):(a&&o(p,{params:{path:a}}),i&&o(p,i)),new n(c,p)}constructor(e,t={}){Object.assign(this,{type:e,...t})}}e.exports={ApplicationError:n,ErrorType:s}},497:(e,t,r)=>{const o=r(711),a=r(149),s=r(668),n=r(258),i=r(896),c=r(928),p=r(410),l=r(115),{JSONPath:u}=r(940),d=r(694),{createError:m}=r(184).custom,h=r(285),{getValidatorFactory:f,compileValidate:y}=r(631),x=r(600),{ApplicationError:g,ErrorType:P}=r(476),{createValidationResponse:E,dereferenceJsonSchema:j}=r(427),w=Symbol("internal"),v="schemasWithExamples",O=["yaml","yml"],F=m(P.jsonPathNotFound);async function $(e,{noAdditionalProperties:t,ignoreFormats:r,allPropertiesRequired:o}={}){const a=x.getImplementation(e);e=await d.dereference(e),e=a.prepare(e,{noAdditionalProperties:t,allPropertiesRequired:o});let s=a.getJsonPathsToExamples().reduce(((t,r)=>t.concat(S(r,e))),[]);return function({impl:e},t,r,{ignoreFormats:o}){const a=T(),s={valid:!0,statistics:a,errors:[]},n=V(r,{ignoreFormats:o});let i;try{i=e.buildValidationMap(t)}catch(e){if(!(e instanceof g))throw e;return s.valid=!1,s.errors.push(e),s}return Object.keys(i).forEach((e=>{!function({openapiSpec:e,createValidator:t,schemaPointer:r,validationMap:o,statistics:a,validationResult:s}){const n=s.errors;o[r].forEach((o=>{const i=M(o,e),c=J(r,e,!0),p=R({createValidator:t,schema:c,example:i,statistics:a}).map((e=>(e.examplePath=o,e)));p.length&&(s.valid=!1,n.splice(n.length-1,0,...p))}))}({openapiSpec:r,createValidator:n,schemaPointer:e,validationMap:i,statistics:a,validationResult:s})})),s}({impl:a},s,e,{ignoreFormats:r})}async function b(e){const t=function(e){const t=e.split(".").pop();return O.includes(t)}(e);let r;if(t)try{r=l.parse(i.readFileSync(e,"utf-8"))}catch(e){const{name:t,message:r}=e;throw new g(P.parseError,{message:`${t}: ${r}`})}else r=JSON.parse(i.readFileSync(e,"utf-8"));return await j(e,r)}function q(e){const t=T(),r=e(t);return E({errors:r,statistics:t})}function A(e,t,r,{cwdToMappingFile:o=!1,dirPathMapExternalExamples:n,ignoreFormats:l}){return s(Object.entries(t),(([t,u])=>{let d=null;try{d=J(N(t,e),e)}catch(e){return g.create(e)}return s(a([u]),(t=>{let a=[];try{const e=o?c.join(n,t):t,r=p.sync(e);if(0===r.length)return[g.create({type:P.jsENOENT,message:`No such file or directory: '${e}'`,path:e})];for(const e of r)a.push({path:c.normalize(e),content:JSON.parse(i.readFileSync(e,"utf-8"))})}catch(e){return[g.create(e)]}return s(a,(t=>R({createValidator:V(e,{ignoreFormats:l}),schema:d,example:t.content,statistics:r,filePathExample:t.path})))}))}))}function S(e,t){return u({json:t,path:e,resultType:h.pointer})}function N(e,t){const r=S(e,t);return 0===r.length&&k(e),r.length>1?[g.create({type:P.jsonPathNotFound,message:`Path to schema cannot identify unique schema: '${e}'`,path:e})]:r[0]}function T(){const e={[w]:{[v]:new Set},examplesTotal:0,examplesWithoutSchema:0};return Object.defineProperty(e,v,{enumerable:!0,get:()=>e[w][v].size}),e}function M(e,t){try{return n.get(t,e)}catch(e){return}}function R({createValidator:e,schema:t,example:r,statistics:o,filePathExample:a}){const s=[];if(o.examplesTotal++,!t)return o.examplesWithoutSchema++,s;o[w][v].add(t);const n=y(e(),t);return n(r)?s:s.concat(...n.errors.map(g.create)).map((e=>a?(e.exampleFilePath=a,e):e))}function V(e,{ignoreFormats:t}){return f(e,{schemaId:"auto",discriminator:!0,strict:!1,allErrors:!0,formats:t&&t.reduce(((e,t)=>(e[t]=()=>!0,e)),{})})}function J(e,t,r=!1){const o=M(e,t);return r||o||k(e),o}function k(e){throw new F(`Path to schema can't be found: '${e}'`,{params:{path:e}})}e.exports={default:$,validateFile:async function(e,{noAdditionalProperties:t,ignoreFormats:r,allPropertiesRequired:o}={}){let a=null;try{a=await b(e)}catch(e){return E({errors:[g.create(e)]})}return $(a,{noAdditionalProperties:t,ignoreFormats:r,allPropertiesRequired:o})},validateExample:async function(e,t,r,{noAdditionalProperties:o,ignoreFormats:a,allPropertiesRequired:s}={}){let n=null,c=null,p=null;try{n=JSON.parse(i.readFileSync(r,"utf-8")),p=await b(e),p=x.getImplementation(p).prepare(p,{noAdditionalProperties:o,allPropertiesRequired:s}),c=J(N(t,p),p)}catch(e){return E({errors:[g.create(e)]})}return q((e=>R({createValidator:V(p,{ignoreFormats:a}),schema:c,example:n,statistics:e,filePathExample:r})))},validateExamplesByMap:async function(e,t,{cwdToMappingFile:r,noAdditionalProperties:a,ignoreFormats:s,allPropertiesRequired:n}={}){let l=0;const u=p.sync(t,{nonull:!0});let d=[];for(const t of u){let o=null,p=null;try{o=JSON.parse(i.readFileSync(t,"utf-8")),p=await b(e),p=x.getImplementation(p).prepare(p,{noAdditionalProperties:a,allPropertiesRequired:n})}catch(e){d.push(E({errors:[g.create(e)]}));continue}l++,d.push(q((e=>A(p,o,e,{cwdToMappingFile:r,dirPathMapExternalExamples:c.dirname(t),ignoreFormats:s}).map((e=>Object.assign(e,{mapFilePath:c.normalize(t)}))))))}return o(d.reduce(((e,t)=>{return e?(o=t,E({errors:(r=e).errors.concat(o.errors),statistics:Object.entries(r.statistics).reduce(((e,[t,a])=>v===t?([r,o].forEach((t=>{const r=t.statistics[w][v].values();for(let t of r)e[w][v].add(t)})),e):(e[t]=a+o.statistics[t],e)),T())})):t;var r,o}),null),{statistics:{matchingFilePathsMapping:l}})}}},600:(e,t,r)=>{const o=r(273),a=r(474),s=/^3\./;e.exports={getImplementation:function(e){return"string"==typeof e.swagger?o:e.openapi&&e.openapi.match(s)?a:null}}},631:(e,t,r)=>{const{JSONPath:o}=r(940),a=r(258),s=r(804),n=r(402),i="$id",c="$..$ref",p="https://www.npmjs.com/package/openapi-examples-validator/defs.json";e.exports={getValidatorFactory:function(e,t){const r=function(e){const t={[i]:p};return o({path:c,json:e,callback(r){if(!r.startsWith("#"))return;const o=r.substring(1),s=a.get(e,o);a.set(t,o,s)}}),t}(e);return()=>{const e=new s(t);return n(e),e.addSchema(r),e}},compileValidate:function(e,t){const r=function(e){const t=Object.assign({},e);return t.$id="https://www.npmjs.com/package/openapi-examples-validator/schema.json",t}(t);let a;o({path:c,json:r,callback(e,t,r){e.startsWith("#")&&(r.parent[r.parentProperty]=`${p}${e}`)}});try{a=e.compile(r)}catch(e){a=()=>{},a.errors=[e]}return a}}},668:e=>{"use strict";e.exports=require("lodash.flatmap")},694:e=>{"use strict";e.exports=require("json-schema-ref-parser")},711:e=>{"use strict";e.exports=require("lodash.merge")},804:e=>{"use strict";e.exports=require("ajv-draft-04")},896:e=>{"use strict";e.exports=require("fs")},928:e=>{"use strict";e.exports=require("path")},935:(e,t,r)=>{const{JSONPath:o}=r(940),a=r(285);function s(e,t,r=a.path,s){return o({json:e,path:t,flatten:!0,resultType:r,callback:s})}e.exports={applyCallbackToAllObjectModels:function(e,t,r){const o=new Set;s(e,"$..schema..").forEach((e=>{(function(e){if(!e.match(/\['properties']$/))return;const t=e.match(/(?<!\['properties'])(\['properties']\['properties'])+$/);return!t||t.length%2!=0})(e)||o.add(e)})),function(e,t,r){r.forEach((r=>{s(e,r).forEach((e=>{for(const r of t)r.startsWith(e)&&t.delete(r)}))}))}(e,o,t);for(const t of o){const o=r(t);s(e,t,a.value,((e,t,r)=>{var a;(a=e)&&("object"===a.type||a.properties)&&o(e,t,r)}))}}}},940:e=>{"use strict";e.exports=require("jsonpath-plus")},984:e=>{"use strict";e.exports=require("lodash.clonedeep")}},t={},r=function r(o){var a=t[o];if(void 0!==a)return a.exports;var s=t[o]={exports:{}};return e[o](s,s.exports,r),s.exports}(497);module.exports=r})(); //# sourceMappingURL=index.js.map