Skip to content

Commit 7a8cc0e

Browse files
feat(rulesets): add oas3_1-servers-in-webhook and oas3_1-callbacks-in… (#2581)
* feat(rulesets): add oas3_1-servers-in-webhook and oas3_1-callbacks-in-webhook rules * feat(rulesets): fix oas3_1-servers-in-webhook and oas3_1-callbacks-in-webhook rules * feat(rulesets): fix lint in openapi-rules.md * feat(rulesets): check servers array at webhook operation level * feat(rulesets): show warning if callbacks defined in a callback --------- Co-authored-by: Nauman <[email protected]>
1 parent cd4510e commit 7a8cc0e

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed

docs/reference/openapi-rules.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,3 +920,66 @@ servers:
920920
```
921921

922922
In this example, both **`{region}`** and **`{version}`** variables are properly defined and used in the server URL. Also, the default value for **`region`** is within the allowed values.
923+
924+
### oas3_callbacks_in_callbacks
925+
926+
A callback should not be defined within another callback.
927+
928+
**Recommended:** Yes
929+
930+
**Bad Example**
931+
932+
```yaml
933+
paths:
934+
/path:
935+
get:
936+
callbacks:
937+
onData:
938+
/data:
939+
post:
940+
callbacks: ...
941+
```
942+
943+
### oas3_1-servers-in-webhook
944+
945+
Servers should not be defined in a webhook.
946+
947+
**Recommended:** Yes
948+
949+
**Bad Example**
950+
951+
At the path item object level:
952+
953+
```yaml
954+
webhooks:
955+
servers:
956+
- url: https://example.com/
957+
- url: https://example.com/api/
958+
```
959+
960+
or
961+
962+
At the operation level:
963+
964+
```yaml
965+
webhooks:
966+
newPet:
967+
post:
968+
servers:
969+
-url: https://example.com/
970+
```
971+
972+
### oas3_1-callbacks-in-webhook
973+
974+
Callbacks should not be defined in a webhook.
975+
976+
**Recommended:** Yes
977+
978+
**Bad Example**
979+
980+
```yaml
981+
webhooks:
982+
newPet:
983+
post:
984+
callbacks: ...
985+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { DiagnosticSeverity } from '@stoplight/types';
2+
import testRule from '../../__tests__/__helpers__/tester';
3+
4+
testRule('oas3-callbacks-in-callbacks', [
5+
{
6+
name: 'callbacks defined within a callback',
7+
document: {
8+
openapi: '3.0.0',
9+
paths: {
10+
'/path': {
11+
get: {
12+
callbacks: {
13+
onData: {
14+
'/data': {
15+
post: {
16+
callbacks: {},
17+
},
18+
},
19+
},
20+
},
21+
},
22+
},
23+
},
24+
},
25+
errors: [
26+
{
27+
message: 'Callbacks should not be defined within a callback',
28+
path: ['paths', '/path', 'get', 'callbacks', 'onData', '/data', 'post', 'callbacks'],
29+
severity: DiagnosticSeverity.Warning,
30+
},
31+
],
32+
},
33+
]);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { DiagnosticSeverity } from '@stoplight/types';
2+
import testRule from '../../__tests__/__helpers__/tester';
3+
4+
testRule('oas3_1-callbacks-in-webhook', [
5+
{
6+
name: 'callbacks defined in webhook',
7+
document: {
8+
openapi: '3.1.0',
9+
webhooks: {
10+
newPet: {
11+
post: {
12+
callbacks: {},
13+
},
14+
},
15+
},
16+
},
17+
errors: [
18+
{
19+
message: 'Callbacks should not be defined in a webhook.',
20+
path: ['webhooks', 'newPet', 'post', 'callbacks'],
21+
severity: DiagnosticSeverity.Warning,
22+
},
23+
],
24+
},
25+
]);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { DiagnosticSeverity } from '@stoplight/types';
2+
import testRule from '../../__tests__/__helpers__/tester';
3+
4+
testRule('oas3_1-servers-in-webhook', [
5+
{
6+
name: 'servers defined in webhook',
7+
document: {
8+
openapi: '3.1.0',
9+
webhooks: {
10+
servers: [],
11+
newPet: {
12+
post: {
13+
servers: [],
14+
},
15+
},
16+
},
17+
},
18+
errors: [
19+
{
20+
message: 'Servers should not be defined in a webhook.',
21+
path: ['webhooks', 'servers'],
22+
severity: DiagnosticSeverity.Warning,
23+
},
24+
{
25+
message: 'Servers should not be defined in a webhook.',
26+
path: ['webhooks', 'newPet', 'post', 'servers'],
27+
severity: DiagnosticSeverity.Warning,
28+
},
29+
],
30+
},
31+
]);

packages/rulesets/src/oas/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,5 +707,32 @@ const ruleset = {
707707
},
708708
},
709709
},
710+
'oas3-callbacks-in-callbacks': {
711+
message: 'Callbacks should not be defined within a callback',
712+
formats: [oas3],
713+
recommended: true,
714+
given: ['#OperationObject.callbacks[*][*][*].callbacks'],
715+
then: {
716+
function: undefined,
717+
},
718+
},
719+
'oas3_1-servers-in-webhook': {
720+
message: 'Servers should not be defined in a webhook.',
721+
formats: [oas3_1],
722+
recommended: true,
723+
given: ['$.webhooks.servers', '$.webhooks[*][*].servers'],
724+
then: {
725+
function: undefined,
726+
},
727+
},
728+
'oas3_1-callbacks-in-webhook': {
729+
message: 'Callbacks should not be defined in a webhook.',
730+
formats: [oas3_1],
731+
recommended: true,
732+
given: ['$.webhooks[*][*].callbacks'],
733+
then: {
734+
function: undefined,
735+
},
736+
},
710737
},
711738
};

0 commit comments

Comments
 (0)