Professional validation argument for WordPress REST API
Download zip file and install plugin in your WordPress site.
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");
}
}]
]
]
)
);
}
All laravel validation Rules are available in this plugin:
https://laravel.com/docs/10.x/validation#available-validation-rules
apply_filters('wp_rest_api_validate_args_key', 'validate');
apply_filters('wp_rest_api_validate_locale', get_locale());
apply_filters('wp_rest_api_validate_lang_dir', dirname(__FILE__) . '/lang');
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
);
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.
The wp-rest-api-validate
is open-sourced software licensed under the MIT license.