/wp-rest-api-validate

Laravel Validation for WordPress REST API Plugin

Primary LanguagePHPMIT LicenseMIT

Laravel Validation for WordPress REST API Plugin

Packagist

Professional validation argument for WordPress REST API

Table of Contents

Installation

Download zip file and install plugin in your WordPress site.

How to use in register_rest_route

You Can add validate parameter in register_rest_route function:

add_action('rest_api_init', 'register_routes');
function register_routes()
{

    register_rest_route(
        'v1',
        'book/add',
        array(
            'methods' => \WP_REST_Server::CREATABLE,
            'callback' => 'callback_function_name',
            'permission_callback' => '__return_true',
            'args' => [
                'isbn' => [
                    'title' => __('Book ISBN'),
                    'validate' => ['required', 'min:5']
                ],
                'image' => [
                    'title' => __('screenshot'),
                    'validate' => ['file', 'mimes:jpg,png']
                ],
                'meta' => [
                    'title' => __('meta'),
                    'validate' => 'required|array|min:3'
                ],
                'author' => [
                    'title' => __('Author Book'),
                    'validate' => ['required', function ($attribute, $value, $fail) {
                        if (strlen($value) < 10) {
                            $fail("Show Error in closure-based Validation");
                        }
                    }]
                ]
            ]
        )
    );
}

List of Laravel Validation

All laravel validation Rules are available in this plugin:

https://laravel.com/docs/10.x/validation#available-validation-rules

List of WordPress Hooks

Use custom parameter key in your code

apply_filters('wp_rest_api_validate_args_key', 'validate');

Use custom language (default is WordPress locale)

apply_filters('wp_rest_api_validate_locale', get_locale());

Change validation language dir

apply_filters('wp_rest_api_validate_lang_dir', dirname(__FILE__) . '/lang');

Pre Validation Start

use for disable or add custom condition by request:

apply_filters('wp_rest_api_validate_pre', 
    null, 
    \WP_REST_Response $response, 
    \WP_REST_Request $request, 
    $handler
);

Contributing

We appreciate you taking the initiative to contribute to this project. Contributing isn’t limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.

License

The wp-rest-api-validate is open-sourced software licensed under the MIT license.