MailPoet\EmailEditor\Validator
Validator::validate_and_sanitize_value_from_schema()
Mirrors rest_validate_value_from_schema() and rest_sanitize_value_from_schema().
Method of the class: Validator{}
No Hooks.
Return
Mixed|WP_Error
.
Usage
// private - for code of main (parent) class only $result = $this->validate_and_sanitize_value_from_schema( $value, $schema, $param_name );
- $value(mixed) (required)
- The value to validate.
- $schema(array) (required)
- The schema to validate against.
- $param_name(string) (required)
- The parameter name.
Validator::validate_and_sanitize_value_from_schema() Validator::validate and sanitize value from schema code WC 9.8.1
private function validate_and_sanitize_value_from_schema( $value, array $schema, string $param_name ) { // nullable. $full_type = $schema['type'] ?? null; if ( is_array( $full_type ) && in_array( 'null', $full_type, true ) && null === $value ) { return null; } // anyOf, oneOf. if ( isset( $schema['anyOf'] ) ) { return $this->validate_and_sanitize_any_of( $value, $schema, $param_name ); } elseif ( isset( $schema['oneOf'] ) ) { return $this->validate_and_sanitize_one_of( $value, $schema, $param_name ); } // make types strict. $type = is_array( $full_type ) ? $full_type[0] : $full_type; switch ( $type ) { case 'number': if ( ! is_float( $value ) && ! is_int( $value ) ) { return $this->get_type_error( $param_name, $full_type ); } break; case 'integer': if ( ! is_int( $value ) ) { return $this->get_type_error( $param_name, $full_type ); } break; case 'boolean': if ( ! is_bool( $value ) ) { return $this->get_type_error( $param_name, $full_type ); } break; case 'array': if ( ! is_array( $value ) ) { return $this->get_type_error( $param_name, $full_type ); } if ( isset( $schema['items'] ) ) { foreach ( $value as $i => $v ) { $result = $this->validate_and_sanitize_value_from_schema( $v, $schema['items'], $param_name . '[' . $i . ']' ); if ( is_wp_error( $result ) ) { return $result; } } } break; case 'object': if ( ! is_array( $value ) && ! $value instanceof stdClass && ! $value instanceof JsonSerializable ) { return $this->get_type_error( $param_name, $full_type ); } // ensure string keys. $value = (array) ( $value instanceof JsonSerializable ? $value->jsonSerialize() : $value ); if ( count( array_filter( array_keys( $value ), 'is_string' ) ) !== count( $value ) ) { return $this->get_type_error( $param_name, $full_type ); } // validate object properties. foreach ( $value as $k => $v ) { if ( isset( $schema['properties'][ $k ] ) ) { $result = $this->validate_and_sanitize_value_from_schema( $v, $schema['properties'][ $k ], $param_name . '[' . $k . ']' ); if ( is_wp_error( $result ) ) { return $result; } continue; } $pattern_property_schema = rest_find_matching_pattern_property_schema( $k, $schema ); if ( $pattern_property_schema ) { $result = $this->validate_and_sanitize_value_from_schema( $v, $pattern_property_schema, $param_name . '[' . $k . ']' ); if ( is_wp_error( $result ) ) { return $result; } continue; } if ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) { $result = $this->validate_and_sanitize_value_from_schema( $v, $schema['additionalProperties'], $param_name . '[' . $k . ']' ); if ( is_wp_error( $result ) ) { return $result; } } } break; } $result = rest_validate_value_from_schema( $value, $schema, $param_name ); if ( is_wp_error( $result ) ) { return $result; } return rest_sanitize_value_from_schema( $value, $schema, $param_name ); }