-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKeywords.php
More file actions
462 lines (395 loc) · 14.4 KB
/
Keywords.php
File metadata and controls
462 lines (395 loc) · 14.4 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
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php
declare(strict_types=1);
namespace Membrane\OpenAPIReader\ValueObject\Valid\V31;
use Membrane\OpenAPIReader\Exception\InvalidOpenAPI;
use Membrane\OpenAPIReader\OpenAPIVersion;
use Membrane\OpenAPIReader\ValueObject\Limit;
use Membrane\OpenAPIReader\ValueObject\Partial;
use Membrane\OpenAPIReader\ValueObject\Valid;
use Membrane\OpenAPIReader\ValueObject\Valid\Enum\Type;
use Membrane\OpenAPIReader\ValueObject\Valid\Exception\SchemaShouldBeBoolean;
use Membrane\OpenAPIReader\ValueObject\Valid\Identifier;
use Membrane\OpenAPIReader\ValueObject\Valid\Validated;
use Membrane\OpenAPIReader\ValueObject\Valid\Warning;
use Membrane\OpenAPIReader\ValueObject\Value;
final class Keywords extends Validated implements Valid\Schema
{
/** @var list<Type> */
public readonly array $types;
/** @var non-empty-list<Value>|null */
public readonly array|null $enum;
public readonly Value|null $default;
public readonly float|int|null $multipleOf;
public readonly Limit|null $maximum;
public readonly Limit|null $minimum;
public readonly int|null $maxLength;
public readonly int $minLength;
public readonly string|null $pattern;
public readonly Schema $items;
public readonly int|null $maxItems;
public readonly int $minItems;
public readonly bool $uniqueItems;
public readonly int|null $maxProperties;
public readonly int $minProperties;
/** @var list<string> */
public readonly array $required;
/** @var array<string, Schema> */
public readonly array $properties;
public readonly Schema $additionalProperties;
/** @var list<Schema> */
public readonly array $allOf;
/** @var list<Schema> */
public readonly array $anyOf;
/** @var list<Schema> */
public readonly array $oneOf;
public readonly Schema $not;
public readonly string $format;
public readonly string $title;
public readonly string $description;
/** @var Type[] */
private readonly array $typesItCanBe;
public function __construct(
Identifier $identifier,
Valid\Warnings $warnings,
Partial\Schema $schema
) {
parent::__construct($identifier, $warnings);
$this->types = $this->validateTypes($schema->type);
$this->enum = $this->reviewEnum($this->types, $schema->const, $schema->enum);
$this->default = $this->validateDefault($this->types, $schema->default);
$this->multipleOf = $this->validateMultipleOf($schema->multipleOf);
$this->maximum = $this->validateMaximum($schema->maximum, $schema->exclusiveMaximum);
$this->minimum = $this->validateMinimum($schema->minimum, $schema->exclusiveMinimum);
$this->maxLength = $this->validateNonNegativeInteger('maxLength', $schema->maxLength, false);
$this->minLength = $this->validateNonNegativeInteger('minLength', $schema->minLength, true);
$this->pattern = $schema->pattern; //TODO validatePattern is valid regex
$this->items = $this->reviewItems($this->types, $schema->items);
$this->maxItems = $this->validateNonNegativeInteger('maxItems', $schema->maxItems, false);
$this->minItems = $this->validateNonNegativeInteger('minItems', $schema->minItems, true);
$this->uniqueItems = $schema->uniqueItems;
$this->maxProperties = $this->validateNonNegativeInteger('maxProperties', $schema->maxProperties, false);
$this->minProperties = $this->validateNonNegativeInteger('minProperties', $schema->minProperties, true);
//TODO if a property is defined as a false schema AND required, that should be a warning
//TODO if property is false schema and required, and type must be object, the whole schema is the false schema
$this->required = $this->validateRequired($schema->required);
$this->properties = $this->validateProperties($schema->properties);
$this->additionalProperties = new Schema(
$this->appendedIdentifier('additionalProperties'),
$schema->additionalProperties,
);
//TODO throw ShouldBeBooleanSchema::false if allOf contains false schema
$this->allOf = $this->validateSubSchemas('allOf', $schema->allOf);
$this->anyOf = $this->validateSubSchemas('anyOf', $schema->anyOf);
$this->oneOf = $this->validateSubSchemas('oneOf', $schema->oneOf);
$this->not = new Schema(
$this->appendedIdentifier('not'),
$schema->not ?? false,
);
$this->format = $this->formatMetadataString($schema->format);
$this->title = $this->formatMetadataString($schema->title);
$this->description = $this->formatMetadataString($schema->description);
$this->typesItCanBe = array_map(
fn($t) => Type::from($t),
$this->typesItCanBe()
);
if (empty($this->typesItCanBe)) {
$this->addWarning(
'no data type can satisfy this schema',
Warning::IMPOSSIBLE_SCHEMA
);
}
}
/** @return string[] */
public function typesItCanBe(): array
{
$possibilities = [array_merge(
Type::valuesForVersion(OpenAPIVersion::Version_3_0),
[Type::Null->value],
)];
if ($this->types !== []) {
$possibilities[] = array_map(fn($t) => $t->value, $this->types);
}
if (!empty($this->allOf)) {
$possibilities[] = array_intersect(...array_map(
fn($s) => $s->typesItCanBe(),
$this->allOf,
));
}
if (!empty($this->anyOf)) {
$possibilities[] = array_unique(array_merge(...array_map(
fn($s) => $s->typesItCanBe(),
$this->anyOf
)));
}
if (!empty($this->oneOf)) {
$possibilities[] = array_unique(array_merge(...array_map(
fn($s) => $s->typesItCanBe(),
$this->oneOf
)));
}
return array_values(array_intersect(...$possibilities));
}
/**
* @param null|string|array<string> $type
* @return list<Type>
*/
private function validateTypes(null|string|array $type): array
{
if (empty($type)) {
return [];
}
if (is_string($type)) {
$type = [$type];
}
return array_map(
fn($t) => Type::tryFromVersion(OpenAPIVersion::Version_3_1, $t)
?? throw InvalidOpenAPI::invalidType($this->getIdentifier(), $t),
$type,
);
}
/**
* @param list<Type> $types
* @param list<Value>|null $enum
* @return non-empty-list<Value>|null
*/
private function reviewEnum(
array $types,
Value|null $const,
array|null $enum,
): array|null {
if ($const !== null) {
if (empty($enum)) {
return [$const];
} elseif (in_array($const, $enum)) {
$this->addWarning(
'enum is redundant when const is specified',
Warning::REDUNDANT,
);
return [$const];
} else {
throw SchemaShouldBeBoolean::alwaysFalse(
$this->getIdentifier(),
'const is not contained within enum, '
. 'one or the other will always fail',
);
}
}
if ($enum === null) {
return null;
}
if ($enum === []) {
throw SchemaShouldBeBoolean::alwaysFalse(
$this->getIdentifier(),
'enum does not contain any values',
);
}
if ($types === []) {
return array_values($enum);
}
$enumContainsValidValue = false;
foreach ($enum as $value) {
foreach ($types as $type) {
if ($type->doesValueMatchType($value)) {
$enumContainsValidValue = true;
} else {
$this->addWarning(
"$value does not match allowed types",
Warning::MISLEADING,
);
}
}
}
if (! $enumContainsValidValue) {
throw Valid\Exception\SchemaShouldBeBoolean::alwaysFalse(
$this->getIdentifier(),
'enum does not contain any valid values',
);
}
return array_values($enum);
}
/**
* @param list<Type> $types
*/
private function validateDefault(array $types, Value|null $default): Value|null
{
if ($default === null) {
return null;
}
foreach ($types as $type) {
if ($type->doesValueMatchType($default)) {
return $default;
}
}
throw InvalidOpenAPI::defaultMustConformToType($this->getIdentifier());
}
private function validateMultipleOf(float|int|null $value): float|int|null
{
if ($value === null || $value > 0) {
return $value;
}
if ($value < 0) {
$this->addWarning(
'multipleOf must be greater than zero',
Warning::INVALID,
);
return abs($value);
}
throw InvalidOpenAPI::keywordCannotBeZero(
$this->getIdentifier(),
'multipleOf'
);
}
private function validateMinimum(
float|int|null $minimum,
bool|float|int|null $exclusiveMinimum,
): Limit|null {
if (is_bool($exclusiveMinimum)) {
throw InvalidOpenAPI::boolExclusiveMinMaxIn31(
$this->getIdentifier(),
'exclusiveMinimum',
);
}
if (isset($exclusiveMinimum) && isset($minimum)) {
$this->addWarning(
'Having both minimum and exclusiveMinimum is redundant, '
. 'only the stricter one will ever apply',
Warning::REDUNDANT,
);
return $exclusiveMinimum >= $minimum ?
new Limit($exclusiveMinimum, true) :
new Limit($minimum, false);
} elseif (isset($exclusiveMinimum)) {
return new Limit($exclusiveMinimum, true);
} elseif (isset($minimum)) {
return new Limit($minimum, false);
} else {
return null;
}
}
private function validateMaximum(
float|int|null $maximum,
bool|float|int|null $exclusiveMaximum,
): Limit|null {
if (is_bool($exclusiveMaximum)) {
throw InvalidOpenAPI::boolExclusiveMinMaxIn31(
$this->getIdentifier(),
'exclusiveMaximum',
);
}
if (isset($exclusiveMaximum) && isset($maximum)) {
$this->addWarning(
'Having both maximum and exclusiveMaximum is redundant, '
. 'only the stricter one will ever apply',
Warning::REDUNDANT,
);
return $exclusiveMaximum <= $maximum ?
new Limit($exclusiveMaximum, true) :
new Limit($maximum, false);
} elseif (isset($exclusiveMaximum)) {
return new Limit($exclusiveMaximum, true);
} elseif (isset($maximum)) {
return new Limit($maximum, false);
} else {
return null;
}
}
/** @return ($defaultsToZero is true ? int : int|null) */
private function validateNonNegativeInteger(
string $keyword,
int|null $value,
bool $defaultsToZero,
): int|null {
if ($value !== null && $value < 0) {
if (! $defaultsToZero) {
throw InvalidOpenAPI::keywordMustBeNonNegativeInteger(
$this->getIdentifier(),
$keyword
);
} else {
$this->addWarning("$keyword must not be negative", Warning::INVALID);
return 0;
}
}
return $value;
}
/** @param list<Type> $types */
private function reviewItems(
array $types,
Partial\Schema|null $items
): Schema {
if (in_array(Type::Array, $types) && ! isset($items)) {
$this->addWarning(
'items must be specified, if type is array',
Warning::INVALID,
);
}
return new Schema(
$this->getIdentifier()->append('items'),
$items ?? true,
);
}
/**
* @param array<string>|null $required
* @return list<string>
*/
private function validateRequired(array | null $required): array
{
if ($required === null) {
return [];
}
if ($required === []) {
$this->addWarning('required must not be empty', Warning::INVALID);
return [];
}
$uniqueRequired = array_unique($required);
if (count($required) !== count($uniqueRequired)) {
$this->addWarning('required must not contain duplicates', Warning::INVALID);
}
return $uniqueRequired;
}
/**
* @param null|array<Partial\Schema> $subSchemas
* @return list<Schema>
*/
private function validateSubSchemas(string $keyword, ?array $subSchemas): array
{
if ($subSchemas === null) {
return [];
}
if ($subSchemas === []) {
$this->addWarning("$keyword must not be empty", Warning::INVALID);
return [];
}
$result = [];
foreach ($subSchemas as $index => $subSchema) {
$identifier = $this->appendedIdentifier($keyword, sprintf(
empty(trim($subSchema->title ?? '')) ? '%s' : '%2$s[%1$s]',
$index,
trim($subSchema->title ?? ''),
));
$result[] = new Schema($identifier, $subSchema);
}
return $result;
}
/**
* @param array<string, Partial\Schema> $properties
* @return array<string, Schema>
*/
private function validateProperties(?array $properties): array
{
$properties ??= [];
$result = [];
foreach ($properties as $key => $subSchema) {
// json_decode casts numeric string keys to numbers
$result[(string) $key] = new Schema(
$this->getIdentifier()->append("properties($key)"),
$subSchema,
);
}
return $result;
}
public function formatMetadataString(string|null $metadata): string
{
return trim($metadata ?? '');
}
}