Skip to content

Commit f3d8eac

Browse files
committed
refactored strictness in validators
1 parent ad4cf35 commit f3d8eac

15 files changed

Lines changed: 78 additions & 120 deletions

File tree

app/helpers/MetaFormats/Attributes/FPath.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,5 @@ public function __construct(
2424
bool $nullable = false,
2525
) {
2626
parent::__construct(Type::Path, $validators, $description, $required, $nullable);
27-
// do not use json validation for path params
28-
$this->disableJsonValidation();
2927
}
3028
}

app/helpers/MetaFormats/Attributes/FQuery.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,5 @@ public function __construct(
2424
bool $nullable = false,
2525
) {
2626
parent::__construct(Type::Query, $validators, $description, $required, $nullable);
27-
// do not use json validation for query params
28-
$this->disableJsonValidation();
2927
}
3028
}

app/helpers/MetaFormats/Attributes/FormatParameterAttribute.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,12 @@ public function __construct(
5353
}
5454
$this->validators = $validators;
5555
}
56-
}
5756

58-
/**
59-
* Disables JSON validation for all validators.
60-
*/
61-
protected function disableJsonValidation()
62-
{
63-
foreach ($this->validators as $validator) {
64-
$validator->useJsonValidation = false;
57+
// remove strict type checking for query and path parameters
58+
if ($type === Type::Path || $type === Type::Query) {
59+
foreach ($this->validators as $validator) {
60+
$validator->setStrict(false);
61+
}
6562
}
6663
}
6764
}

app/helpers/MetaFormats/Attributes/Path.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,5 @@ public function __construct(
2626
bool $nullable = false,
2727
) {
2828
parent::__construct(Type::Path, $name, $validators, $description, $required, $nullable);
29-
// do not use json validation for path params
30-
$this->disableJsonValidation();
3129
}
3230
}

app/helpers/MetaFormats/Attributes/Query.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,5 @@ public function __construct(
2626
bool $nullable = false,
2727
) {
2828
parent::__construct(Type::Query, $name, $validators, $description, $required, $nullable);
29-
// do not use json validation for query params
30-
$this->disableJsonValidation();
3129
}
3230
}

app/helpers/MetaFormats/Validators/BaseValidator.php

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,50 @@
77
*/
88
class BaseValidator
99
{
10+
public function __construct(bool $strict = true)
11+
{
12+
$this->strict = $strict;
13+
}
14+
1015
/**
1116
* @var string One of the valid swagger types (https://swagger.io/docs/specification/v3_0/data-models/data-types/).
1217
*/
1318
public const SWAGGER_TYPE = "invalid";
1419

1520
/**
16-
* @var bool If true, the validateJson method will be used instead of the validateText one for validation.
17-
* Intended to be changed by Attributes containing validators to change their behavior based on the Attribute type.
21+
* @var bool Whether strict type checking is done in validation.
1822
*/
19-
public bool $useJsonValidation = true;
23+
protected bool $strict;
2024

2125
/**
22-
* @return string Returns a sample expected value to be validated by the validator.
23-
* This value will be used in generated swagger documents.
24-
* Can return null, signalling to the swagger generator to omit the example field.
26+
* Sets the strict flag.
27+
* Expected to be changed by Attributes containing validators to change their behavior based on the Attribute type.
28+
* @param bool $strict Whether validation type checking should be done.
29+
* When false, the validation step will no longer enforce the correct type of the value.
2530
*/
26-
public function getExampleValue(): string | null
31+
public function setStrict(bool $strict)
2732
{
28-
return null;
33+
$this->strict = $strict;
2934
}
3035

3136
/**
32-
* Validates a value retrieved from unstructured data sources, such as query parameters.
33-
* @param mixed $value The value to be validated.
34-
* @return bool Whether the value passed the test.
37+
* @return string Returns a sample expected value to be validated by the validator.
38+
* This value will be used in generated swagger documents.
39+
* Can return null, signalling to the swagger generator to omit the example field.
3540
*/
36-
public function validateText(mixed $value): bool
41+
public function getExampleValue(): string | null
3742
{
38-
// return false by default to enforce overriding in derived types
39-
return false;
43+
return null;
4044
}
4145

4246
/**
43-
* Validates a value retrieved from json files (usually request bodies).
47+
* Validates a value with the configured validation strictness.
4448
* @param mixed $value The value to be validated.
4549
* @return bool Whether the value passed the test.
4650
*/
47-
public function validateJson(mixed $value): bool
51+
public function validate(mixed $value): bool
4852
{
4953
// return false by default to enforce overriding in derived types
5054
return false;
5155
}
52-
53-
/**
54-
* Validates a value with the configured validator method.
55-
* @param mixed $value The value to be validated.
56-
* @return bool Whether the value passed the test.
57-
*/
58-
public function validate(mixed $value): bool
59-
{
60-
if ($this->useJsonValidation) {
61-
return $this->validateJson($value);
62-
}
63-
return $this->validateText($value);
64-
}
6556
}

app/helpers/MetaFormats/Validators/VArray.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ class VArray extends BaseValidator
1010
public const SWAGGER_TYPE = "array";
1111

1212
// validator used for elements
13-
private mixed $nestedValidator;
13+
private ?BaseValidator $nestedValidator;
1414

1515
/**
1616
* Creates an array validator.
17-
* @param mixed $nestedValidator A validator that will be applied on all elements
17+
* @param ?BaseValidator $nestedValidator A validator that will be applied on all elements
1818
* (validator arrays are not supported).
1919
*/
20-
public function __construct(mixed $nestedValidator = null)
20+
public function __construct(?BaseValidator $nestedValidator = null, bool $strict = true)
2121
{
22+
parent::__construct($strict);
2223
$this->nestedValidator = $nestedValidator;
2324
}
2425

@@ -43,12 +44,19 @@ public function getElementSwaggerType(): mixed
4344
return $this->nestedValidator::SWAGGER_TYPE;
4445
}
4546

46-
public function validateText(mixed $value): bool
47+
/**
48+
* Sets the strict flag for this validator and the element validator if present.
49+
* Expected to be changed by Attributes containing validators to change their behavior based on the Attribute type.
50+
* @param bool $strict Whether validation type checking should be done.
51+
* When false, the validation step will no longer enforce the correct type of the value.
52+
*/
53+
public function setStrict(bool $strict)
4754
{
48-
return $this->validateJson($value);
55+
parent::setStrict($strict);
56+
$this->nestedValidator?->setStrict($strict);
4957
}
5058

51-
public function validateJson(mixed $value): bool
59+
public function validate(mixed $value): bool
5260
{
5361
if (!is_array($value)) {
5462
return false;

app/helpers/MetaFormats/Validators/VBool.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ public function getExampleValue(): string
1414
return "true";
1515
}
1616

17-
public function validateText(mixed $value): bool
17+
public function validate(mixed $value): bool
1818
{
19-
// FILTER_VALIDATE_BOOL is not used because it additionally allows "on", "yes", "off", "no" and ""
19+
if (is_bool($value)) {
20+
return true;
21+
}
22+
23+
if ($this->strict) {
24+
///TODO: replace this with 'return false;' once the testUpdateInstance test issue is fixed.
25+
return $value === 'false';
26+
}
2027

21-
// urlencoded params are strings
22-
return $value === "0"
28+
// FILTER_VALIDATE_BOOL is not used because it additionally allows "on", "yes", "off", "no" and ""
29+
return $value === 0
30+
|| $value === 1
31+
|| $value === "0"
2332
|| $value === "1"
24-
|| $value === "true"
2533
|| $value === "false"
26-
|| $value === 0
27-
|| $value === 1
28-
|| $this->validateJson($value);
29-
}
30-
31-
public function validateJson(mixed $value): bool
32-
{
33-
///TODO: remove 'false' once the testUpdateInstance test issue is fixed.
34-
return $value === true || $value === false || $value === 'false';
34+
|| $value === "true";
3535
}
3636
}

app/helpers/MetaFormats/Validators/VDouble.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ public function getExampleValue(): string
1414
return "0.1";
1515
}
1616

17-
public function validateText(mixed $value): bool
17+
public function validate(mixed $value): bool
1818
{
19-
return $this->validateJson($value);
20-
}
21-
22-
public function validateJson(mixed $value): bool
23-
{
24-
// check if it is a double
25-
if (is_double($value)) {
19+
// check if it is a double or a whole number (is_double(0) returns false)
20+
if (is_double($value) || is_int($value)) {
2621
return true;
2722
}
2823

24+
if ($this->strict) {
25+
return false;
26+
}
27+
2928
// the value may be a string containing the number, or an integer
3029
return is_numeric($value);
3130
}

app/helpers/MetaFormats/Validators/VEmail.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,20 @@
77
*/
88
class VEmail extends VString
99
{
10-
public function __construct()
10+
public function __construct(bool $strict = true)
1111
{
1212
// the email should not be empty
13-
parent::__construct(1);
13+
parent::__construct(1, strict: $strict);
1414
}
1515

1616
public function getExampleValue(): string
1717
{
1818
return "name@domain.tld";
1919
}
2020

21-
public function validateText(mixed $value): bool
21+
public function validate(mixed $value): bool
2222
{
23-
return $this->validateJson($value);
24-
}
25-
26-
public function validateJson(mixed $value): bool
27-
{
28-
if (!parent::validateJson($value)) {
23+
if (!parent::validate($value)) {
2924
return false;
3025
}
3126

0 commit comments

Comments
 (0)