Giter VIP home page Giter VIP logo

wp-rest-api-validate's Introduction

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.

wp-rest-api-validate's People

Contributors

mehrshaddarzi avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.