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. Built from https://develop.svn.wordpress.org/trunk@39222 git-svn-id: http://core.svn.wordpress.org/trunk@39162 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user