Schema
11 months ago
class-builder.php
11 months ago
class-schema.php
11 months ago
class-validation-exception.php
11 months ago
class-validator.php
11 months ago
index.php
11 months ago
class-validator.php
156 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | namespace Automattic\WooCommerce\EmailEditor\Validator; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use JsonSerializable; |
| 6 | use stdClass; |
| 7 | use WP_Error; |
| 8 | class Validator { |
| 9 | public function validate( Schema $schema, $value, string $param_name = 'value' ) { |
| 10 | return $this->validate_schema_array( $schema->to_array(), $value, $param_name ); |
| 11 | } |
| 12 | public function validate_schema_array( array $schema, $value, string $param_name = 'value' ) { |
| 13 | $result = $this->validate_and_sanitize_value_from_schema( $value, $schema, $param_name ); |
| 14 | if ( $result instanceof WP_Error ) { |
| 15 | throw Validation_Exception::create_from_wp_error( $result ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 16 | } |
| 17 | return $result; |
| 18 | } |
| 19 | private function validate_and_sanitize_value_from_schema( $value, array $schema, string $param_name ) { |
| 20 | // nullable. |
| 21 | $full_type = $schema['type'] ?? null; |
| 22 | if ( is_array( $full_type ) && in_array( 'null', $full_type, true ) && null === $value ) { |
| 23 | return null; |
| 24 | } |
| 25 | // anyOf, oneOf. |
| 26 | if ( isset( $schema['anyOf'] ) ) { |
| 27 | return $this->validate_and_sanitize_any_of( $value, $schema, $param_name ); |
| 28 | } elseif ( isset( $schema['oneOf'] ) ) { |
| 29 | return $this->validate_and_sanitize_one_of( $value, $schema, $param_name ); |
| 30 | } |
| 31 | // make types strict. |
| 32 | $type = is_array( $full_type ) ? $full_type[0] : $full_type; |
| 33 | switch ( $type ) { |
| 34 | case 'number': |
| 35 | if ( ! is_float( $value ) && ! is_int( $value ) ) { |
| 36 | return $this->get_type_error( $param_name, $full_type ); |
| 37 | } |
| 38 | break; |
| 39 | case 'integer': |
| 40 | if ( ! is_int( $value ) ) { |
| 41 | return $this->get_type_error( $param_name, $full_type ); |
| 42 | } |
| 43 | break; |
| 44 | case 'boolean': |
| 45 | if ( ! is_bool( $value ) ) { |
| 46 | return $this->get_type_error( $param_name, $full_type ); |
| 47 | } |
| 48 | break; |
| 49 | case 'array': |
| 50 | if ( ! is_array( $value ) ) { |
| 51 | return $this->get_type_error( $param_name, $full_type ); |
| 52 | } |
| 53 | if ( isset( $schema['items'] ) ) { |
| 54 | foreach ( $value as $i => $v ) { |
| 55 | $result = $this->validate_and_sanitize_value_from_schema( $v, $schema['items'], $param_name . '[' . $i . ']' ); |
| 56 | if ( is_wp_error( $result ) ) { |
| 57 | return $result; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | break; |
| 62 | case 'object': |
| 63 | if ( ! is_array( $value ) && ! $value instanceof stdClass && ! $value instanceof JsonSerializable ) { |
| 64 | return $this->get_type_error( $param_name, $full_type ); |
| 65 | } |
| 66 | // ensure string keys. |
| 67 | $value = (array) ( $value instanceof JsonSerializable ? $value->jsonSerialize() : $value ); |
| 68 | if ( count( array_filter( array_keys( $value ), 'is_string' ) ) !== count( $value ) ) { |
| 69 | return $this->get_type_error( $param_name, $full_type ); |
| 70 | } |
| 71 | // validate object properties. |
| 72 | foreach ( $value as $k => $v ) { |
| 73 | if ( isset( $schema['properties'][ $k ] ) ) { |
| 74 | $result = $this->validate_and_sanitize_value_from_schema( $v, $schema['properties'][ $k ], $param_name . '[' . $k . ']' ); |
| 75 | if ( is_wp_error( $result ) ) { |
| 76 | return $result; |
| 77 | } |
| 78 | continue; |
| 79 | } |
| 80 | $pattern_property_schema = rest_find_matching_pattern_property_schema( $k, $schema ); |
| 81 | if ( $pattern_property_schema ) { |
| 82 | $result = $this->validate_and_sanitize_value_from_schema( $v, $pattern_property_schema, $param_name . '[' . $k . ']' ); |
| 83 | if ( is_wp_error( $result ) ) { |
| 84 | return $result; |
| 85 | } |
| 86 | continue; |
| 87 | } |
| 88 | if ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) { |
| 89 | $result = $this->validate_and_sanitize_value_from_schema( $v, $schema['additionalProperties'], $param_name . '[' . $k . ']' ); |
| 90 | if ( is_wp_error( $result ) ) { |
| 91 | return $result; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | break; |
| 96 | } |
| 97 | $result = rest_validate_value_from_schema( $value, $schema, $param_name ); |
| 98 | if ( is_wp_error( $result ) ) { |
| 99 | return $result; |
| 100 | } |
| 101 | return rest_sanitize_value_from_schema( $value, $schema, $param_name ); |
| 102 | } |
| 103 | private function validate_and_sanitize_any_of( $value, array $any_of_schema, string $param_name ) { |
| 104 | $errors = array(); |
| 105 | foreach ( $any_of_schema['anyOf'] as $index => $schema ) { |
| 106 | $result = $this->validate_and_sanitize_value_from_schema( $value, $schema, $param_name ); |
| 107 | if ( ! is_wp_error( $result ) ) { |
| 108 | return $result; |
| 109 | } |
| 110 | $errors[] = array( |
| 111 | 'error_object' => $result, |
| 112 | 'schema' => $schema, |
| 113 | 'index' => $index, |
| 114 | ); |
| 115 | } |
| 116 | return rest_get_combining_operation_error( $value, $param_name, $errors ); |
| 117 | } |
| 118 | private function validate_and_sanitize_one_of( $value, array $one_of_schema, string $param_name ) { |
| 119 | $matching_schemas = array(); |
| 120 | $errors = array(); |
| 121 | $data = null; |
| 122 | foreach ( $one_of_schema['oneOf'] as $index => $schema ) { |
| 123 | $result = $this->validate_and_sanitize_value_from_schema( $value, $schema, $param_name ); |
| 124 | if ( is_wp_error( $result ) ) { |
| 125 | $errors[] = array( |
| 126 | 'error_object' => $result, |
| 127 | 'schema' => $schema, |
| 128 | 'index' => $index, |
| 129 | ); |
| 130 | } else { |
| 131 | $data = $result; |
| 132 | $matching_schemas[ $index ] = $schema; |
| 133 | } |
| 134 | } |
| 135 | if ( ! $matching_schemas ) { |
| 136 | return rest_get_combining_operation_error( $value, $param_name, $errors ); |
| 137 | } |
| 138 | if ( count( $matching_schemas ) > 1 ) { |
| 139 | // reuse WP method to generate detailed error. |
| 140 | $invalid_schema = array( 'type' => array() ); |
| 141 | $one_of = array_replace( array_fill( 0, count( $one_of_schema['oneOf'] ), $invalid_schema ), $matching_schemas ); |
| 142 | return rest_find_one_matching_schema( $value, array( 'oneOf' => $one_of ), $param_name ); |
| 143 | } |
| 144 | return $data; |
| 145 | } |
| 146 | private function get_type_error( string $param, $type ): WP_Error { |
| 147 | $type = is_array( $type ) ? $type : array( $type ); |
| 148 | return new WP_Error( |
| 149 | 'rest_invalid_type', |
| 150 | // translators: %1$s is the current parameter and %2$s a comma-separated list of the allowed types. |
| 151 | sprintf( __( '%1$s is not of type %2$s.', 'woocommerce' ), $param, implode( ',', $type ) ), |
| 152 | array( 'param' => $param ) |
| 153 | ); |
| 154 | } |
| 155 | } |
| 156 |