-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rulesets): operation-success-response rule does not respect 2XX a…
…nd 3XX (#1793)
- Loading branch information
Showing
2 changed files
with
103 additions
and
16 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
42 changes: 26 additions & 16 deletions
42
packages/rulesets/src/oas/functions/oasOpSuccessResponse.ts
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 |
---|---|---|
@@ -1,22 +1,32 @@ | ||
import type { IFunction } from '@stoplight/spectral-core'; | ||
import { isObject } from './utils/isObject'; | ||
import { createRulesetFunction } from '@stoplight/spectral-core'; | ||
import { oas3 } from '@stoplight/spectral-formats'; | ||
|
||
export const oasOpSuccessResponse: IFunction = targetVal => { | ||
if (!isObject(targetVal)) { | ||
return; | ||
} | ||
export const oasOpSuccessResponse = createRulesetFunction<Record<string, unknown>, null>( | ||
{ | ||
input: { | ||
type: 'object', | ||
}, | ||
options: null, | ||
}, | ||
(input, opts, context) => { | ||
const isOAS3X = context.document.formats?.has(oas3) === true; | ||
|
||
for (const response of Object.keys(input)) { | ||
if (isOAS3X && (response === '2XX' || response === '3XX')) { | ||
return; | ||
} | ||
|
||
for (const response of Object.keys(targetVal)) { | ||
if (Number(response) >= 200 && Number(response) < 400) { | ||
return; | ||
if (Number(response) >= 200 && Number(response) < 400) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
return [ | ||
{ | ||
message: 'Operation must define at least a single 2xx or 3xx response', | ||
}, | ||
]; | ||
}; | ||
return [ | ||
{ | ||
message: 'Operation must define at least a single 2xx or 3xx response', | ||
}, | ||
]; | ||
}, | ||
); | ||
|
||
export default oasOpSuccessResponse; |