Search code examples
phpwordpresswordpress-rest-apiwordpress-gutenbergrest

How to fix Rest API error in WordPress 5.5?


I've just updated to WordPress 5.5 and got error below with debug Mode.

rest_validate_value_from_schema was called incorrectly. The "type" schema keyword for [0] can only be one of the built-in types: array, object, string, number, integer, boolean, and null. Please see Debugging in WordPress for more information. 

I'm using custom end point to create some custom blocks.

function example_custom_route()
{
register_rest_route('example/v1', 'posts', [
    'methods' => WP_REST_Server::READABLE,
    'callback' => 'example_result_posts',
    'permission_callback' => function () {
        return current_user_can( 'edit_posts' );
    }
]);
}
add_action('rest_api_init', 'example_custom_route');

function example_result_posts()
{
    $posts_result = [];
    $post_type = 'post';

    $posts = get_posts([
        'post_type' => $post_type,
        'order' => 'DESC',
        'orderby' => 'date',
        'ignore_sticky_posts' => 1,
        'posts_per_page' => -1,
        'post_status' => 'publish'
    ]);
    if (!empty($posts)) {
        foreach ($posts as $post) {
            array_push($posts_result, [
                'value' => $post->ID,
                'label' => wp_specialchars_decode(wp_strip_all_tags($post->post_title)),
            ]);
        }
        wp_reset_postdata();
    }
    return $posts_result;
}

What is 'The "type" schema keyword' and how can I fix it?

Still searching for the solition for this... on 14th aug.


Solution

  • WP 5.5 started throwing notices when rest_validate_value_from_schema() is called without required parameters, or with incorrect values. The allowed values for a type property are 'array', 'object', 'string', 'number', 'integer', 'boolean', 'null'.

    The code you shared doesn't produce any errors for me, so it may be that the problem is in some of the code that you didn't share? If you're registering a block with attributes, then each attribute will need a type parameter.

    Aside: There's a dedicated WordPress Stack Exchange. You'll probably get better responses there in the future.