Skip to content

Commit f231185

Browse files
committed
string validator constraints are now propagated to the swagger document
1 parent 5bcdd30 commit f231185

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

app/helpers/MetaFormats/RequestParamData.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ public function toAnnotationParameterData()
122122
// get example value from the first validator
123123
$exampleValue = $this->validators[0]->getExampleValue();
124124

125+
// get constraints from validators
126+
$constraints = null;
127+
foreach ($this->validators as $validator) {
128+
$constraints = $validator->getConstraints();
129+
// it is assumed that at most one validator defines constraints
130+
if ($constraints !== null) {
131+
break;
132+
}
133+
}
134+
125135
// add nested parameter data if this is an object
126136
$format = $this->getFormatName();
127137
$nestedObjectParameterData = null;
@@ -142,6 +152,7 @@ public function toAnnotationParameterData()
142152
$exampleValue,
143153
$nestedArraySwaggerType,
144154
$nestedObjectParameterData,
155+
$constraints,
145156
);
146157
}
147158
}

app/helpers/MetaFormats/Validators/BaseValidator.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Helpers\MetaFormats\Validators;
44

5+
use App\Helpers\Swagger\ParameterConstraints;
6+
57
/**
68
* Base class for all validators.
79
*/
@@ -43,6 +45,16 @@ public function getExampleValue(): string | null
4345
return null;
4446
}
4547

48+
/**
49+
* @return ParameterConstraints Returns all parameter constrains that will be written into the generated
50+
* swagger document. Returns null if there are no constraints.
51+
*/
52+
public function getConstraints(): ?ParameterConstraints
53+
{
54+
// there are no default constraints
55+
return null;
56+
}
57+
4658
/**
4759
* Validates a value with the configured validation strictness.
4860
* @param mixed $value The value to be validated.

app/helpers/MetaFormats/Validators/VString.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Helpers\MetaFormats\Validators;
44

5+
use App\Helpers\Swagger\ParameterConstraints;
6+
57
/**
68
* Validates strings.
79
*/
@@ -32,6 +34,14 @@ public function getExampleValue(): string
3234
return "text";
3335
}
3436

37+
public function getConstraints(): ParameterConstraints
38+
{
39+
// do not pass redundant constraints
40+
$minLength = ($this->minLength > 0 ? $this->minLength : null);
41+
$maxLength = ($this->maxLength !== -1 ? $this->maxLength : null);
42+
return new ParameterConstraints($this->regex, $minLength, $maxLength);
43+
}
44+
3545
public function validate(mixed $value): bool
3646
{
3747
// do not allow other types

app/helpers/Swagger/AnnotationParameterData.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class AnnotationParameterData
1919
public ?string $example;
2020
public ?string $nestedArraySwaggerType;
2121
public ?array $nestedObjectParameterData;
22+
public ?ParameterConstraints $constraints;
2223

2324
public function __construct(
2425
string $swaggerType,
@@ -27,9 +28,10 @@ public function __construct(
2728
string $location,
2829
bool $required,
2930
bool $nullable,
30-
string $example = null,
31-
string $nestedArraySwaggerType = null,
31+
?string $example = null,
32+
?string $nestedArraySwaggerType = null,
3233
?array $nestedObjectParameterData = null,
34+
?ParameterConstraints $constraints = null,
3335
) {
3436
$this->swaggerType = $swaggerType;
3537
$this->name = $name;
@@ -40,10 +42,12 @@ public function __construct(
4042
$this->example = $example;
4143
$this->nestedArraySwaggerType = $nestedArraySwaggerType;
4244
$this->nestedObjectParameterData = $nestedObjectParameterData;
45+
$this->constraints = $constraints;
4346
}
4447

4548
private function addArrayItemsIfArray(ParenthesesBuilder $container)
4649
{
50+
///TODO: nested constraints should be added here
4751
if ($this->swaggerType !== "array") {
4852
return;
4953
}
@@ -128,6 +132,9 @@ public function toPropertyAnnotation(): string
128132
$body->addKeyValue("description", $this->description);
129133
}
130134

135+
// handle param constraints
136+
$this->constraints?->addConstraints($body);
137+
131138
// handle arrays
132139
$this->addArrayItemsIfArray($body);
133140

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\Helpers\Swagger;
4+
5+
class ParameterConstraints
6+
{
7+
private array $constraints;
8+
9+
/**
10+
* Constructs a container for swagger constraints.
11+
* Constructor parameter names match swagger keywords, see
12+
* https://swagger.io/docs/specification/v3_0/data-models/keywords/.
13+
* @param ?string $pattern String regex pattern.
14+
* @param ?int $minLength String min length.
15+
* @param ?int $maxLength String max length.
16+
*/
17+
public function __construct(?string $pattern = null, ?int $minLength = null, ?int $maxLength = null)
18+
{
19+
$this->constraints["pattern"] = $pattern;
20+
$this->constraints["minLength"] = $minLength;
21+
$this->constraints["maxLength"] = $maxLength;
22+
}
23+
24+
/**
25+
* Adds constraints to a ParenthesesBuilder for swagger doc construction.
26+
* @param \App\Helpers\Swagger\ParenthesesBuilder $container The container for keywords and values.
27+
*/
28+
public function addConstraints(ParenthesesBuilder $container)
29+
{
30+
foreach ($this->constraints as $keyword=>$value) {
31+
// skip null values
32+
if ($value === null) {
33+
continue;
34+
}
35+
36+
$container->addKeyValue($keyword, $value);
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)