UNPKG

openapi-examples-validator

Version:
2 lines 11.1 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 a=r(984),{setAllPropertiesRequired:o}=r(299),{setNoAdditionalProperties:s}=r(305);function n(){return["$..examples[?(@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=a(e);return t&&s(n,["$..examples[?(@property.match(/[/+]json/))]"]),r&&o(n,["$..examples[?(@property.match(/[/+]json/))]"]),n}}},285:e=>{e.exports={parent:"parent",parentProperty:"parentProperty",path:"path",pointer:"pointer",value:"value"}},299:(e,t,r)=>{const{applyCallbackToAllObjectModels:a}=r(935);e.exports={setAllPropertiesRequired:function(e,t=[]){a(e,t,(()=>e=>{e.hasOwnProperty("properties")&&(e.required=Object.keys(e.properties))}))}}},305:(e,t,r)=>{const{applyCallbackToAllObjectModels:a}=r(935);e.exports={setNoAdditionalProperties:function(e,t=[]){const r=new RegExp("(?<!\\['properties'\\])\\['(?:"+o.join("|")+")'\\]");a(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.`):o.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 o=["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 a=r(928),o=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(a.dirname(e));const s=await o.dereference(t);return process.chdir(r),s}}},474:(e,t,r)=>{const a=r(984),{ApplicationError:o,ErrorType:s}=r(476),{setAllPropertiesRequired:n}=r(299),{setNoAdditionalProperties:i}=r(305),c="$..responses..content[?(@property.match(/[/+]json/))]",p="$..requestBody.content[?(@property.match(/[/+]json/))]",l=".example",u=".examples.*.value",d=`${c}${l}`,m=`${c}${u}`,h=`${p}${l}`,f=`${p}${u}`,x="single";function y(){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:a,exampleType:n}=function(e){const t=e.split("/"),r=t.lastIndexOf("example"),a=r>-1?x:"multi",o=a===x?r:t.lastIndexOf("examples");return t.splice(o,t.length-o,"schema"),{exampleType:a,pathSchemaAsArray:t}}(r),i=a.join("/"),c=t.get(i);return c&&c!==n&&function(e){const t=e.slice(0,e.length-1);throw o.create({type:s.errorAndErrorsMutuallyExclusive,message:'Properties "error" and "errors" are mutually exclusive',params:{pathContext:t.join("/")}})}(a),t.set(i,n),e[i]=(e[i]||new Set).add(r),e}),{})},getJsonPathsToExamples:y,prepare:function(e,{noAdditionalProperties:t,allPropertiesRequired:r}={}){const o=a(e);return t&&i(o,y()),r&&n(o,y()),o}}},476:(e,t,r)=>{const a=r(711),{ENOENT:o}=r(184).code,s={jsENOENT:o.code,jsonPathNotFound:"JsonPathNotFound",errorAndErrorsMutuallyExclusive:"ErrorErrorsMutuallyExclusive",parseError:"ParseError",validation:"Validation"};class n{static create(e){const{code:t,message:r,path:o,cause:i}=e,c=t||e.type||s.validation,p={message:r};return s.validation===c||s.errorAndErrorsMutuallyExclusive===c?a(p,e):(o&&a(p,{params:{path:o}}),i&&a(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 a=r(711),o=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:x}=r(631),y=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:a}={}){const o=y.getImplementation(e);e=await d.dereference(e),e=o.prepare(e,{noAdditionalProperties:t,allPropertiesRequired:a});let s=o.getJsonPathsToExamples().reduce(((t,r)=>t.concat(S(r,e))),[]);return function({impl:e},t,r,{ignoreFormats:a}){const o=T(),s={valid:!0,statistics:o,errors:[]},n=V(r,{ignoreFormats:a});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:a,statistics:o,validationResult:s}){const n=s.errors;a[r].forEach((a=>{const i=M(a,e),c=J(r,e,!0),p=R({createValidator:t,schema:c,example:i,statistics:o}).map((e=>(e.examplePath=a,e)));p.length&&(s.valid=!1,n.splice(n.length-1,0,...p))}))}({openapiSpec:r,createValidator:n,schemaPointer:e,validationMap:i,statistics:o,validationResult:s})})),s}({impl:o},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:a=!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(o([u]),(t=>{let o=[];try{const e=a?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)o.push({path:c.normalize(e),content:JSON.parse(i.readFileSync(e,"utf-8"))})}catch(e){return[g.create(e)]}return s(o,(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:a,filePathExample:o}){const s=[];if(a.examplesTotal++,!t)return a.examplesWithoutSchema++,s;a[w][v].add(t);const n=x(e(),t);return n(r)?s:s.concat(...n.errors.map(g.create)).map((e=>o?(e.exampleFilePath=o,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 a=M(e,t);return r||a||k(e),a}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:a}={}){let o=null;try{o=await b(e)}catch(e){return E({errors:[g.create(e)]})}return $(o,{noAdditionalProperties:t,ignoreFormats:r,allPropertiesRequired:a})},validateExample:async function(e,t,r,{noAdditionalProperties:a,ignoreFormats:o,allPropertiesRequired:s}={}){let n=null,c=null,p=null;try{n=JSON.parse(i.readFileSync(r,"utf-8")),p=await b(e),p=y.getImplementation(p).prepare(p,{noAdditionalProperties:a,allPropertiesRequired:s}),c=J(N(t,p),p)}catch(e){return E({errors:[g.create(e)]})}return q((e=>R({createValidator:V(p,{ignoreFormats:o}),schema:c,example:n,statistics:e,filePathExample:r})))},validateExamplesByMap:async function(e,t,{cwdToMappingFile:r,noAdditionalProperties:o,ignoreFormats:s,allPropertiesRequired:n}={}){let l=0;const u=p.sync(t,{nonull:!0});let d=[];for(const t of u){let a=null,p=null;try{a=JSON.parse(i.readFileSync(t,"utf-8")),p=await b(e),p=y.getImplementation(p).prepare(p,{noAdditionalProperties:o,allPropertiesRequired:n})}catch(e){d.push(E({errors:[g.create(e)]}));continue}l++,d.push(q((e=>A(p,a,e,{cwdToMappingFile:r,dirPathMapExternalExamples:c.dirname(t),ignoreFormats:s}).map((e=>Object.assign(e,{mapFilePath:c.normalize(t)}))))))}return a(d.reduce(((e,t)=>{return e?(a=t,E({errors:(r=e).errors.concat(a.errors),statistics:Object.entries(r.statistics).reduce(((e,[t,o])=>v===t?([r,a].forEach((t=>{const r=t.statistics[w][v].values();for(let t of r)e[w][v].add(t)})),e):(e[t]=o+a.statistics[t],e)),T())})):t;var r,a}),null),{statistics:{matchingFilePathsMapping:l}})}}},600:(e,t,r)=>{const a=r(273),o=r(474),s=/^3\./;e.exports={getImplementation:function(e){return"string"==typeof e.swagger?a:e.openapi&&e.openapi.match(s)?o:null}}},631:(e,t,r)=>{const{JSONPath:a}=r(940),o=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 a({path:c,json:e,callback(r){if(!r.startsWith("#"))return;const a=r.substring(1),s=o.get(e,a);o.set(t,a,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 o;a({path:c,json:r,callback(e,t,r){e.startsWith("#")&&(r.parent[r.parentProperty]=`${p}${e}`)}});try{o=e.compile(r)}catch(e){o=()=>{},o.errors=[e]}return o}}},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:a}=r(940),o=r(285);function s(e,t,r=o.path,s){return a({json:e,path:t,flatten:!0,resultType:r,callback:s})}e.exports={applyCallbackToAllObjectModels:function(e,t,r){const a=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)||a.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,a,t);for(const t of a){const a=r(t);s(e,t,o.value,((e,t,r)=>{var o;(o=e)&&("object"===o.type||o.properties)&&a(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(a){var o=t[a];if(void 0!==o)return o.exports;var s=t[a]={exports:{}};return e[a](s,s.exports,r),s.exports}(497);module.exports=r})(); //# sourceMappingURL=index.js.map