|
| 1 | +# hejunjie/schema-validator |
1 | 2 |
|
| 3 | +<div align="center"> |
| 4 | + <a href="./README.md">English</a>|<a href="./README.zh-CN.md">简体中文</a> |
| 5 | + <hr width="50%"/> |
| 6 | +</div> |
2 | 7 |
|
| 8 | +A simple and extensible PHP parameter validation library, supporting rule-based definitions and custom extensions, suitable for any structured data verification scenarios |
3 | 9 |
|
| 10 | +--- |
| 11 | + |
| 12 | +## 📦 Installation method |
| 13 | + |
| 14 | +Install using Composer: |
4 | 15 |
|
5 | | -### 🟦 类型类 |
| 16 | +```bash |
| 17 | +composer require hejunjie/schema-validator |
| 18 | +``` |
6 | 19 |
|
7 | | -- `StringRule`:内容必须为字符串 |
8 | | -- `IntegerRule`:内容必须为整数 |
9 | | -- `BooleanRule`:内容必须为布尔值 true/false |
10 | | -- `ArrayRule`:内容必须为数组 |
11 | | -- `ObjectRule`:内容必须为对象 |
12 | | -- `FloatRule`:内容必须为浮点数 |
13 | | -- `NumericRule`:内容必须为数字(包含整数、小数、科学计数法等) |
| 20 | +--- |
| 21 | + |
| 22 | +## 🚀 Usage |
| 23 | + |
| 24 | +Support multiple rule definitions + throw exceptions + custom extensions: |
| 25 | + |
| 26 | +```php |
| 27 | +use Hejunjie\SchemaValidator\Validator; |
| 28 | +use Hejunjie\SchemaValidator\Exceptions\ValidationException; |
| 29 | + |
| 30 | +$data = [ |
| 31 | + 'name' => '张三', |
| 32 | + 'age' => 28, |
| 33 | + 'email' => 'invalid-email', |
| 34 | +]; |
| 35 | + |
| 36 | +// Custom extension. If true is returned, the rule passes; otherwise, it is considered as failing |
| 37 | +Validator::extend('is_zh', function ($field, $value, $params = null) { |
| 38 | + if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $value)) { |
| 39 | + return true; |
| 40 | + } |
| 41 | +}); |
| 42 | + |
| 43 | +try { |
| 44 | + Validator::validate($data, [ |
| 45 | + 'name' => ['is_zh', 'string', 'minLength:2'], |
| 46 | + 'age' => ['integer', 'between:18,60'], |
| 47 | + 'email' => ['required', 'email'], |
| 48 | + ]); |
| 49 | + echo "Verified by ✅"; |
| 50 | +} catch (ValidationException $e) { |
| 51 | + echo "Validation failed ❌" . PHP_EOL; |
| 52 | + print_r($e->getErrors()); |
| 53 | +} |
| 54 | + |
| 55 | +// Return the fields and rules indicating whether it is passed or failed: |
| 56 | +// Validation failed ❌ |
| 57 | +// Array |
| 58 | +// ( |
| 59 | +// [email] => Array |
| 60 | +// ( |
| 61 | +// [0] => email |
| 62 | +// ) |
| 63 | + |
| 64 | +// ) |
| 65 | +``` |
14 | 66 |
|
15 | 67 | --- |
16 | 68 |
|
17 | | -### 🟩 比较类 |
| 69 | +## ✅ Default support rule |
| 70 | + |
| 71 | +The following rules are already supported in a built-in manner and are implemented as independent classes, allowing for free extension or replacement: |
18 | 72 |
|
19 | | -- `MinRule`:数值大小或字符串长度不允许小于指定值 |
20 | | -- `MaxRule`:数字大小或字符串长度不允许超过指定值 |
21 | | -- `BetweenRule`:数字大小或字符串长度必须在指定的最小值和最大值之间 |
22 | | -- `LengthRule`:字符串长度必须等于指定值 |
23 | | -- `MinLengthRule`:字符串的长度不允许小于指定值 |
24 | | -- `MaxLengthRule`:字符串的长度不允许超过指定值 |
25 | | -- `GtRule`:数字必须大于指定值 |
26 | | -- `LtRule`:数字必须小于指定值 |
27 | | -- `GteRule`:数字必须大于或等于指定值 |
28 | | -- `LteRule`:数字必须小于或等于指定值 |
| 73 | +### Type class |
| 74 | + |
| 75 | +| Rule Name | Function Description | Parameter Format | Example Usage | |
| 76 | +| ------------- | ------------------------------------------------------------------------------ | ---------------- | -------------------------- | |
| 77 | +| `StringRule` | Verify whether it is a string | `string` | `['param' => ['string']]` | |
| 78 | +| `IntegerRule` | Verify whether it is an integer | `integer` | `['param' => ['integer']]` | |
| 79 | +| `BooleanRule` | Verify whether it is a boolean value (true/false or 0/1) | `boolean` | `['param' => ['boolean']]` | |
| 80 | +| `ArrayRule` | Verify whether it is an array | `array` | `['param' => ['array']]` | |
| 81 | +| `ObjectRule` | Verify whether it is an object | `object` | `['param' => ['object']]` | |
| 82 | +| `FloatRule` | Verify whether it is a floating-point number | `float` | `['param' => ['float']]` | |
| 83 | +| `NumericRule` | Verify whether it is a number (including integer, floating-point string, etc.) | `numeric` | `['param' => ['numeric']]` | |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +### Compare class |
| 88 | + |
| 89 | +| Rule Name | Function Description | Parameter Format | Example Usage | |
| 90 | +| --------------- | -------------------------------------------------------------------------------------------------------- | ---------------- | -------------------------------- | |
| 91 | +| `MinRule` | The numerical value or string length cannot be less than the specified value | `min` | `['param' => ['min:2']]` | |
| 92 | +| `MaxRule` | The size of numbers or the length of strings are not allowed to exceed the specified value | `max` | `['param' => ['max:2']]` | |
| 93 | +| `BetweenRule` | The size of a number or the length of a string must fall within the specified minimum and maximum values | `between` | `['param' => ['between:18,60']]` | |
| 94 | +| `LengthRule` | The length of the string must be equal to the specified value | `length` | `['param' => ['length:10']]` | |
| 95 | +| `MinLengthRule` | The length of the string is not allowed to exceed the specified value | `min_length` | `['param' => ['min_length:2']]` | |
| 96 | +| `MaxLengthRule` | The length of the string cannot be less than the specified value | `max_length` | `['param' => ['max_length:20']]` | |
| 97 | +| `GtRule` | The number must be greater than the specified value | `gt` | `['param' => ['gt:2']]` | |
| 98 | +| `LtRule` | The number must be less than the specified value | `lt` | `['param' => ['lt:2']]` | |
| 99 | +| `GteRule` | The number must be greater than or equal to the specified value | `gte` | `['param' => ['gte:2']]` | |
| 100 | +| `LteRule` | The number must be less than or equal to the specified value | `lte` | `['param' => ['lte:2']]` | |
29 | 101 |
|
30 | 102 | --- |
31 | 103 |
|
32 | | -### 🟨 格式类 |
| 104 | +### Format class |
33 | 105 |
|
34 | | -- `EmailRule`:内容必须是邮箱格式 |
35 | | -- `MobileRule`:内容必须是中国大陆手机号格式 |
36 | | -- `UrlRule`:内容必须是URL |
37 | | -- `IpRule`:内容必须是有效的 IP 地址(IPv4 或 IPv6) |
38 | | -- `JsonRule`:内容必须是有效的Json字符串 |
39 | | -- `AlphaRule`:内容只能包含字母 |
40 | | -- `AlphaNumRule`:内容只能包含字母和数字 |
41 | | -- `AlphaDashRule`:内容只能包含字母、数字、破折号、下划线 |
| 106 | +| Rule Name | Function Description | Parameter Format | Example Usage | |
| 107 | +| --------------- | ------------------------------------------------------------------------- | ---------------- | ----------------------------- | |
| 108 | +| `EmailRule` | The content must be in email format | `email` | `['param' => ['email']]` | |
| 109 | +| `MobileRule` | The content must be in the format of a mainland China mobile phone number | `mobile` | `['param' => ['mobile']]` | |
| 110 | +| `UrlRule` | The content must be a URL | `url` | `['param' => ['url']]` | |
| 111 | +| `IpRule` | The content must be a valid IP address (IPv4 or IPv6) | `ip` | `['param' => ['ip']]` | |
| 112 | +| `JsonRule` | The content must be a valid JSON string | `json` | `['param' => ['json']]` | |
| 113 | +| `AlphaRule` | The content can only contain letters | `alpha` | `['param' => ['alpha']]` | |
| 114 | +| `AlphaNumRule` | The content can only contain letters and numbers | `alpha_num` | `['param' => ['alpha_num']]` | |
| 115 | +| `AlphaDashRule` | The content can only contain letters, numbers, dashes, and underscores | `alpha_dash` | `['param' => ['alpha_dash']]` | |
42 | 116 |
|
43 | 117 | --- |
44 | 118 |
|
45 | | -### 🟥 布尔类 |
| 119 | +### Boolean class |
46 | 120 |
|
47 | | -- `RequiredRule`:内容必须存在且不为空 |
48 | | -- `AcceptedRule`:内容只能为 yes、on、1、true |
49 | | -- `DeclinedRule`:内容只能为 no、off、0、false |
| 121 | +| Rule Name | Function Description | Parameter Format | Example Usage | |
| 122 | +| -------------- | ---------------------------------------------------- | ---------------- | --------------------------- | |
| 123 | +| `RequiredRule` | The content must exist and not be empty | `required` | `['param' => ['required']]` | |
| 124 | +| `AcceptedRule` | The content can only be "yes", "on", "1", or "true" | `accepted` | `['param' => ['accepted']]` | |
| 125 | +| `DeclinedRule` | The content can only be "no", "off", "0", or "false" | `declined` | `['param' => ['declined']]` | |
50 | 126 |
|
51 | 127 | --- |
52 | 128 |
|
53 | | -### 🟪 自定义类 |
| 129 | +### Custom class |
| 130 | + |
| 131 | +| Rule Name | Function Description | Parameter Format | Example Usage | |
| 132 | +| ---------------- | ------------------------------------------------ | ---------------- | ------------------------------ | |
| 133 | +| `StartsWithRule` | The content must start with the specified string | `starts_with` | `['param' => ['starts_with']]` | |
| 134 | +| `EndsWithRule` | The content must end with the specified string | `ends_with` | `['param' => ['ends_with']]` | |
| 135 | +| `ContainsRule` | The content must contain the specified string | `contains` | `['param' => ['contains']]` | |
| 136 | + |
| 137 | +> The error message is returned as an array of rule names, and the prompt text can be customized |
| 138 | +
|
| 139 | +--- |
| 140 | + |
| 141 | +## 🧩 Purpose & Original Intent |
| 142 | + |
| 143 | +In daily development, we often need to perform structured validation on incoming data, but many existing libraries are either bulky, rely on frameworks, or are not flexible in terms of extension (such as Laravel Validator). |
| 144 | + |
| 145 | +The goal of this library is to: |
| 146 | + |
| 147 | +- ✅ Zero dependencies, suitable for any PHP project |
| 148 | +- ✅ Validation for structured arrays |
| 149 | +- ✅ Each rule is encapsulated independently, facilitating customization and expansion |
| 150 | +- ✅ More suitable for field prompts and error handling in the Chinese context |
| 151 | + |
| 152 | +If you need a simple, clear, and rule-controlled data verification tool, it may be just right for you. |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +## 🙌 Welcome to contribute |
| 157 | + |
| 158 | +Welcome to raise issues, submit pull requests, or directly fork for use! |
54 | 159 |
|
55 | | -- `StartsWithRule`:内容必须以指定字符串开头 |
56 | | -- `EndsWithRule`:内容必须以指定字符串结尾 |
57 | | -- `ContainsRule`:内容必须包含指定字符串 |
| 160 | +If you have other commonly used validation rules, feel free to add them, even if it's just a line of regular expression. |
0 commit comments