-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInvalidOpenAPI.php
More file actions
449 lines (375 loc) · 12.2 KB
/
InvalidOpenAPI.php
File metadata and controls
449 lines (375 loc) · 12.2 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<?php
declare(strict_types=1);
namespace Membrane\OpenAPIReader\Exception;
use Membrane\OpenAPIReader\ValueObject\Valid\Enum\Type;
use Membrane\OpenAPIReader\ValueObject\Valid\Identifier;
use RuntimeException;
/*
* This exception occurs if your Open API is invalid according to the OpenAPI Specification
*/
final class InvalidOpenAPI extends RuntimeException
{
public static function missingRequiredField(
Identifier $identifier,
string $requiredField,
): self {
return new InvalidOpenAPI(<<<TEXT
$identifier
- Missing required field "$requiredField"
TEXT);
}
public static function missingInfo(): self
{
$message = <<<TEXT
An OpenAPI Object MUST contain an Info Object.
That Info Object MUST contain a "title" and "version".
TEXT;
return new self($message);
}
public static function missingOpenAPIVersion(Identifier $identifier): self
{
$message = <<<TEXT
$identifier
An OpenAPI Object MUST contain an "openapi" field.
This is the version of the OpenAPI Specification you are implementing
TEXT;
return new self($message);
}
public static function missingPaths(Identifier $identifier): self
{
$message = <<<TEXT
$identifier
An OpenAPI Object MUST contain an "paths" field.
TEXT;
return new self($message);
}
public static function forwardSlashMustPrecedePath(
Identifier $identifier,
string $path
): self {
$message = <<<TEXT
$identifier
All paths must be preceded by a forward slash.
"$path" should be changed to "/$path"
TEXT;
return new self($message);
}
public static function pathMissingEndPoint(Identifier $identifier): self
{
$message = "$identifier contains a PathItem which is not mapped to an endpoint";
return new self($message);
}
public static function serverMissingUrl(Identifier $identifier): self
{
$message = <<<TEXT
$identifier
Every Server Object MUST specify a "url"
TEXT;
return new self($message);
}
public static function serverHasUndefinedVariables(
Identifier $identifier,
string ...$variables
): self {
$variables = implode(
"\n",
array_map(fn($v) => sprintf('- "%s"', $v), $variables)
);
$message = <<<TEXT
$identifier
"url" names variable(s) that have not been defined by "variables":
$variables
TEXT;
return new self($message);
}
public static function urlNestedVariable(Identifier $identifier): self
{
return new self(
<<<TEXT
$identifier
Templated URL contains nested expressions.
RFC6570 - 3.2: Expressions cannot be nested.
TEXT
);
}
public static function urlLiteralClosingBrace(Identifier $identifier): self
{
return new self(
<<<TEXT
$identifier
URL contains a closing brace used outside of a variable.
RFC6750 - 2.1: Braces used outside of variables should be pct-encoded
TEXT
);
}
public static function urlUnclosedVariable(Identifier $identifier): self
{
$message = <<<TEXT
$identifier
URL contains an unclosed variable.
RFC6750 - 3.2: A variable begins with an opening brace "{" and continues until the closing brace "}"
TEXT;
return new self($message);
}
public static function serverVariableMissingName(
Identifier $identifier
): self {
$message = <<<TEXT
$identifier
Every Server Variable Object MUST be mapped to by its name
TEXT;
return new self($message);
}
public static function serverVariableMissingDefault(
Identifier $identifier
): self {
$message = <<<TEXT
$identifier
Every Server Variable Object MUST specify a "default"
TEXT;
return new self($message);
}
public static function duplicateParameters(
Identifier $identifier,
Identifier $parameter,
Identifier $otherParameter
): self {
$message = <<<TEXT
$identifier
This MUST NOT contain duplicate Parameters.
Parameter uniqueness is determined by "name" and "in"
$parameter
$otherParameter
TEXT;
return new self($message);
}
public static function parameterMissingName(Identifier $identifier): self
{
$message = <<<TEXT
$identifier contains a Parameter Object without a "name" field.
All Parameter Objects MUST have a "name" field.
TEXT;
return new self($message);
}
public static function parameterMissingLocation(Identifier $identifier): self
{
$message = <<<TEXT
$identifier
This Parameter MUST have an "in" field specifying their location.
Its value MUST be "path", "query", "header" or "cookie".
TEXT;
return new self($message);
}
public static function parameterInvalidLocation(Identifier $identifier): self
{
return self::parameterMissingLocation($identifier);
}
public static function parameterMissingRequired(Identifier $identifier): self
{
$message = <<<TEXT
$identifier
If the parameter location is "path", this property is REQUIRED and its value MUST be true.
TEXT;
return new self($message);
}
public static function parameterInvalidStyle(Identifier $identifier): self
{
$message = <<<TEXT
$identifier
This Parameter has an invalid value for its "style" field.
TEXT;
return new self($message);
}
public static function parameterIncompatibleStyle(Identifier $identifier): self
{
$message = <<<TEXT
$identifier
This Parameter has an incompatible combination of "style" and "in".
TEXT;
return new self($message);
}
public static function identicalEndpoints(Identifier $identifier): self
{
$message = <<<TEXT
$identifier
Two paths have been specified with identical endpoints.
TEXT;
return new self($message);
}
public static function equivalentTemplates(Identifier $firstPath, Identifier $secondPath): self
{
$message = <<<TEXT
Templated paths with the same hierarchy but different templated names MUST NOT exist as they are identical.
The invalid paths are as follows:
First Path: '$firstPath'.
Second Path: '$secondPath'.
TEXT;
return new self($message);
}
public static function duplicateOperationIds(
string $operationId,
string $firstPath,
string $firstMethod,
string $secondPath,
string $secondMethod
): self {
$message = <<<TEXT
A valid OpenAPI must contain unique operationIds across the API.
The operationId: '$operationId' is duplicated between the following operations:
Path: '$firstPath', Method: '$firstMethod'.
Path: '$secondPath', Method: '$secondMethod'.
TEXT;
return new self($message);
}
public static function mustHaveSchemaXorContent(Identifier $identifier): self
{
return new self(<<<TEXT
$identifier
Parameter MUST have either a Schema or Content, but not both.
TEXT);
}
public static function parameterContentCanOnlyHaveOneEntry(Identifier $identifier): self
{
return new self(
sprintf('Parameter found at %s has a content map. The map MUST only contain one entry.', $identifier),
);
}
public static function contentMissingMediaType(Identifier $identifier): self
{
$message = <<<TEXT
$identifier
"content" array must contain mediaType => Media Type Object pairs
TEXT;
return new self($message);
}
public static function deepObjectMustBeObject(Identifier $identifier): self
{
$message = <<<TEXT
$identifier
style:deepObject is only applicable to schemas of type:object
Therefore, your schema MUST be valid ONLY as type:object
TEXT;
return new self($message);
}
public static function invalidType(Identifier $identifier, string $type): self
{
$message = <<<TEXT
$identifier
"type" MUST be "boolean", "object", "array", "number", "string", or "integer".
"$type" is invalid.
TEXT;
return new self($message);
}
public static function keywordMustBeType(
Identifier $identifier,
string $keyword,
Type $type,
): self {
$message = <<<TEXT
$identifier
$keyword MUST be $type->value
TEXT;
return new self($message);
}
public static function numericExclusiveMinMaxIn30(
Identifier $identifier,
string $keyword,
): self {
$message = <<<TEXT
$identifier
$keyword MUST be a boolean in OpenAPI ^3.0
TEXT;
return new self($message);
}
public static function arrayItemsIn31(Identifier $identifier): self
{
$message = <<<TEXT
$identifier
items MUST be a Schema in OpenAPI ^3.1
TEXT;
return new self($message);
}
public static function boolExclusiveMinMaxIn31(
Identifier $identifier,
string $keyword,
): self {
$message = <<<TEXT
$identifier
$keyword MUST be a number in OpenAPI ^3.1
TEXT;
return new self($message);
}
public static function keywordCannotBeZero(
Identifier $identifier,
string $keyword,
): self {
$message = <<<TEXT
$identifier
$keyword MUST not be zero
TEXT;
return new self($message);
}
public static function keywordMustBeNonNegativeInteger(
Identifier $identifier,
string $keyword,
): self {
$message = <<<TEXT
$identifier
$keyword MUST be greater than or equal to zero
TEXT;
return new self($message);
}
public static function failedCebeValidation(string ...$errors): self
{
$message = sprintf("OpenAPI is invalid for the following reasons:\n\t- %s", implode("\n\t- ", $errors));
return new self($message);
}
public static function mustBeNonEmpty(
Identifier $identifier,
string $keyword
): self {
$message = <<<TEXT
$identifier
$keyword MUST be a non-empty array
TEXT;
return new self($message);
}
public static function mustContainUniqueItems(
Identifier $identifier,
string $keyword
): self {
$message = <<<TEXT
$identifier
$keyword MUST be an array of unique strings
TEXT;
return new self($message);
}
public static function mustSpecifyItemsForArrayType(
Identifier $identifier,
): self {
$message = <<<TEXT
$identifier
items MUST be specified if type is array
TEXT;
return new self($message);
}
public static function responseCodeMustBeNumericOrDefault(
Identifier $identifier,
string $code,
): self {
$message = <<<TEXT
$identifier
Response code MUST be numeric, or "default".
"$code" is invalid.
TEXT;
return new self($message);
}
public static function defaultMustConformToType(
Identifier $identifier,
): self {
return new self(<<<TEXT
$identifier
- default MUST conform to type
TEXT);
}
}