{"id":21502226,"url":"https://github.com/bit-apps-pro/wp-validator","last_synced_at":"2026-02-02T09:22:09.171Z","repository":{"id":195548261,"uuid":"692972096","full_name":"Bit-Apps-Pro/wp-validator","owner":"Bit-Apps-Pro","description":"The WP Validator is a robust PHP package designed to simplify the process of data validation and sanitization. It offers a versatile and user-friendly solution for developers to ensure that user input meets specific criteria and is secure against common vulnerabilities.","archived":false,"fork":false,"pushed_at":"2024-10-24T17:55:24.000Z","size":133,"stargazers_count":7,"open_issues_count":2,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-26T02:18:06.423Z","etag":null,"topics":["php","request-validation","validation-library","wordpress","wordpress-development","wordpress-plugin"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Bit-Apps-Pro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-18T04:53:59.000Z","updated_at":"2024-10-22T06:36:28.000Z","dependencies_parsed_at":"2024-02-29T08:26:54.304Z","dependency_job_id":"9ae9415c-659f-499b-b3b0-0bed9465b8bb","html_url":"https://github.com/Bit-Apps-Pro/wp-validator","commit_stats":null,"previous_names":["bit-apps-pro/php-validator-sanitizer","bit-apps-pro/wp-validator"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bit-Apps-Pro%2Fwp-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bit-Apps-Pro%2Fwp-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bit-Apps-Pro%2Fwp-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bit-Apps-Pro%2Fwp-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Bit-Apps-Pro","download_url":"https://codeload.github.com/Bit-Apps-Pro/wp-validator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226077479,"owners_count":17570164,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","request-validation","validation-library","wordpress","wordpress-development","wordpress-plugin"],"created_at":"2024-11-23T18:14:03.788Z","updated_at":"2026-02-02T09:22:09.130Z","avatar_url":"https://github.com/Bit-Apps-Pro.png","language":"PHP","readme":"# WP Validator\n\nValidate and sanitize form inputs and API requests with a PHP library inspired by Laravel, designed specifically for WordPress.\n\n[![Latest Stable Version](https://poser.pugx.org/bitapps/wp-validator/v/stable)](https://packagist.org/packages/bitapps/wp-validator) [![Total Downloads](https://poser.pugx.org/bitapps/wp-validator/downloads)](https://packagist.org/packages/bitapps/wp-validator) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)\n\n## Overview\n\nWP Validator is a comprehensive PHP package inspired by Laravel. It simplifies the process of data validation and sanitization for WordPress, providing a versatile and user-friendly solution for developers to ensure that user input meets specific criteria and is secure against common vulnerabilities.\n\n## Features\n\n- **Data Validation:** Easily validate user inputs, form submissions, and API requests.\n- **Custom Validation Rules:** Define your custom validation rules to meet your application's specific needs.\n- **Error Messages:** Detailed error messages to assist users in understanding validation failures.\n- **Data Sanitization:** Optional data sanitization functions for cleaning and formatting data.\n\n## Example Usage\n\nTo use the wp-validator package for data validation in your PHP application, follow these steps:\n\n### 1. Install the Package\n\nBegin by installing the WP Validator package using Composer:\n\n```bash\ncomposer require bitapps/wp-validator\n```\n\n### 2. Initialize the Validator\n\nCreate an instance of the Validator class from the package:\n\n```php\nuse BitApps\\WPValidator\\Validator;\n\n$validator = new Validator;\n```\n\nFor public methods that can be used with `$validator`, refer to the [Validator Instance Methods](#validator-instance-methods) section.\n\n### 3. Define Your Data and Validation Rules\n\nPrepare the data you want to validate and define the validation rules. Here's an example:\n\n```php\n$data = [\n    'first_name' =\u003e 'John',\n    'last_name' =\u003e '',\n    'email' =\u003e 'email@example',\n    'password' =\u003e '##112233',\n    'confirm_password' =\u003e '##112233',\n];\n\n$rules = [\n    'first_name' =\u003e ['required', 'string'],\n    'last_name' =\u003e ['required', 'string'],\n    'email' =\u003e ['required', 'email'],\n    'password' =\u003e ['required', 'min:8'],\n    'confirm_password' =\u003e ['required', 'min:6', 'same:password'],\n];\n```\n\nExplore all available validation rules and their usage in the [Available Validation Rules](#available-validation-rules) section.\n\n### 4. Customize Error Messages (Optional)\n\nIf you need to customize error messages, you can use the `$customMessages` array. In this example, we leave it empty.\n\n```php\n$customMessages = [];\n```\n\nLearn more about customizing error messages in the [Customizing Error Messages](#customizing-error-messages) section.\n\n### 5. Map Attribute Names (Optional)\n\nMap your field names to user-friendly labels using the `$attributes` array, these labels will be used for error messages.\n\n```php\n$attributes = [\n    'first_name' =\u003e 'First Name',\n    'last_name' =\u003e 'Last Name',\n    'email' =\u003e 'Email',\n];\n```\n\n### 6. Perform Validation\n\nExecute the validation using the `make` method:\n\n```php\n$validation = $validator-\u003emake($data, $rules, $customMessages, $attributes);\n```\n\n### 7. Handle Validation Results\n\nCheck if validation fails and, if so, print out the validation errors:\n\n```php\nif ($validation-\u003efails()) {\n    echo \"\u003cpre\u003e\";\n    echo print_r($validation-\u003eerrors(), true);\n    echo \"\u003c/pre\u003e\";\n} else {\n    echo \"Success!\";\n}\n```\n\n### Validator Instance Methods\n\n#### `make($data: array, $rules: array[, $customMessages?: array, $attributes?: array])`\n\nThis method runs the validations of `$data` based on given `$rules`. Optionally, if you pass `$customMessages` and `$attributes`, it will make the error messages (*if any*) based on that.\n\n#### `fails(): boolean`\n\nThis method will return true or false based on the validation status. If it returns true, that means the validator has found errors in data, and you can get those errors by the `errors()` method.\n\n#### `errors(): array`\n\nThis method will return the error messages (*if any*) based on the format of the passed `$data` array in the `make()` method.\n\n### Available Validation Rules\n\nWP Validator provides a comprehensive set of validation rules to suit your needs. Here's a list of available rules:\n\n1. **`accepted`**\u003cbr/\u003e\nChecks if the field under validation is one of the following: `'yes'`, `'on'`, `'1'`, `1`, `'true'`, `true`. This is useful for validating agreement type fields.\n2. **`array`**\u003cbr/\u003e\nChecks if the field under validation is an array.\n3. **`between:min,max`**\u003cbr/\u003e\nChecks if the field under validation falls within the range of `:min` and `:max` (inclusive).\n    - For string data, the value corresponds to the number of characters.\n    - For numeric data, the value corresponds to a given integer value.\n    - For an array, the value corresponds to the count of the array.\n4. **`date`**\u003cbr/\u003e\nChecks if the field under validation is a valid date according to the `strtotime` PHP function.\n5. **`digit_between:min,max`**\u003cbr/\u003e\nChecks if the length of digits for the integer number falls within the range of `:min` and `:max` (inclusive).\n6. **`digits:value`**\u003cbr/\u003e\nChecks if the length of digits for the integer number is exactly the same as `:digits`.\n7. **`email`**\u003cbr/\u003e\nChecks if the field under validation is a valid email address.\n8. **`integer`**\u003cbr/\u003e\nChecks if the field under validation is an integer number.\n9. **`ip`**\u003cbr/\u003e\nChecks if the field under validation is a valid IP (IPv4, IPv6) address.\n10. **`ipv4`**\u003cbr/\u003e\nChecks if the field under validation is a valid IPv4 address.\n11. **`ipv6`**\u003cbr/\u003e\nChecks if the field under validation is a valid IPv6 address.\n12. **`json`**\u003cbr/\u003e\nChecks if the field under validation is a valid JSON string.\n13. **`lowercase`**\u003cbr/\u003e\nChecks if the field under validation consists of all lowercase letters.\n14. **`mac_address`**\u003cbr/\u003e\nChecks if the field under validation is a valid MAC address.\n15. **`max:value`**\u003cbr/\u003e\nChecks if the field under validation is less than or equal to `:max`.\n    - For string data, the value corresponds to the number of characters.\n    - For numeric data, the value corresponds to a given integer value.\n    - For an array, the value corresponds to the count of the array.\n16. **`min:value`**\u003cbr/\u003e\nChecks if the field under validation has a minimum value of `:min`.\n    - For string data, the value corresponds to the number of characters.\n    - For numeric data, the value corresponds to a given integer value.\n    - For an array, the value corresponds to the count of the array.\n17. **`nullable`**\u003cbr/\u003e\nMakes the field under validation as optional (allows to be null), but respects other validation rules if specified and value is not null.\n18. **`numeric`**\u003cbr/\u003e\nChecks if the field under validation is a valid real number.\n19. **`required`**\u003cbr/\u003e\nChecks if the field under validation is present and not empty. A field is \"empty\" if it meets one of the following criteria:\n    - The value is `NULL` or `FALSE`.\n    - The value is an empty string.\n    - The value is an empty array or empty countable object.\n\n20. **`present`**\u003cbr/\u003e\nThe field must be present in the input data for validation.\n21. **`same:field`**\u003cbr/\u003e\nChecks if the field under validation is equal to the specified `:other` attribute.\n22. **`size:value`**\u003cbr/\u003e\nChecks if the field under validation has exactly the same size as `:size`.\n    - For string data, the value corresponds to the number of characters.\n    - For numeric data, the value corresponds to a given integer value.\n    - For an array, the value corresponds to the count of the array.\n23. **`string`**\u003cbr/\u003e\nChecks if the given value is a string.\n24. **`uppercase`**\u003cbr/\u003e\nChecks if the string value consists of all uppercase letters.\n25. **`url`**\u003cbr/\u003e\nChecks if the value is a valid URL.\n\nMissing any validation rule that you need? Refer to the [Custom Validation Rule](#custom-validation-rule) section to know how you can create and use custom validation rules in your project alongside the library.\n\n### Available sanitization functions\n1. **`sanitize_email`**\u003cbr/\u003e\nStrip out all characters that are not allowable in an email address.\u003cbr/\u003e\ne.g. `['email' =\u003e ['required', 'email', 'sanitize:email']`\n\n2. **`sanitize_file_name`**\u003cbr/\u003e\nSanitizes a file name by removing special characters.\u003cbr/\u003e\ne.g `['file' =\u003e ['required', 'string', 'sanitize:file_name']`\n\n3. **`sanitize_html_class`**\u003cbr/\u003e\nSanitize content with allowed HTML tags for class attribute.\u003cbr/\u003e\ne.g `['class' =\u003e ['required', 'string', 'sanitize:html_class']`\n\n4. **`sanitize_key`**\u003cbr/\u003e\nSanitize content with allowed HTML tags for key attribute.\u003cbr/\u003e\ne.g `['key' =\u003e ['required', 'string', 'sanitize:sanitize_key']`\n\n5. **`sanitize_text`**\u003cbr/\u003e\nStrip out all characters that are not allowable in a string.\u003cbr/\u003e\ne.g. `['name' =\u003e ['required', 'string', 'sanitize:text']`\n\n6. **`sanitize_textarea_field`**\u003cbr/\u003e\nSanitize content with allowed HTML tags for textarea field.\u003cbr/\u003e\ne.g `['content' =\u003e ['required', 'string', 'sanitize:textarea']`\n\n7. **`sanitize_title`**\u003cbr/\u003e\nStrip out all characters that are not allowable in a title.\u003cbr/\u003e\ne.g. `['title' =\u003e ['required', 'string', 'sanitize:title']`\n\n8. **`sanitize_user`**\u003cbr/\u003e\nSanitize a username, striping out unsafe characters.\u003cbr/\u003e\ne.g `['user' =\u003e ['required', 'string', 'sanitize:user']`\n\n9. **`sanitize_url`**\u003cbr/\u003e\nSanitizes a URL by removing invalid characters for safe use in HTML attributes.\u003cbr/\u003e\ne.g. `['url' =\u003e ['required', 'url', 'sanitize:url']`\n\n10. **`wp_kses`**\u003cbr/\u003e\nSanitize content with allowed HTML tags.\u003cbr/\u003e\ne.g `['content' =\u003e ['required', 'string', 'sanitize:wp_kses|a.href,a.title,br,em,strong']`\n\n11. **`wp_kses_post`**\u003cbr/\u003e\nSanitize content with allowed HTML tags for post content.\u003cbr/\u003e\ne.g `['content' =\u003e ['required', 'string', 'sanitize:wp_kses_post']`\n\n\n### Custom Validation Rule\n\nCreate the class of the validation rule into your project:\n\n```php\n\u003c?php\n\nuse BitApps\\WPValidator\\Rule;\n\nclass BooleanRule extends Rule\n{\n    // error message if fails...\n    private $message = \"The :attribute must be a boolean\";\n\n    public function validate($value)\n    {\n        // validation code here...\n        return is_bool($value);\n    }\n\n    public function message()\n    {\n        return $this-\u003emessage;\n    }\n}\n```\n\nPass them as an instance into the `$rules` array:\n\n```php\n$rules = [\n    'agreed' =\u003e ['required', new BooleanRule],\n];\n```\n### Customizing Error Messages\n\nWP Validator provides default error messages based on validation rules. For added flexibility, you can change these error messages globally or even for specific fields and validation rules:\n\n```php\n$customMessages = [\n    'required' =\u003e ':attribute is missing',\n    'string' =\u003e ':attribute cannot contain any numerics',\n    'between' =\u003e 'The :attribute must be given between :min \u0026 :max',\n    'size' =\u003e 'The account number must consist of :size characters',\n];\n```\nNow, for each validation rule, it will return the custom error messages you have set.\n**Note:** `:attribute` refers to the field it's currently validating, acting as a placeholder. We have more placeholders like this; explore them in the [List of Placeholders](#list-of-placeholders) section.\n\nIf you want more flexibility and wish to customize error messages individually for each validation rule and field, you can also achieve that:\n\n```php\n$customMessages = [\n    'first_name' =\u003e [\n        'required' =\u003e 'First name must be present',\n        'string' =\u003e 'You cannot include anything except letters in the first name',\n    ],\n    'email' =\u003e [\n        'email' =\u003e 'The provided email is not valid',\n    ],\n];\n```\n\nIf you use any other validation rules that you didn't mention in the Custom Messages array, WP Validator will follow the default error message.\n\n### List of Placeholders\n\n1. **`:attribute`**\u003cbr/\u003e\nIt will refer to the field name under validation \u0026 custom label if changed via `$attributes` array.\n2. **`:value`**\u003cbr/\u003e\nIt will refer to the value of the field under validation.\n3. **`:min`**\u003cbr/\u003e\nIt will refer to the min value parameter of `between`, `digits_between`, `min` validation rules.\n4. **`:max`**\u003cbr/\u003e\nIt will refer to the max value parameter of `between`, `digits_between`, `max` validation rules.\n5. **`:digits`**\u003cbr/\u003e\nIt will refer to the value parameter of `digits` validation rule.\n6. **`:other`**\u003cbr/\u003e\nIt will refer to the field parameter of `same` validation rule.\n7. **`:size`**\u003cbr/\u003e\nIt will refer to the value parameter of `size` validation rule.\n\n# Contributing\n\nWe welcome contributions from the community. If you find a bug or have a feature suggestion, please open an issue or submit a pull request.\n\n# License\n\nThis package is open-source and available under the MIT License.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbit-apps-pro%2Fwp-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbit-apps-pro%2Fwp-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbit-apps-pro%2Fwp-validator/lists"}