Skip to content

Commit

Permalink
fix: empty servers threw an error
Browse files Browse the repository at this point in the history
Fix #3
  • Loading branch information
philsturgeon committed Nov 14, 2022
1 parent a2fb440 commit e3c997f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
29 changes: 29 additions & 0 deletions __tests__/one-api-version-per-document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,35 @@ testRule("one-api-version-per-document", [
errors: [],
},

{
name: "valid case: no servers defined",
document: {
openapi: "3.1.0",
info: { version: "1.0" },
},
errors: [],
},

{
name: "valid case: empty servers defined",
document: {
openapi: "3.1.0",
info: { version: "1.0" },
servers: [],
},
errors: [],
},

{
name: "valid case: weird servers defined",
document: {
openapi: "3.1.0",
info: { version: "1.0" },
servers: {},
},
errors: [],
},

{
name: "invalid: multiple versions in directory",
document: {
Expand Down
8 changes: 6 additions & 2 deletions src/functions/onlyOneServerVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ export default createRulesetFunction({
options: {
type: ['null'],
},
}, (targetVal: any) => {
}, (targetVal: Array<any>) => {

if (! Array.isArray(targetVal)) {
return [];
}

const versionsMatches = targetVal.map(({ url }: any) => url.match(/([\\.|\\/|](v|version)?[0-9]{1,3}(?:\/)?)/i)[0]);

// If there are fewer than two versions mentioned there cannot be multiple versions
if (versionsMatches < 2) {
if (versionsMatches.length < 2) {
return [];
}

Expand Down

0 comments on commit e3c997f

Please sign in to comment.