WordPress custom REST API: How to validate input data by many context?

Question

I registered my own custom REST API in WordPress by creating new class extends from WP_REST_Controller.

public function register_routes() {
    register_rest_route(
        $this->namespace,
        "https://wordpress.stackexchange.com/" . $this->rest_base,
        array(
            array(
                'methods'             => WP_REST_Server::READABLE,
                'callback'            => array( $this, 'get_items' ),
                'permission_callback' => array( $this, 'get_items_permissions_check' ),
                'args'                => $this->get_collection_params(),
            ),
            'schema' => array( $this, 'get_public_item_schema' ),
        )
    );
}

I used 'args' => $this->get_collection_params(), to validate input data.

public function get_collection_params() {
    $params = array(
        'aaa'           => array(
            'description' => __( 'AAA' ),
            'type'        => 'integer',
            'minimum'     => 1,
            'maximum'     => 10,
        ),
        'bbb'         => array(
            'description' => __( 'BBB' ),
            'type'        => 'integer',
            'minimum'     => 20,
            'maximum'     => 30,
        ),
    );
    return $params;
}

My question is how can I ONLY validate bbb if it exists (ignore aaa) and vice versa (only validate aaa if it exists)

Because if the user input both aaa and bbb, the validation will apply for both.

Exaple with aaa=15&bbb=25, it is valid parameters, but because the valiation apply for aaa also so will get the error on validation.

I have used $_GET to determine the validation rules like this:

public function get_collection_params() {
    if ( isset( $_GET['bbb'] ) ) {
        return array(
            'bbb'         => array(
                'description' => __( 'BBB' ),
                'type'        => 'integer',
                'minimum'     => 20,
                'maximum'     => 30,
            )
        );
    }

    return array(
        'aaa'           => array(
            'description' => __( 'AAA' ),
            'type'        => 'integer',
            'minimum'     => 1,
            'maximum'     => 10,
        )
    );
}

It worked good. BUT there was an exception. When I wrorte the Unit Test like this

$request  = new WP_REST_Request( 'GET', '/my-custom-endpoint-rest-api' );
$request->set_param( 'bbb', 25 );
$response = rest_get_server()->dispatch( $request );

In my Controller, the $_GET always empty, so can’t do Unit Test for case bbb

I have a few question:

  1. There is any way to access “global” WP_REST_Request instance? I need to use the instance of WP_REST_Request in my method get_collection_params()
  2. There is any “keyword” to define something like ignore aaa if bbb exists?
  3. If NOT, maybe I need to change the way to do the Unit Test. I can use wp_remote_get() instead of WP_REST_Request and rest_get_server()->dispatch( $request ). BUT the problem is wp_remote_get() require full-path-url-api and I don’t know it. Means, not sure about the domain. I only know the path of REST API.

Do you have any idea?

0
Ai Chau 3 weeks 2022-07-31T14:52:42-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse