-
Notifications
You must be signed in to change notification settings - Fork 20
feat: add parameter validation rules #4986
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
Open
robert-hebel-sb
wants to merge
9
commits into
main
Choose a base branch
from
feat/swg--15850--port-parameters-validation-rules
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+619
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
76a3eb5
feat: migrate unique parameter name
robert-hebel-sb 2da94c7
feat: migrate unique parameter name
robert-hebel-sb 555703b
feat: migrate parameter named "Authorization" is ignored rule
robert-hebel-sb 46f0f7a
feat: migrate parameter named "content-type" is ignored rule
robert-hebel-sb 028ab12
feat: migrate parameter named "accept" is ignored rule
robert-hebel-sb f2c2bbd
feat: migrate multiple body rule
robert-hebel-sb 21fe90c
fix: adjust multiple body rule
robert-hebel-sb 1eb1a38
fix: parameter unique name validation for oas2
robert-hebel-sb 1581750
refactor: improve typings
robert-hebel-sb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
31 changes: 31 additions & 0 deletions
31
packages/apidom-ls/src/config/openapi/parameter/lint/in--accept.ts
This file contains hidden or 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,31 @@ | ||
import { DiagnosticSeverity } from 'vscode-languageserver-types'; | ||
|
||
import { OpenAPI3 } from '../../target-specs.ts'; | ||
import ApilintCodes from '../../../codes.ts'; | ||
|
||
const inAcceptLint = { | ||
code: ApilintCodes.OPENAPI3_0_PARAMETER_FIELD_IN_CONTENT_TYPE, | ||
source: 'apilint', | ||
message: | ||
'Header Parameter named "Accept" is ignored. The values for the "Accept" header are defined by `requestBody.content.<media-type>`', | ||
severity: DiagnosticSeverity.Warning, | ||
linterParams: ['^(?!\\b(A|a)ccept\\b).*$'], | ||
linterFunction: 'apilintValueRegex', | ||
marker: 'value', | ||
target: 'name', | ||
data: {}, | ||
conditions: [ | ||
{ | ||
targets: [ | ||
{ | ||
path: 'in', | ||
}, | ||
], | ||
function: 'apilintContainsValue', | ||
params: ['header'], | ||
}, | ||
], | ||
targetSpecs: OpenAPI3, | ||
}; | ||
|
||
export default inAcceptLint; |
31 changes: 31 additions & 0 deletions
31
packages/apidom-ls/src/config/openapi/parameter/lint/in--authorization.ts
This file contains hidden or 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,31 @@ | ||
import { DiagnosticSeverity } from 'vscode-languageserver-types'; | ||
|
||
import { OpenAPI3 } from '../../target-specs.ts'; | ||
import ApilintCodes from '../../../codes.ts'; | ||
|
||
const inAuthorizationLint = { | ||
code: ApilintCodes.OPENAPI3_0_PARAMETER_FIELD_IN_AUTHORIZATION, | ||
source: 'apilint', | ||
message: | ||
'Header Parameter named "Authorization" is ignored. Use the "securitySchemes" and "security" sections instead to define authorization', | ||
severity: DiagnosticSeverity.Warning, | ||
linterParams: ['^(?!\\b(A|a)uthorization\\b).*$'], | ||
linterFunction: 'apilintValueRegex', | ||
marker: 'value', | ||
target: 'name', | ||
data: {}, | ||
conditions: [ | ||
{ | ||
targets: [ | ||
{ | ||
path: 'in', | ||
}, | ||
], | ||
function: 'apilintContainsValue', | ||
params: ['header'], | ||
}, | ||
], | ||
targetSpecs: OpenAPI3, | ||
}; | ||
|
||
export default inAuthorizationLint; |
31 changes: 31 additions & 0 deletions
31
packages/apidom-ls/src/config/openapi/parameter/lint/in--content-type.ts
This file contains hidden or 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,31 @@ | ||
import { DiagnosticSeverity } from 'vscode-languageserver-types'; | ||
|
||
import { OpenAPI3 } from '../../target-specs.ts'; | ||
import ApilintCodes from '../../../codes.ts'; | ||
|
||
const inContentTypeLint = { | ||
code: ApilintCodes.OPENAPI3_0_PARAMETER_FIELD_IN_CONTENT_TYPE, | ||
source: 'apilint', | ||
message: | ||
'Header Parameter named "Content-Type" is ignored. The values for the "Content-Type" header are defined by `requestBody.content.<media-type>`', | ||
severity: DiagnosticSeverity.Warning, | ||
linterParams: ['^(?!\\b(C|c)ontent-(T|t)ype\\b).*$'], | ||
linterFunction: 'apilintValueRegex', | ||
marker: 'value', | ||
target: 'name', | ||
data: {}, | ||
conditions: [ | ||
{ | ||
targets: [ | ||
{ | ||
path: 'in', | ||
}, | ||
], | ||
function: 'apilintContainsValue', | ||
params: ['header'], | ||
}, | ||
], | ||
targetSpecs: OpenAPI3, | ||
}; | ||
|
||
export default inContentTypeLint; |
27 changes: 27 additions & 0 deletions
27
packages/apidom-ls/src/config/openapi/parameter/lint/in--multiple-body.ts
This file contains hidden or 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,27 @@ | ||
import { DiagnosticSeverity } from 'vscode-languageserver-types'; | ||
|
||
import { OpenAPI2 } from '../../target-specs.ts'; | ||
import ApilintCodes from '../../../codes.ts'; | ||
|
||
const inMultipleBody = { | ||
code: ApilintCodes.OPENAPI2_PARAMETER_FIELD_IN_MULTIPLE_BODY, | ||
source: 'apilint', | ||
message: 'Multiple body parameters are not allowed', | ||
severity: DiagnosticSeverity.Error, | ||
linterFunction: 'apilintPropertyUniqueSiblingValue', | ||
linterParams: ['parameters', 'in'], | ||
marker: 'key', | ||
markerTarget: 'in', | ||
target: 'in', | ||
conditions: [ | ||
{ | ||
targets: [{ path: 'in' }], | ||
function: 'apilintContainsValue', | ||
params: ['body'], | ||
}, | ||
], | ||
data: {}, | ||
targetSpecs: OpenAPI2, | ||
}; | ||
|
||
export default inMultipleBody; |
This file contains hidden or 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
21 changes: 21 additions & 0 deletions
21
packages/apidom-ls/src/config/openapi/parameter/lint/unique-name.ts
This file contains hidden or 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,21 @@ | ||
import { DiagnosticSeverity } from 'vscode-languageserver-types'; | ||
|
||
import ApilintCodes from '../../../codes.ts'; | ||
import { LinterMeta } from '../../../../apidom-language-types.ts'; | ||
import { OpenAPI2, OpenAPI3 } from '../../target-specs.ts'; | ||
|
||
const parametersTypeLint: LinterMeta = { | ||
code: ApilintCodes.OPENAPI2_PARAMETER_FIELD_NAME_UNIQUE, | ||
source: 'apilint', | ||
message: 'Name must be unique among all parameters', | ||
severity: DiagnosticSeverity.Error, | ||
linterFunction: 'apilintPropertyUniqueSiblingValue', | ||
linterParams: ['parameters', 'name'], | ||
marker: 'key', | ||
markerTarget: 'name', | ||
target: 'name', | ||
data: {}, | ||
targetSpecs: [...OpenAPI2, ...OpenAPI3], | ||
}; | ||
|
||
export default parametersTypeLint; |
This file contains hidden or 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 |
---|---|---|
|
@@ -795,6 +795,30 @@ export const standardLinterfunctions: FunctionItem[] = [ | |
return true; | ||
}, | ||
}, | ||
{ | ||
functionName: 'apilintPropertyUniqueSiblingValue', | ||
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. sorry for the late reply, instead of having new linter, could the 'apilintPropertyUniqueValue' be used ? |
||
function: (element, elementOrClasses, key) => { | ||
const value = toValue(element); | ||
|
||
const filterSiblingsOAS2 = ( | ||
el: Element & { key?: { content?: string }; content: { value?: string } }, | ||
) => isString(el) && el.key?.content === key && toValue(el.content.value) === value; | ||
|
||
const filterSiblingsOAS3 = (el: Element) => | ||
isObject(el) && el.hasKey(key) && toValue(el.get(key)) === value; | ||
|
||
const elements = filter((el: Element) => { | ||
const classes: string[] = toValue(el.getMetaProperty('classes', [])); | ||
|
||
return ( | ||
(elementOrClasses.includes(el.element) || | ||
classes.every((v) => elementOrClasses.includes(v))) && | ||
(filterSiblingsOAS2(el) || filterSiblingsOAS3(el)) | ||
); | ||
}, element.parent?.parent?.parent); | ||
return elements.length <= 1; | ||
}, | ||
}, | ||
{ | ||
functionName: 'apilintChannelParameterExist', | ||
function: (element: Element): boolean => { | ||
|
15 changes: 15 additions & 0 deletions
15
packages/apidom-ls/test/fixtures/validation/oas/parameter-in-accept-3-0.yaml
This file contains hidden or 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,15 @@ | ||
openapi: 3.0.0 | ||
info: | ||
version: 1.0.0 | ||
title: 'test' | ||
paths: | ||
/: | ||
get: | ||
responses: | ||
default: | ||
description: 'test' | ||
parameters: | ||
- in: header | ||
name: accept | ||
content: | ||
application/xml: {} |
15 changes: 15 additions & 0 deletions
15
packages/apidom-ls/test/fixtures/validation/oas/parameter-in-authorization-3-0.yaml
This file contains hidden or 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,15 @@ | ||
openapi: 3.0.0 | ||
info: | ||
version: 1.0.0 | ||
title: 'test' | ||
paths: | ||
/: | ||
get: | ||
responses: | ||
default: | ||
description: 'test' | ||
parameters: | ||
- in: header | ||
name: Authorization | ||
content: | ||
application/xml: {} |
15 changes: 15 additions & 0 deletions
15
packages/apidom-ls/test/fixtures/validation/oas/parameter-in-content-type-3-0.yaml
This file contains hidden or 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,15 @@ | ||
openapi: 3.0.0 | ||
info: | ||
version: 1.0.0 | ||
title: 'test' | ||
paths: | ||
/: | ||
get: | ||
responses: | ||
default: | ||
description: 'test' | ||
parameters: | ||
- in: header | ||
name: content-type | ||
content: | ||
application/xml: {} |
19 changes: 19 additions & 0 deletions
19
packages/apidom-ls/test/fixtures/validation/oas/parameter-in-multiple-body.yaml
This file contains hidden or 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,19 @@ | ||
swagger: '2.0' | ||
info: | ||
title: 'test' | ||
version: 1.0.0 | ||
paths: | ||
/: | ||
get: | ||
responses: | ||
default: | ||
description: 'test' | ||
consumes: | ||
- multipart/form-data | ||
parameters: | ||
- in: body | ||
name: Else1 | ||
schema: {} | ||
- in: body | ||
name: Else2 | ||
schema: {} |
21 changes: 21 additions & 0 deletions
21
packages/apidom-ls/test/fixtures/validation/oas/parameter-unique-name-2-0.yaml
This file contains hidden or 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,21 @@ | ||
swagger: "2.0" | ||
info: | ||
version: 1.0.0 | ||
title: 'test' | ||
paths: | ||
/pets: | ||
get: | ||
responses: | ||
parameters: | ||
- name: pathLevel | ||
type: string | ||
in: query | ||
description: tags to filter by | ||
schema: | ||
type: string | ||
- name: pathLevel | ||
type: string | ||
in: query | ||
description: tags to filter by | ||
schema: | ||
type: string |
19 changes: 19 additions & 0 deletions
19
packages/apidom-ls/test/fixtures/validation/oas/parameter-unique-name-3-0.yaml
This file contains hidden or 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,19 @@ | ||
openapi: 3.0.0 | ||
info: | ||
version: 1.0.0 | ||
title: 'test' | ||
paths: | ||
/pets: | ||
get: | ||
responses: | ||
parameters: | ||
- name: pathLevel | ||
in: query | ||
description: tags to filter by | ||
schema: | ||
type: string | ||
- name: pathLevel | ||
in: query | ||
description: tags to filter by | ||
schema: | ||
type: string |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
is the empty data needed ?
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.
I see it's a default in a case when we don't specify any quickfix rules