Make WordPress Core

Changeset 35890


Ignore:
Timestamp:
12/12/2015 06:22:02 PM (11 years ago)
Author:
joehoyle
Message:

REST API: Improve formatting of failed validation errors.

If a validation_callback returns a WP_Error it should give the same response format as if it returned false. This makes programmatically reading the validation errors better.

Props bradyvercher for initial patch.
Fixes #35028.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/.gitignore

    r35254 r35890  
    11# gitignore file for WordPress Core
    2 
     2.svn
    33# Configuration files with possibly sensitive information
    44wp-config.php
  • trunk/src/wp-includes/rest-api/class-wp-rest-request.php

    r35750 r35890  
    852852
    853853                                if ( is_wp_error( $valid_check ) ) {
    854                                         $invalid_params[] = sprintf( '%s (%s)', $key, $valid_check->get_error_message() );
     854                                        $invalid_params[ $key ] = $valid_check->get_error_message();
    855855                                }
    856856                        }
    … …  
    858858
    859859                if ( $invalid_params ) {
    860                         return new WP_Error( 'rest_invalid_param', sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', $invalid_params ) ), array( 'status' => 400, 'params' => $invalid_params ) );
     860                        return new WP_Error( 'rest_invalid_param', sprintf( __( 'Invalid parameter(s): %s' ), implode( ', ', array_keys( $invalid_params ) ) ), array( 'status' => 400, 'params' => $invalid_params ) );
    861861                }
    862862
  • trunk/tests/phpunit/tests/rest-api/rest-request.php

    r34928 r35890  
    392392                $this->assertArrayHasKey( 'someotherinteger', $data['params'] );
    393393        }
     394
     395        public function test_invalid_params_error_response_format() {
     396                $this->request->set_url_params( array(
     397                        'someinteger' => '123',
     398                        'someotherparams' => '123',
     399                ));
     400
     401                $this->request->set_attributes( array(
     402                        'args' => array(
     403                                'someinteger' => array(
     404                                        'validate_callback' => '__return_false',
     405                                ),
     406                                'someotherparams' => array(
     407                                        'validate_callback' => array( $this, '_return_wp_error_on_validate_callback' ),
     408                                ),
     409                        ),
     410                ));
     411
     412                $valid = $this->request->has_valid_params();
     413                $this->assertWPError( $valid );
     414                $error_data = $valid->get_error_data();
     415
     416                $this->assertEquals( array( 'someinteger', 'someotherparams' ), array_keys( $error_data['params'] ) );
     417                $this->assertEquals( 'This is not valid!', $error_data['params']['someotherparams'] );
     418        }
     419
     420        public function _return_wp_error_on_validate_callback() {
     421                return new WP_Error( 'some-error', 'This is not valid!' );
     422        }
    394423}
Note: See TracChangeset for help on using the changeset viewer.