From 5e034f69eba37344b843069ae56e67e76a9a27c2 Mon Sep 17 00:00:00 2001 From: Sean Nieuwoudt Date: Sun, 10 Aug 2025 13:51:29 +0200 Subject: [PATCH 1/5] Added: additional validators - camel case, future date, hex colors, JWT token, coordinates, mac address, prime numbers, password & uuid --- README.md | 91 ++- gump.class.php | 726 +++++++++++++++++- lang/en.php | 47 ++ tests/Validators/CamelCaseValidatorTest.php | 54 ++ tests/Validators/FutureDateValidatorTest.php | 58 ++ tests/Validators/HexColorValidatorTest.php | 63 ++ tests/Validators/JwtTokenValidatorTest.php | 45 ++ tests/Validators/LatitudeValidatorTest.php | 69 ++ tests/Validators/MacAddressValidatorTest.php | 63 ++ tests/Validators/PrimeValidatorTest.php | 62 ++ .../StrongPasswordValidatorTest.php | 63 ++ tests/Validators/UuidValidatorTest.php | 63 ++ 12 files changed, 1397 insertions(+), 7 deletions(-) create mode 100644 tests/Validators/CamelCaseValidatorTest.php create mode 100644 tests/Validators/FutureDateValidatorTest.php create mode 100644 tests/Validators/HexColorValidatorTest.php create mode 100644 tests/Validators/JwtTokenValidatorTest.php create mode 100644 tests/Validators/LatitudeValidatorTest.php create mode 100644 tests/Validators/MacAddressValidatorTest.php create mode 100644 tests/Validators/PrimeValidatorTest.php create mode 100644 tests/Validators/StrongPasswordValidatorTest.php create mode 100644 tests/Validators/UuidValidatorTest.php diff --git a/README.md b/README.md index 5f2411e..5222576 100644 --- a/README.md +++ b/README.md @@ -205,7 +205,7 @@ $is_valid = GUMP::is_valid($data, [ ## Available Validators -GUMP provides **41 built-in validators** for comprehensive data validation: +GUMP provides **70+ built-in validators** for comprehensive data validation:
@@ -254,6 +254,54 @@ GUMP provides **41 built-in validators** for comprehensive data validation: | **valid_array_size_greater**,1 | Check if an input is an array and if the size is more or equal to a specific value. | | **valid_array_size_lesser**,1 | Check if an input is an array and if the size is less or equal to a specific value. | | **valid_array_size_equal**,1 | Check if an input is an array and if the size is equal to a specific value. | + +### 🔐 Security Validators +| **strong_password** | Validates password with uppercase, lowercase, number and special character (min 8 chars). | +| **jwt_token** | Validates JWT token format (3 base64url parts separated by dots). | +| **hash**,md5 | Validates hash format for specified algorithm (md5, sha1, sha256, sha512). | +| **no_sql_injection** | Detects common SQL injection patterns in input. | +| **no_xss** | Enhanced XSS detection beyond basic sanitize_string. | + +### 🌐 Modern Web Validators +| **uuid** | Validates UUID format (any version 1-5). | +| **base64** | Validates base64 encoded data. | +| **hex_color** | Validates hexadecimal color code (#FF0000 or #FFF format). | +| **rgb_color** | Validates RGB color format (rgb(255,0,0)). | +| **timezone** | Validates timezone identifier (America/New_York, UTC, etc.). | +| **language_code** | Validates language code (en, en-US format - ISO 639). | +| **country_code** | Validates country code (US, CA format - ISO 3166). | +| **currency_code** | Validates currency code (USD, EUR format - ISO 4217). | + +### 📡 Network Validators +| **mac_address** | Validates MAC address format (AA:BB:CC:DD:EE:FF or AA-BB-CC-DD-EE-FF). | +| **domain_name** | Validates domain name format (example.com - without protocol). | +| **port_number** | Validates port number (1-65535). | +| **social_handle** | Validates social media handle format (@username or username). | + +### 🗺️ Geographic Validators +| **latitude** | Validates latitude coordinate (-90 to 90). | +| **longitude** | Validates longitude coordinate (-180 to 180). | +| **postal_code**,US | Validates postal code for specified country (US, CA, UK, DE, FR, AU, JP). | +| **coordinates** | Validates coordinates in lat,lng format (40.7128,-74.0060). | + +### 📅 Enhanced Date/Time Validators +| **future_date** | Validates that date is in the future. | +| **past_date** | Validates that date is in the past. | +| **business_day** | Validates that date falls on a business day (Monday-Friday). | +| **valid_time** | Validates time format (HH:MM:SS or HH:MM). | +| **date_range**,2024-01-01;2024-12-31 | Validates date falls within specified range. | + +### 🔢 Mathematical Validators +| **even** | Validates that number is even. | +| **odd** | Validates that number is odd. | +| **prime** | Validates that number is prime. | + +### 📝 Content & Format Validators +| **word_count**,min,10,max,500 | Validates word count within specified range. | +| **camel_case** | Validates camelCase format (variableName). | +| **snake_case** | Validates snake_case format (variable_name). | +| **url_slug** | Validates URL slug format (my-url-slug). | +
## Comprehensive Validator Reference @@ -317,7 +365,7 @@ $rules = [ **API Payload Validation** ```php $rules = [ - 'user_id' => 'required|guidv4', + 'user_id' => 'required|uuid', 'email' => 'required|valid_email', 'metadata' => 'valid_json_string', 'permissions' => 'valid_array_size_greater,0', @@ -327,6 +375,45 @@ $rules = [ ]; ``` +**Security & Authentication Form** +```php +$rules = [ + 'password' => 'required|strong_password', + 'token' => 'required|jwt_token', + 'api_key' => 'required|hash,sha256', + 'input' => 'no_sql_injection|no_xss', + 'timezone' => 'timezone', + 'language' => 'language_code' +]; +``` + +**Geographic & Network Validation** +```php +$rules = [ + 'latitude' => 'required|latitude', + 'longitude' => 'required|longitude', + 'postal_code' => 'required|postal_code,US', + 'coordinates' => 'coordinates', + 'mac_address' => 'mac_address', + 'domain' => 'domain_name', + 'port' => 'port_number', + 'twitter' => 'social_handle' +]; +``` + +**Content & Format Validation** +```php +$rules = [ + 'variable_name' => 'required|snake_case', + 'functionName' => 'required|camel_case', + 'blog_slug' => 'required|url_slug', + 'article_body' => 'required|word_count,min,100,max,2000', + 'color_theme' => 'hex_color', + 'schedule_date' => 'required|future_date|business_day', + 'prime_number' => 'prime' +]; +``` + ### Advanced Validation Patterns **Conditional Validation** diff --git a/gump.class.php b/gump.class.php index 52b50f5..4299fe6 100644 --- a/gump.class.php +++ b/gump.class.php @@ -3,6 +3,22 @@ use GUMP\ArrayHelpers; use GUMP\EnvHelpers; +/** + * GUMP - A Fast PHP Data Validation & Filtering Library + * + * GUMP is a standalone PHP data validation and filtering library that makes validating + * any data easy and painless without the reliance on a framework. Supports 41 validators, + * 15+ filters, internationalization (19 languages), and custom validators/filters. + * + * @package GUMP + * @version 1.x + * @author Sean Nieuwoudt + * @copyright 2013-2025 Sean Nieuwoudt + * @license MIT + * @link https://github.com/wixel/gump + * + * @since 1.0 + */ class GUMP { /** @@ -88,8 +104,18 @@ public static function get_instance() // ** ------------------------- Validation Data ------------------------------- ** // + /** + * Basic HTML tags allowed in the basic_tags filter. + * + * @var string + */ public static $basic_tags = '