Schema
3 years ago
Builder.php
1 year ago
Schema.php
8 months ago
ValidationException.php
4 years ago
Validator.php
3 years ago
index.php
3 years ago
Validator.php
213 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Validator; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use JsonSerializable; |
| 9 | use MailPoet\WP\Functions as WPFunctions; |
| 10 | use stdClass; |
| 11 | use WP_Error; |
| 12 | |
| 13 | class Validator { |
| 14 | /** @var WPFunctions */ |
| 15 | private $wp; |
| 16 | |
| 17 | public function __construct( |
| 18 | WPFunctions $wp |
| 19 | ) { |
| 20 | $this->wp = $wp; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Strict validation & sanitization implementation. |
| 25 | * It only coerces int to float (e.g. 5 to 5.0). |
| 26 | * |
| 27 | * @param mixed $value |
| 28 | * @return mixed |
| 29 | */ |
| 30 | public function validate(Schema $schema, $value, string $paramName = 'value') { |
| 31 | return $this->validateSchemaArray($schema->toArray(), $value, $paramName); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Strict validation & sanitization implementation. |
| 36 | * It only coerces int to float (e.g. 5 to 5.0). |
| 37 | * |
| 38 | * @param array $schema. The array must follow the format, which is returned from Schema::toArray(). |
| 39 | * @param mixed $value |
| 40 | * @return mixed |
| 41 | */ |
| 42 | public function validateSchemaArray(array $schema, $value, string $paramName = 'value') { |
| 43 | $result = $this->validateAndSanitizeValueFromSchema($value, $schema, $paramName); |
| 44 | if ($result instanceof WP_Error) { |
| 45 | throw ValidationException::createFromWpError($result); |
| 46 | } |
| 47 | return $result; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Mirrors rest_validate_value_from_schema() and rest_sanitize_value_from_schema(). |
| 52 | * |
| 53 | * @param mixed $value |
| 54 | * @param array $schema |
| 55 | * @param string $paramName |
| 56 | * @return mixed|WP_Error |
| 57 | */ |
| 58 | private function validateAndSanitizeValueFromSchema($value, array $schema, string $paramName) { |
| 59 | // nullable |
| 60 | $fullType = $schema['type'] ?? null; |
| 61 | if (is_array($fullType) && in_array('null', $fullType, true) && $value === null) { |
| 62 | return null; |
| 63 | } |
| 64 | |
| 65 | // anyOf, oneOf |
| 66 | if (isset($schema['anyOf'])) { |
| 67 | return $this->validateAndSanitizeAnyOf($value, $schema, $paramName); |
| 68 | } elseif (isset($schema['oneOf'])) { |
| 69 | return $this->validateAndSanitizeOneOf($value, $schema, $paramName); |
| 70 | } |
| 71 | |
| 72 | // make types strict |
| 73 | $type = is_array($fullType) ? $fullType[0] : $fullType; |
| 74 | switch ($type) { |
| 75 | case 'number': |
| 76 | if (!is_float($value) && !is_int($value)) { |
| 77 | return $this->getTypeError($paramName, $fullType); |
| 78 | } |
| 79 | break; |
| 80 | case 'integer': |
| 81 | if (!is_int($value)) { |
| 82 | return $this->getTypeError($paramName, $fullType); |
| 83 | } |
| 84 | break; |
| 85 | case 'boolean': |
| 86 | if (!is_bool($value)) { |
| 87 | return $this->getTypeError($paramName, $fullType); |
| 88 | } |
| 89 | break; |
| 90 | case 'array': |
| 91 | if (!is_array($value)) { |
| 92 | return $this->getTypeError($paramName, $fullType); |
| 93 | } |
| 94 | |
| 95 | if (isset($schema['items'])) { |
| 96 | foreach ($value as $i => $v) { |
| 97 | $result = $this->validateAndSanitizeValueFromSchema($v, $schema['items'], $paramName . '[' . $i . ']'); |
| 98 | if ($this->wp->isWpError($result)) { |
| 99 | return $result; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | break; |
| 104 | case 'object': |
| 105 | if (!is_array($value) && !$value instanceof stdClass && !$value instanceof JsonSerializable) { |
| 106 | return $this->getTypeError($paramName, $fullType); |
| 107 | } |
| 108 | |
| 109 | // ensure string keys |
| 110 | $value = (array)($value instanceof JsonSerializable ? $value->jsonSerialize() : $value); |
| 111 | if (count(array_filter(array_keys($value), 'is_string')) !== count($value)) { |
| 112 | return $this->getTypeError($paramName, $fullType); |
| 113 | } |
| 114 | |
| 115 | // validate object properties |
| 116 | foreach ($value as $k => $v) { |
| 117 | if (isset($schema['properties'][$k])) { |
| 118 | $result = $this->validateAndSanitizeValueFromSchema($v, $schema['properties'][$k], $paramName . '[' . $k . ']'); |
| 119 | if ($this->wp->isWpError($result)) { |
| 120 | return $result; |
| 121 | } |
| 122 | continue; |
| 123 | } |
| 124 | |
| 125 | $patternPropertySchema = $this->wp->restFindMatchingPatternPropertySchema($k, $schema); |
| 126 | if ($patternPropertySchema) { |
| 127 | $result = $this->validateAndSanitizeValueFromSchema($v, $patternPropertySchema, $paramName . '[' . $k . ']'); |
| 128 | if ($this->wp->isWpError($result)) { |
| 129 | return $result; |
| 130 | } |
| 131 | continue; |
| 132 | } |
| 133 | |
| 134 | if (isset($schema['additionalProperties']) && is_array($schema['additionalProperties'])) { |
| 135 | $result = $this->validateAndSanitizeValueFromSchema($v, $schema['additionalProperties'], $paramName . '[' . $k . ']'); |
| 136 | if ($this->wp->isWpError($result)) { |
| 137 | return $result; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | $result = $this->wp->restValidateValueFromSchema($value, $schema, $paramName); |
| 145 | if ($this->wp->isWpError($result)) { |
| 146 | return $result; |
| 147 | } |
| 148 | return $this->wp->restSanitizeValueFromSchema($value, $schema, $paramName); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Mirrors rest_find_any_matching_schema(). |
| 153 | * |
| 154 | * @param mixed $value |
| 155 | * @return mixed|WP_Error |
| 156 | */ |
| 157 | private function validateAndSanitizeAnyOf($value, array $anyOfSchema, string $paramName) { |
| 158 | $errors = []; |
| 159 | foreach ($anyOfSchema['anyOf'] as $index => $schema) { |
| 160 | $result = $this->validateAndSanitizeValueFromSchema($value, $schema, $paramName); |
| 161 | if (!$this->wp->isWpError($result)) { |
| 162 | return $result; |
| 163 | } |
| 164 | $errors[] = ['error_object' => $result, 'schema' => $schema, 'index' => $index]; |
| 165 | } |
| 166 | return $this->wp->restGetCombiningOperationError($value, $paramName, $errors); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Mirrors rest_find_one_matching_schema(). |
| 171 | * |
| 172 | * @param mixed $value |
| 173 | * @return mixed|WP_Error |
| 174 | */ |
| 175 | private function validateAndSanitizeOneOf($value, array $oneOfSchema, string $paramName) { |
| 176 | $matchingSchemas = []; |
| 177 | $errors = []; |
| 178 | $data = null; |
| 179 | foreach ($oneOfSchema['oneOf'] as $index => $schema) { |
| 180 | $result = $this->validateAndSanitizeValueFromSchema($value, $schema, $paramName); |
| 181 | if ($this->wp->isWpError($result)) { |
| 182 | $errors[] = ['error_object' => $result, 'schema' => $schema, 'index' => $index]; |
| 183 | } else { |
| 184 | $data = $result; |
| 185 | $matchingSchemas[$index] = $schema; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if (!$matchingSchemas) { |
| 190 | return $this->wp->restGetCombiningOperationError($value, $paramName, $errors); |
| 191 | } |
| 192 | |
| 193 | if (count($matchingSchemas) > 1) { |
| 194 | // reuse WP method to generate detailed error |
| 195 | $invalidSchema = ['type' => []]; |
| 196 | $oneOf = array_replace(array_fill(0, count($oneOfSchema['oneOf']), $invalidSchema), $matchingSchemas); |
| 197 | return $this->wp->restFindOneMatchingSchema($value, ['oneOf' => $oneOf], $paramName); |
| 198 | } |
| 199 | return $data; |
| 200 | } |
| 201 | |
| 202 | /** @param string|string[] $type */ |
| 203 | private function getTypeError(string $param, $type): WP_Error { |
| 204 | $type = is_array($type) ? $type : [$type]; |
| 205 | return new WP_Error( |
| 206 | 'rest_invalid_type', |
| 207 | // translators: %1$s is the current parameter and %2$s a comma-separated list of the allowed types. |
| 208 | sprintf(__('%1$s is not of type %2$s.', 'mailpoet'), $param, implode(',', $type)), |
| 209 | ['param' => $param] |
| 210 | ); |
| 211 | } |
| 212 | } |
| 213 |