wpmvc / request-validator
Installs: 5 411
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/wpmvc/request-validator
Requires (Dev)
This package is auto-updated.
Last update: 2025-10-29 03:18:49 UTC
README
Request Validator
The Request Validator provides a robust and extensible system for validating REST API requests in WordPress plugins. It supports a fluent rule-based syntax inspired by Laravel and integrates seamlessly with WP_REST_Request.
📦 Installation
If using standalone:
composer require wpmvc/validator
🧱 Structure Overview
| File | Purpose | 
|---|---|
Validator | 
Main validation handler class | 
Mime | 
Utility for validating uploaded file types | 
DateTime | 
Trait for handling date-based validation rules | 
🚀 Basic Usage
Inside a controller method:
public function store( Validator $validator, WP_REST_Request $request ) { $validator->validate([ 'title' => 'required|string|min:3|max:255', 'email' => 'required|email', 'price' => 'numeric|min:0', 'tags' => 'array', 'image' => 'file|mimes:png,jpg,jpeg|max:2048', 'launched' => 'date:Y-m-d|after_or_equal:2024-01-01', ]); // Validation passed; continue logic }
❗ If validation fails, a
WpMVC\Exceptions\Exceptionis thrown with HTTP status422and the error messages.
🛠 Available Rules
| Rule | Description | 
|---|---|
required | 
Field must be present and non-empty | 
string | 
Must be a string | 
email | 
Validates email format | 
numeric | 
Must be a numeric value | 
integer | 
Must be an integer | 
boolean | 
Must be true or false | 
array | 
Must be an array | 
uuid | 
Must be a valid UUID | 
url | 
Must be a valid URL | 
mac_address | 
Must be a valid MAC address | 
json | 
Must be a valid JSON string | 
confirmed | 
field_confirmation must match field | 
accepted | 
Value must be in allowed list | 
file | 
Checks file upload validity | 
mimes | 
Allowed file extensions (e.g. jpg,png) | 
max | 
Max length/size/value (string, numeric, file, array) | 
min | 
Min length/size/value (string, numeric, file, array) | 
date:format | 
Must be a date in given format | 
date_equals | 
Must exactly match a date | 
before | 
Must be before given date | 
after | 
Must be after given date | 
before_or_equal | 
Must be before or equal to given date | 
after_or_equal | 
Must be after or equal to given date | 
📁 File Validation Example
$validator->validate([ 'photo' => 'required|file|mimes:png,jpg|max:1024', ]);
- Validates that 
photois a file - Accepts only 
png,jpg - Maximum 1MB (1024 KB)
 
📅 Date Validation Example
$validator->validate([ 'launched' => 'required|date:Y-m-d|after_or_equal:2022-01-01', ]);
Supports custom formats and comparison logic using native PHP DateTime.
📋 Error Handling
If throw_errors is true (default), the validator throws an exception and returns a 422 JSON response:
{
  "message": "",
  "errors": {
    "email": ["The email field must be a valid email address."],
    "price": ["The price must be at least 0."]
  }
}
🔄 Manual Check
You can also use:
$validator->validate($rules, false); if ( $validator->is_fail() ) { return Response::send(['errors' => $validator->errors], 422); }
🔧 Internal Utilities
Mimeclass for file type validation usingmime_content_type.DateTimetrait for parsing and comparing dates based on format.get_format(),is_it_valid_date(),get_timestamp()handle date logic.