-
Notifications
You must be signed in to change notification settings - Fork 9
/
contract.js
207 lines (206 loc) · 6.45 KB
/
contract.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* Copyright 2022 Cisco Systems, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
import {
oas2, oas3,
} from '@stoplight/spectral-formats';
import { oas } from '@stoplight/spectral-rulesets';
import completedSchema from './functions/completedSchema.js';
import ensureField from './functions/ensureField.js';
import includeAll from './functions/includeAll.js';
import keyMatchAnyPattern from './functions/keyMatchAnyPattern.js';
import multiVersion from './functions/multiVersion.js';
import operationIdCheck from './functions/operationIdCheck.js';
export default {
'extends': [
[
oas,
'off',
],
],
'rules': {
'oas3-schema': 'error',
'oas2-schema': 'error',
'oas3-missing-schema-definition': {
'description': 'There is no schema attribute for a component.',
'message': '{{description}}; {{error}}',
'severity': 'error',
'formats': [oas3],
'given': [
'$.paths.*.*.responses[?(@property.match(/^[2]\\d{2}$/i) && @property!=204)].content[*]',
'$.*.*.*.requestBody.content[*]',
],
'then': {
'function': ensureField,
'functionOptions': {
'field': 'schema',
},
},
},
'oas2-missing-schema-definition': {
'description': 'There is no schema attribute for a component.',
'message': '{{description}}; {{error}}',
'severity': 'error',
'formats': [oas2],
'given': [
'$.paths.*.*.responses[?(@property.match(/^[2]\\d{2}$/i) && @property!=204 )]',
"$.*.*.*.parameters[?(@ != null && @.in === 'body')]",
],
'then': {
'function': ensureField,
'functionOptions': {
'field': 'schema',
},
},
},
'general-schema-definition': {
'description': 'Some of the defined schema use object as a final field when describing their object structure',
'message': '{{description}}; {{error}}',
'severity': 'error',
'given': [
'$.paths.*.*.*.*.content[?(@ && @.schema)].schema',
'$.paths.*.*.*.content[?(@ && @.schema)].schema',
'$.paths.*.*.responses[*].headers[?(@ && @.schema)].schema',
'$.paths.*.*.parameters[?(@ && @.schema)].schema',
'$.paths.*.*.responses[?(@ && @.schema)].schema',
"$.paths.*.*.parameters[?(@ && @.schema && @.in === 'body')].schema",
],
'then': [
{
'function': completedSchema,
},
],
},
'oas3-missing-returned-representation': {
'description': '2XX (except 204) responses must have a response schema defined',
'message': '{{description}}; {{error}}',
'severity': 'error',
'formats': [oas3],
'given': ['$.paths.*.*.responses[?(@property.match(/^[2]\\d{2}$/i) && @property!=204)].content[*]'],
'then': {
'function': ensureField,
'functionOptions': {
'field': 'schema',
},
},
},
'oas2-missing-returned-representation': {
'description': '2XX (except 204) responses must have a response schema defined.',
'message': '{{description}}; {{error}}',
'severity': 'error',
'formats': [oas2],
'given': ['$.paths.*.*.responses[?(@property.match(/^[2]\\d{2}$/i) && @property!=204 )]'],
'then': {
'function': ensureField,
'functionOptions': {
'field': 'schema',
},
},
},
'success-status-code': {
'description': 'For every operation in the OAS document, there should be at least one success status code defined. A successful status code is in the 1xx, 2xx or 3xx range series, and generally a 200, 201 or 204.',
'message': '{{description}}; {{error}}',
'severity': 'error',
'given': '$.paths.*.*.responses',
'then': {
'function': keyMatchAnyPattern,
'functionOptions': {
'patterns': ['/^([123]\\d{2}|default)$/'],
},
},
},
'error-status-code': {
'description': 'responses should include at least one error status code (4xx or 5xx)',
'message': '{{description}}; {{error}}',
'severity': 'warn',
'given': '$.paths.*.*.responses',
'then': {
'function': keyMatchAnyPattern,
'functionOptions': {
'patterns': [
'/^4\\d{2}$/',
'/^5\\d{2}$/',
'/^default$/',
],
},
},
},
'oas2-meta-info': {
'description': 'Some meta fields must be present.',
'message': '{{description}}; {{error}}',
'severity': 'error',
'formats': [oas2],
'type': 'validation',
'given': '$',
'then': {
'function': includeAll,
'functionOptions': {
'values': [
'info',
'swagger',
'info.title',
'info.version',
'basePath',
'consumes',
'securityDefinitions',
],
},
},
},
'multi-versions-server-url-missing-version': {
'description': 'path should not have version while server object has one',
'message': '{{description}}; {{error}}',
'severity': 'warn',
'given': [
'$',
],
'then': {
'function': multiVersion,
'functionOptions': {
'check': 'server-url-missing'
},
},
},
'multi-versions': {
'description': 'should only contain a single API version at a time.',
'message': '{{description}}; {{error}}',
'severity': 'error',
'given': [
'$',
],
'then': {
'function': multiVersion,
'functionOptions': {
'exceptions' : [ "networks/{networkId}/switch/dhcp/v4" ]
},
},
},
'operationId-required-and-unique': {
'description': 'operationId must be required and uniq',
'message': '{{description}}; {{error}}',
'severity': 'error',
'given': [
'$',
],
'then': [
{
'function': operationIdCheck,
},
],
},
},
};