-
-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(rulesets): add new rule that requires sibling items field for type array #2632
Changes from 4 commits
55c976d
743e735
893b498
271d950
5bee343
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
lts/* | ||
18.18.2 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
||
import testRule from '../../__tests__/__helpers__/tester'; | ||
|
||
testRule('array-items', [ | ||
{ | ||
name: 'valid case', | ||
document: { | ||
swagger: '2.0', | ||
securityDefinitions: { | ||
apikey: {}, | ||
}, | ||
paths: { | ||
'/path': { | ||
get: { | ||
security: [ | ||
{ | ||
apikey: [], | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
|
||
{ | ||
name: 'array items sibling is present', | ||
document: { | ||
$ref: '#/', | ||
responses: { | ||
200: { | ||
type: 'array', | ||
items: {}, | ||
}, | ||
201: { | ||
type: 'array', | ||
items: { | ||
type: 'array', | ||
items: {}, | ||
}, | ||
}, | ||
}, | ||
openapi: '3.0.0', | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'array items sibling is missing', | ||
document: { | ||
$ref: '#/', | ||
responses: { | ||
200: { | ||
type: 'array', | ||
}, | ||
201: { | ||
type: 'array', | ||
items: { | ||
type: 'array', | ||
}, | ||
}, | ||
}, | ||
openapi: '3.0.0', | ||
}, | ||
errors: [ | ||
{ | ||
code: 'array-items', | ||
message: 'Schemas with "type: array", require a sibling "items" field', | ||
path: ['responses', '200'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
{ | ||
code: 'array-items', | ||
message: 'Schemas with "type: array", require a sibling "items" field', | ||
path: ['responses', '201', 'items'], | ||
severity: DiagnosticSeverity.Error, | ||
}, | ||
], | ||
}, | ||
]); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -361,6 +361,18 @@ const ruleset = { | |
function: refSiblings, | ||
}, | ||
}, | ||
'array-items': { | ||
formats: [oas3_0], | ||
message: 'Schemas with "type: array", require a sibling "items" field', | ||
severity: 0, | ||
recommended: true, | ||
resolved: false, | ||
given: "$..[?(@.type === 'array')]", | ||
then: { | ||
function: truthy, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this detect a child schema? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question! It should, I added a separate test for nested schemas: https://github.com/stoplightio/spectral/pull/2632/files#diff-7c7ca1323e671563e8696b6c8f4c6b7376a94a6cb93e08dfcec0774957b5a494R57-R63 |
||
field: 'items', | ||
}, | ||
}, | ||
'typed-enum': { | ||
description: 'Enum values must respect the specified type.', | ||
message: '{{error}}', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
====test==== | ||
Schemas with "type: array", require a sibling "items" field | ||
====document==== | ||
openapi: 3.0.3 | ||
info: | ||
title: test | ||
description: Test specification file | ||
version: '1.0' | ||
contact: | ||
name: John Doe | ||
url: 'https://example.com' | ||
email: john_doe@example.com | ||
license: | ||
name: Apache 2.0 | ||
url: 'https://www.apache.org/licenses/LICENSE-2.0' | ||
servers: | ||
- url: 'http://localhost:3000' | ||
tags: | ||
- name: list-endpoint | ||
description: Endpoint for listing objects | ||
paths: | ||
/users: | ||
get: | ||
summary: List Users | ||
operationId: get-users | ||
description: List all Users | ||
tags: | ||
- list-endpoint | ||
responses: | ||
'200': | ||
description: OK | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
favoriteColorSets: | ||
type: array | ||
items: | ||
type: array | ||
|
||
====asset:ruleset==== | ||
const { oas } = require('@stoplight/spectral-rulesets'); | ||
module.exports = oas; | ||
====command==== | ||
{bin} lint {document} --ruleset "{asset:ruleset}" | ||
====stdout==== | ||
{document} | ||
36:27 error array-items Schemas with "type: array", require a sibling "items" field paths./users.get.responses[200].content.application/json.schema.properties.favoriteColorSets.items | ||
|
||
✖ 1 problem (1 error, 0 warnings, 0 infos, 0 hints) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be 3.1 as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we discussed this with @mnaumanali94. We agreed to fix one version at a time. We can address 3.1 if there will be any interest in the community.