Schema
1 year ago
class-builder.php
1 year ago
class-schema.php
1 year ago
class-validation-exception.php
1 year ago
class-validator.php
1 year ago
class-validator.php
232 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This file is part of the WooCommerce Email Editor package. |
| 4 | * |
| 5 | * @package Automattic\WooCommerce\EmailEditor |
| 6 | */ |
| 7 | |
| 8 | declare( strict_types = 1 ); |
| 9 | namespace Automattic\WooCommerce\EmailEditor\Validator; |
| 10 | |
| 11 | use JsonSerializable; |
| 12 | use stdClass; |
| 13 | use WP_Error; |
| 14 | |
| 15 | /** |
| 16 | * Validates and sanitizes values based on a schema. |
| 17 | */ |
| 18 | class Validator { |
| 19 | /** |
| 20 | * Strict validation & sanitization implementation. |
| 21 | * It only coerces int to float (e.g. 5 to 5.0). |
| 22 | * |
| 23 | * @param Schema $schema The schema to validate against. |
| 24 | * @param mixed $value The value to validate. |
| 25 | * @param string $param_name The parameter name. |
| 26 | * @return mixed |
| 27 | */ |
| 28 | public function validate( Schema $schema, $value, string $param_name = 'value' ) { |
| 29 | return $this->validate_schema_array( $schema->to_array(), $value, $param_name ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Strict validation & sanitization implementation. |
| 34 | * It only coerces int to float (e.g. 5 to 5.0). |
| 35 | * |
| 36 | * @param array $schema The array must follow the format, which is returned from Schema::toArray(). |
| 37 | * @param mixed $value The value to validate. |
| 38 | * @param string $param_name The parameter name. |
| 39 | * @return mixed |
| 40 | * @throws Validation_Exception If the value does not match the schema. |
| 41 | */ |
| 42 | public function validate_schema_array( array $schema, $value, string $param_name = 'value' ) { |
| 43 | $result = $this->validate_and_sanitize_value_from_schema( $value, $schema, $param_name ); |
| 44 | if ( $result instanceof WP_Error ) { |
| 45 | throw Validation_Exception::create_from_wp_error( $result ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 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 The value to validate. |
| 54 | * @param array $schema The schema to validate against. |
| 55 | * @param string $param_name The parameter name. |
| 56 | * @return mixed|WP_Error |
| 57 | */ |
| 58 | private function validate_and_sanitize_value_from_schema( $value, array $schema, string $param_name ) { |
| 59 | // nullable. |
| 60 | $full_type = $schema['type'] ?? null; |
| 61 | if ( is_array( $full_type ) && in_array( 'null', $full_type, true ) && null === $value ) { |
| 62 | return null; |
| 63 | } |
| 64 | |
| 65 | // anyOf, oneOf. |
| 66 | if ( isset( $schema['anyOf'] ) ) { |
| 67 | return $this->validate_and_sanitize_any_of( $value, $schema, $param_name ); |
| 68 | } elseif ( isset( $schema['oneOf'] ) ) { |
| 69 | return $this->validate_and_sanitize_one_of( $value, $schema, $param_name ); |
| 70 | } |
| 71 | |
| 72 | // make types strict. |
| 73 | $type = is_array( $full_type ) ? $full_type[0] : $full_type; |
| 74 | switch ( $type ) { |
| 75 | case 'number': |
| 76 | if ( ! is_float( $value ) && ! is_int( $value ) ) { |
| 77 | return $this->get_type_error( $param_name, $full_type ); |
| 78 | } |
| 79 | break; |
| 80 | case 'integer': |
| 81 | if ( ! is_int( $value ) ) { |
| 82 | return $this->get_type_error( $param_name, $full_type ); |
| 83 | } |
| 84 | break; |
| 85 | case 'boolean': |
| 86 | if ( ! is_bool( $value ) ) { |
| 87 | return $this->get_type_error( $param_name, $full_type ); |
| 88 | } |
| 89 | break; |
| 90 | case 'array': |
| 91 | if ( ! is_array( $value ) ) { |
| 92 | return $this->get_type_error( $param_name, $full_type ); |
| 93 | } |
| 94 | |
| 95 | if ( isset( $schema['items'] ) ) { |
| 96 | foreach ( $value as $i => $v ) { |
| 97 | $result = $this->validate_and_sanitize_value_from_schema( $v, $schema['items'], $param_name . '[' . $i . ']' ); |
| 98 | if ( is_wp_error( $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->get_type_error( $param_name, $full_type ); |
| 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->get_type_error( $param_name, $full_type ); |
| 113 | } |
| 114 | |
| 115 | // validate object properties. |
| 116 | foreach ( $value as $k => $v ) { |
| 117 | if ( isset( $schema['properties'][ $k ] ) ) { |
| 118 | $result = $this->validate_and_sanitize_value_from_schema( $v, $schema['properties'][ $k ], $param_name . '[' . $k . ']' ); |
| 119 | if ( is_wp_error( $result ) ) { |
| 120 | return $result; |
| 121 | } |
| 122 | continue; |
| 123 | } |
| 124 | |
| 125 | $pattern_property_schema = rest_find_matching_pattern_property_schema( $k, $schema ); |
| 126 | if ( $pattern_property_schema ) { |
| 127 | $result = $this->validate_and_sanitize_value_from_schema( $v, $pattern_property_schema, $param_name . '[' . $k . ']' ); |
| 128 | if ( is_wp_error( $result ) ) { |
| 129 | return $result; |
| 130 | } |
| 131 | continue; |
| 132 | } |
| 133 | |
| 134 | if ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) { |
| 135 | $result = $this->validate_and_sanitize_value_from_schema( $v, $schema['additionalProperties'], $param_name . '[' . $k . ']' ); |
| 136 | if ( is_wp_error( $result ) ) { |
| 137 | return $result; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | $result = rest_validate_value_from_schema( $value, $schema, $param_name ); |
| 145 | if ( is_wp_error( $result ) ) { |
| 146 | return $result; |
| 147 | } |
| 148 | return rest_sanitize_value_from_schema( $value, $schema, $param_name ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Mirrors rest_find_any_matching_schema(). |
| 153 | * |
| 154 | * @param mixed $value The value to validate. |
| 155 | * @param array $any_of_schema The schema to validate against. |
| 156 | * @param string $param_name The parameter name. |
| 157 | * @return mixed|WP_Error |
| 158 | */ |
| 159 | private function validate_and_sanitize_any_of( $value, array $any_of_schema, string $param_name ) { |
| 160 | $errors = array(); |
| 161 | foreach ( $any_of_schema['anyOf'] as $index => $schema ) { |
| 162 | $result = $this->validate_and_sanitize_value_from_schema( $value, $schema, $param_name ); |
| 163 | if ( ! is_wp_error( $result ) ) { |
| 164 | return $result; |
| 165 | } |
| 166 | $errors[] = array( |
| 167 | 'error_object' => $result, |
| 168 | 'schema' => $schema, |
| 169 | 'index' => $index, |
| 170 | ); |
| 171 | } |
| 172 | /* @phpstan-ignore-next-line Wrong annotation for parameter in WP. */ |
| 173 | return rest_get_combining_operation_error( $value, $param_name, $errors ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Mirrors rest_find_one_matching_schema(). |
| 178 | * |
| 179 | * @param mixed $value The value to validate. |
| 180 | * @param array $one_of_schema The schema to validate against. |
| 181 | * @param string $param_name The parameter name. |
| 182 | * @return mixed|WP_Error |
| 183 | */ |
| 184 | private function validate_and_sanitize_one_of( $value, array $one_of_schema, string $param_name ) { |
| 185 | $matching_schemas = array(); |
| 186 | $errors = array(); |
| 187 | $data = null; |
| 188 | foreach ( $one_of_schema['oneOf'] as $index => $schema ) { |
| 189 | $result = $this->validate_and_sanitize_value_from_schema( $value, $schema, $param_name ); |
| 190 | if ( is_wp_error( $result ) ) { |
| 191 | $errors[] = array( |
| 192 | 'error_object' => $result, |
| 193 | 'schema' => $schema, |
| 194 | 'index' => $index, |
| 195 | ); |
| 196 | } else { |
| 197 | $data = $result; |
| 198 | $matching_schemas[ $index ] = $schema; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if ( ! $matching_schemas ) { |
| 203 | /* @phpstan-ignore-next-line Wrong annotation for parameter in WP. */ |
| 204 | return rest_get_combining_operation_error( $value, $param_name, $errors ); |
| 205 | } |
| 206 | |
| 207 | if ( count( $matching_schemas ) > 1 ) { |
| 208 | // reuse WP method to generate detailed error. |
| 209 | $invalid_schema = array( 'type' => array() ); |
| 210 | $one_of = array_replace( array_fill( 0, count( $one_of_schema['oneOf'] ), $invalid_schema ), $matching_schemas ); |
| 211 | return rest_find_one_matching_schema( $value, array( 'oneOf' => $one_of ), $param_name ); |
| 212 | } |
| 213 | return $data; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Returns a WP_Error for a type mismatch. |
| 218 | * |
| 219 | * @param string $param The parameter name. |
| 220 | * @param string|string[] $type The expected type. |
| 221 | */ |
| 222 | private function get_type_error( string $param, $type ): WP_Error { |
| 223 | $type = is_array( $type ) ? $type : array( $type ); |
| 224 | return new WP_Error( |
| 225 | 'rest_invalid_type', |
| 226 | // translators: %1$s is the current parameter and %2$s a comma-separated list of the allowed types. |
| 227 | sprintf( __( '%1$s is not of type %2$s.', 'woocommerce' ), $param, implode( ',', $type ) ), |
| 228 | array( 'param' => $param ) |
| 229 | ); |
| 230 | } |
| 231 | } |
| 232 |