REST API: Validate and Sanitize registered meta based off the schema.

With the addition of Array support in our schema validation functions, it's now possible to use these in the meta validation and sanitization steps. Also, this increases the test coverage of using registered via meta the API significantly.

Fixes #38531.
Props rachelbaker, tharsheblows.


git-svn-id: https://develop.svn.wordpress.org/trunk@39222 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe Hoyle
2016-11-14 16:35:35 +00:00
parent 76e44c9166
commit 58169b04fa
5 changed files with 314 additions and 19 deletions
+10
View File
@@ -998,6 +998,9 @@ function rest_validate_value_from_schema( $value, $args, $param = '' ) {
if ( ! is_array( $value ) ) {
$value = preg_split( '/[\s,]+/', $value );
}
if ( ! wp_is_numeric_array( $value ) ) {
return new WP_Error( 'rest_invalid_param', sprintf( /* translators: 1: parameter, 2: type name */ __( '%1$s is not of type %2$s.' ), $param, 'array' ) );
}
foreach ( $value as $index => $v ) {
$is_valid = rest_validate_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' );
if ( is_wp_error( $is_valid ) ) {
@@ -1107,6 +1110,9 @@ function rest_sanitize_value_from_schema( $value, $args ) {
foreach ( $value as $index => $v ) {
$value[ $index ] = rest_sanitize_value_from_schema( $v, $args['items'] );
}
// Normalize to numeric array so nothing unexpected
// is in the keys.
$value = array_values( $value );
return $value;
}
if ( 'integer' === $args['type'] ) {
@@ -1140,5 +1146,9 @@ function rest_sanitize_value_from_schema( $value, $args ) {
}
}
if ( 'string' === $args['type'] ) {
return strval( $value );
}
return $value;
}