forked from swagger-api/swagger-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve(validation): array schema
items
(swagger-api#1765)
* add failing tests * implement cross-version array schema validation
- Loading branch information
Showing
5 changed files
with
202 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export const validate2And3TypeArrayRequiresItems = () => (system) => { | ||
return system.validateSelectors | ||
.allSchemas() | ||
.then(nodes => { | ||
return nodes.reduce((acc, node) => { | ||
const schemaObj = node.node | ||
const { type, items } = schemaObj || {} | ||
if(type === "array" && typeof items === "undefined") { | ||
acc.push({ | ||
message: "Schemas with 'type: array', require a sibling 'items: ' field", | ||
path: node.path, | ||
level: "error", | ||
}) | ||
} else if(type === "array" && (typeof items !== "object" || Array.isArray(items))) { | ||
acc.push({ | ||
message: "`items` must be an object", | ||
path: [...node.path, "items"], | ||
level: "error", | ||
}) | ||
} | ||
return acc | ||
}, []) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import expect from "expect" | ||
import validateHelper from "../validate-helper.js" | ||
|
||
describe(`validation plugin - semantic - 2and3 schemas`, () => { | ||
describe(`array schemas must have an Object value in "items"`, () => { | ||
it("should return an error for an array items value in Swagger 2", () => { | ||
const spec = { | ||
swagger: "2.0", | ||
"paths": { | ||
"/pets": { | ||
"get": { | ||
"parameters": [ | ||
{ | ||
name: "myParam", | ||
in: "query", | ||
type: "array", | ||
items: [{ type: "object" }] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
|
||
return validateHelper(spec) | ||
.then(system => { | ||
const allErrors = system.errSelectors.allErrors().toJS() | ||
const firstError = allErrors[0] | ||
expect(allErrors.length).toEqual(1) | ||
expect(firstError.path).toEqual(["paths", "/pets", "get", "parameters", "0", "items"]) | ||
expect(firstError.message).toEqual("`items` must be an object") | ||
}) | ||
}) | ||
it("should return an error for an array items value in OpenAPI 3", () => { | ||
const spec = { | ||
openapi: "3.0.0", | ||
"paths": { | ||
"/pets": { | ||
"get": { | ||
"parameters": [ | ||
{ | ||
name: "myParam", | ||
in: "query", | ||
schema: { | ||
type: "array", | ||
items: [{ type: "object" }] | ||
} | ||
}, | ||
] | ||
} | ||
} | ||
} | ||
} | ||
|
||
return validateHelper(spec) | ||
.then(system => { | ||
const allErrors = system.errSelectors.allErrors().toJS() | ||
const firstError = allErrors[0] | ||
expect(allErrors.length).toEqual(1) | ||
expect(firstError.path).toEqual(["paths", "/pets", "get", "parameters", "0", "schema", "items"]) | ||
expect(firstError.message).toEqual("`items` must be an object") | ||
}) | ||
}) | ||
it("should return an error for a missing items value for an array schema in Swagger 2", () => { | ||
const spec = { | ||
swagger: "2.0", | ||
"paths": { | ||
"/pets": { | ||
"get": { | ||
"parameters": [ | ||
{ | ||
name: "myParam", | ||
in: "query", | ||
type: "array" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
|
||
return validateHelper(spec) | ||
.then(system => { | ||
const allErrors = system.errSelectors.allErrors().toJS() | ||
const firstError = allErrors[0] | ||
expect(allErrors.length).toEqual(1) | ||
expect(firstError.message).toEqual("Schemas with 'type: array', require a sibling 'items: ' field") | ||
expect(firstError.path).toEqual(["paths", "/pets", "get", "parameters", "0"]) | ||
}) | ||
}) | ||
it("should return an error for a missing items value for an array schema in OpenAPI 3", () => { | ||
const spec = { | ||
openapi: "3.0.0", | ||
"paths": { | ||
"/pets": { | ||
"get": { | ||
"parameters": [ | ||
{ | ||
name: "myParam", | ||
in: "query", | ||
schema: { | ||
type: "array" | ||
} | ||
}, | ||
] | ||
} | ||
} | ||
} | ||
} | ||
|
||
return validateHelper(spec) | ||
.then(system => { | ||
const allErrors = system.errSelectors.allErrors().toJS() | ||
const firstError = allErrors[0] | ||
expect(allErrors.length).toEqual(1) | ||
expect(firstError.path).toEqual(["paths", "/pets", "get", "parameters", "0", "schema"]) | ||
expect(firstError.message).toEqual("Schemas with 'type: array', require a sibling 'items: ' field") | ||
}) | ||
}) | ||
it("should not return an error for a missing items value for a non-array schema in Swagger 2", () => { | ||
const spec = { | ||
swagger: "2.0", | ||
"paths": { | ||
"/pets": { | ||
"get": { | ||
"parameters": [ | ||
{ | ||
name: "myParam", | ||
in: "query", | ||
type: "object" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
|
||
return validateHelper(spec) | ||
.then(system => { | ||
const allErrors = system.errSelectors.allErrors().toJS() | ||
expect(allErrors.length).toEqual(0) | ||
}) | ||
}) | ||
it("should not return an error for a missing items value for a non-array schema in OpenAPI 3", () => { | ||
const spec = { | ||
openapi: "3.0.0", | ||
"paths": { | ||
"/pets": { | ||
"get": { | ||
"parameters": [ | ||
{ | ||
name: "myParam", | ||
in: "query", | ||
schema: { | ||
type: "object" | ||
} | ||
}, | ||
] | ||
} | ||
} | ||
} | ||
} | ||
|
||
return validateHelper(spec) | ||
.then(system => { | ||
const allErrors = system.errSelectors.allErrors().toJS() | ||
expect(allErrors.length).toEqual(0) | ||
}) | ||
}) | ||
}) | ||
}) |