|
7 | 7 | */ |
8 | 8 | class BaseValidator |
9 | 9 | { |
| 10 | + public function __construct(bool $strict = true) |
| 11 | + { |
| 12 | + $this->strict = $strict; |
| 13 | + } |
| 14 | + |
10 | 15 | /** |
11 | 16 | * @var string One of the valid swagger types (https://swagger.io/docs/specification/v3_0/data-models/data-types/). |
12 | 17 | */ |
13 | 18 | public const SWAGGER_TYPE = "invalid"; |
14 | 19 |
|
15 | 20 | /** |
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. |
18 | 22 | */ |
19 | | - public bool $useJsonValidation = true; |
| 23 | + protected bool $strict; |
20 | 24 |
|
21 | 25 | /** |
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. |
25 | 30 | */ |
26 | | - public function getExampleValue(): string | null |
| 31 | + public function setStrict(bool $strict) |
27 | 32 | { |
28 | | - return null; |
| 33 | + $this->strict = $strict; |
29 | 34 | } |
30 | 35 |
|
31 | 36 | /** |
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. |
35 | 40 | */ |
36 | | - public function validateText(mixed $value): bool |
| 41 | + public function getExampleValue(): string | null |
37 | 42 | { |
38 | | - // return false by default to enforce overriding in derived types |
39 | | - return false; |
| 43 | + return null; |
40 | 44 | } |
41 | 45 |
|
42 | 46 | /** |
43 | | - * Validates a value retrieved from json files (usually request bodies). |
| 47 | + * Validates a value with the configured validation strictness. |
44 | 48 | * @param mixed $value The value to be validated. |
45 | 49 | * @return bool Whether the value passed the test. |
46 | 50 | */ |
47 | | - public function validateJson(mixed $value): bool |
| 51 | + public function validate(mixed $value): bool |
48 | 52 | { |
49 | 53 | // return false by default to enforce overriding in derived types |
50 | 54 | return false; |
51 | 55 | } |
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 | | - } |
65 | 56 | } |
0 commit comments