WebGeeker-Validation: a powerful PHP parameter validator
Project address: Github Code Cloud
Used to check the validity of API request parameters.
When implementing the API interface on the server side, check whether the value of each parameter of each interface is valid to avoid incorrect data input into the system. This task is time-consuming and laborious, but it has to be done. And PHP itself is a weakly typed language, which makes it more complicated to validate not only values but also data types.
This tool is designed for this work, can effectively reduce the amount of coding, code readability.
Take a look at the following code to get an idea of the usage, which should not be difficult to understand:
$params = $request->query(); // GET the GET argument
// Validate (if validation fails, an exception will be thrown)
Validation::validate($params, [
"offset"= >"IntGe:0"."count"= >"The Required | IntGeLe: 1200",]);Copy the code
Supports the verification of various data types: integer, floating point, bool, string, array, object, file, date and time, can verify the parameters in nested data structure, but also supports the verification with conditional judgment.
- directory
- 1 introduction
- 1.1 Why write such a tool?
- 1.2 the characteristics of
- 1.3 A simple example
- 2 the installation
- 3 getting started
- 3.1 A complete example (without any framework)
- 3.2 Error Handling of Failed Authentication
- 3.3 Usage in third-party Frameworks
- 4 Detailed usage
- 4.1 Verifying Integer Parameters
- 4.2 Verifying floating point Parameters
- 4.3 Verifying a bool Parameter
- 4.4 Verifying string Parameters
- 4.5 Validate array, object, file, date and time parameters
- 4.6 Validator series (and)
- 4.7 Required Validator
- 4.8 Ignore all Required validators
- 4.9 Verification of nested Parameters
- 4.10 Conditional judgment validator
- 4.11 Parallel Verification Rules (or)
- 4.12 About Special Values
null
.""
.0
.false
The problem of - 4.13 The relationship between basic data types and strings
- 4.14 Customizing error Message Output Text
- 4.15 the internationalization
- 4.16 Internationalization (prior to version 0.4)
- A Appendix – List of validators
- A. 1 integer
- A. two floating point
- A. 3 bool type
- A.4 The value is A string
- A. 5 array type
- A. 6 type object
- A. 7 file type
- A.8 Date and time type
- A.9 Conditional judgment type
- A.10 Other Validators
- 1 introduction
1 introduction
1.1 Why write such a tool?
When I used the Laravel framework, Laravel provided a parameter validation tool that didn’t work very well:
- Writing a validation class for each validation (inheriting XXX) is too cumbersome, and there will be many, many classes in the system; If these classes are reused in multiple places, or if there is a lot of inheritance between them in order to “reuse more” (reduce repetitive code), then the maintenance of these classes can be a big problem in itself;
- Validators have a “polysemy” problem. Let’s say it has one
size
Validator, which supports validation of string, integer, file and other types of parameters, for different data typessize
Have different meanings. This is just like when you recite English words, there are some English words, it has many, many meanings, different contexts have different meanings. Take the word ‘present’, which means present, present and gift. One of the most frustrating things about a word is not knowing what it means and not being able to remember it.
To solve these problems, a tool was written.
1.2 the characteristics of
- Concise, easy to see validation logic (see examples below)
- Lightweight, no validation classes need to be defined and maintained
- The semantics of the validator are clear and there is no “polysemy” problem
- Regular expression verification is supported
- Support for conditional validation
- Theoretically supports validation of infinitely nested parameters
- Easy to learn and easy to remember. For example, integer validators start with “Int”, floating-point validators start with “Float”, and so on. The only ones that don’t follow this rule are string validators, some of which start with “Str” and some of which don’t, for example
Regexp
.Ip
.Email
.Url
And so on. - No binding to any framework, no dependencies. You can use this tool in any framework, even if you don’t use a framework.
- Each feature has unit tests (41 Tests, 369 assertions)
1.3 A simple example
The following example shows the validation of a Request parameter for a query to get a user complaint list (using conditional validation and validation against nested data structures) :
// Validate the rule
$validations = [
"offset"= >"IntGe:0".// The offset argument should be greater than or equal to 0
"count"= >"The Required | IntGeLe: 1200".The count argument is required and is greater than or equal to 1 and less than or equal to 200
"type"= >"IntIn: 1, 2,".// The value of type can be 1 or 2
"state"= > ['IfIntEq:type,1|IntEq:0'.// If type==1, the state parameter must be 0
'IfIntEq: type, 2 | IntIn: 0,'.// If type==2 (user complaint), then state can take the following values: 1, 2, 3]."search.keyword"= >"StrLenGeLe:1,100".// search.keyword should be a string of [1, 100] length
"search.start_time"= >"Date".// search.start_time should be a string containing a valid date
"search.end_time"= >"BoolSmart".// search.end_time should be a string containing a valid date
];
// Parameters to be verified
$params = [
"offset"= >0.// start with record 0
"count"= >10.// Return a maximum of 10 records
"type"= >2.// 1- Criticism suggestions, 2- user complaints
"state"= >0.// 0- pending, 1- Processing, 2- processed
"search"= > [// Search criteria
"keyword"= >'Hardware failure'./ / key
"start_time"= >"2018-01-01".// Start date
"end_time"= >"2018-01-31".// End date]];// Validate (if validation fails, an exception will be thrown)
Validation::validate($params, $validations);
Copy the code
2 the installation
Install by Composer
Composer require webgeeker/validation: ^ 0.4Copy the code
3 getting started
3.1 A complete example (without any framework)
This example directly validates the parameters in $_POST (the POST form), demonstrating the most basic usage
include "vendor/autoload.php";
use WebGeeker\Validation\Validation;
try {
Validation::validate($_POST, [
"offset"= >"IntGe:0".// The offset argument should be greater than or equal to 0
"count"= >"The Required | IntGeLe: 1200".The count argument is required and is greater than or equal to 1 and less than or equal to 200
]);
} catch (\Exception $e) {
echo $e->getMessage();
}
Copy the code
Note: Failed validation will throw an exception that contains an error description
3.2 Error Handling of Failed Authentication
Validation::validate(…) Method throws exceptions. It is recommended to capture these exceptions at the framework level, extract error description information and return it to the client.
3.3 Usage in third-party Frameworks
Third-party frameworks typically provide Request objects that take GET and POST parameters (Laravel as an example).
//$params = $request->query(); // GET the GET argument
$params = $request->request->all(); // Get the POST argument
// Validate (if validation fails, an exception will be thrown)
Validation::validate($params, [
// Omit validation rule here
]);
Copy the code
4 Detailed usage
4.1 Verifying Integer Parameters
Integer validators all start with “Int” and are used to validate integer values (such as 123) or integer strings (such as “123”). All other data types do not match.
"size"= >"IntGeLe:1,100"
Copy the code
This validation requires that the parameter “size” be an integer that is greater than or equal to 1 and less than or equal to 100.
Refer to Appendix A.1 for A complete list of integer validators.
4.2 Verifying floating point Parameters
Floating-point validators all start with “Float” and are used to validate floating-point values (such as 1.0), floating-point strings (such as “1.0”), integer values (such as 123), or integer strings (such as “123”). All other data types do not match.
"height"= >"FloatGeLe: 0.0, 100.0,"
Copy the code
This validation requires that the argument “height” be a floating point number that is greater than or equal to 0 and less than or equal to 100.0.
See Appendix A.2 for A complete list of floating-point validators.
4.3 Verifying a bool Parameter
There are only two bool validators:
- Bool: The valid values are:
true
.false
."true"
."false"
(The string ignores case). - BoolSmart: The legal values are:
true
.false
."true"
."false"
.1
.0
."1"
."0"
."yes"
."no"
."y"
."n"
(The string ignores case)
case
"accept"= >"BoolSmart"
Copy the code
See Appendix A.3 for A complete list of bool validators.
4.4 Verifying string Parameters
String validators do not always start with “Str”. Only string data is received. All other data types are not matched.
Case 1:
"name"= >"StrLenGeLe: 2, 20"
Copy the code
This validation requires that the argument “name” be a string of length between 2 and 20 (the string length is calculated using mb_strlen()).
Example 2:
"comment"= >"ByteLenLe:1048576"
Copy the code
This validation requires that the argument “comment” be a string of no more than 1048576 bytes (the byte length is calculated using strlen()).
Example 3:
"email"= >"Email"
Copy the code
This verification requires that the parameter “email” be a valid email address.
Example 4 (regular expression validation) :
"phone"= >"Regexp:/^1(3[0-9]|4[579]|5[0-35-9]|7[0135678]|8[0-9]|66|9[89])\d{8}$/"
Copy the code
This verification requires that the parameter “phone” be a valid phone number.
To determine which special characters in a regular expression need to be escaped, use the preg_match() function. For example:
preg_match('/^string$/'.$string);
Copy the code
Then put two ‘/’, and its middle part of the copy out, put the Regexp: behind, don’t need to do additional escape, even in the regular ‘|’ the special symbols, also no longer need to escape.
See Appendix A.4 for A complete list of string validators.
4.5 Validate array, object, file, date and time parameters
Refer to Appendices A.5-A.8
4.6 Validator series (and)
A rule can have multiple validators in series, with a “AND” relationship, such as:
"file"= >"FileMaxSize:10m|FileImage"
Copy the code
This validation requires that the parameter “file” be an image file and that the file size does not exceed 10m
4.7 Required Validator
- The Required validator requires that the parameter must exist and that its value cannot be
null
This is PHPnull
Value instead of the string “null”) (the argument value isnull
This is equivalent to the argument not being present. - If multiple validators are connected in series, the Required validator must precede the other validators.
- If there are also conditional validators, Required must be concatenated after the conditional validator.
- If the validation rule is not Required, the validation is performed only when the parameter exists. If the validation fails, an exception will be thrown. If the parameter does not exist, it is not validated.
Ex. :
"size"= >"Required|StrIn:small,middle,large"
Copy the code
This validation requires that the parameter “size” must be “small”, “middle”, or “large” of the string.
4.8 Ignore all Required validators
For example, when creating a user, require the name, gender, age all must provide; However, when updating user information, you do not need to provide all information.
$validations = [
"name"= >"The Required | StrLenGeLe: 2, 20." "."sex"= >"The Required | IntIn: 0, 1"."age"= >"The Required | IntGeLe: 1200",]; $userInfo = ["name"= >"tom"."sex"= >"0"."age"= >"10",]; Validation::validate($userInfo, $validations);// Authentication when creating a user
unset($userInfo["age"]); // Delete the age field
Validation::validate($userInfo, $validations, true); // Verify when updating user information
Copy the code
Note the last line of the code above: the third argument to the validate() function is true to indicate that all Required validators are ignored.
In this way, we only need to write a validation rule, which can be used to create users and update user information.
4.9 Verification of nested Parameters
The following example shows validation of nested arguments containing arrays and objects:
$params = [
"comments" => [
[
"title"= >"title 1"."content"= >"content 1",], ["title"= >"title 1"."content"= >"content 1",], ["title"= >"title 1"."content"= >"content 1",]]]; $validations = ["comments[*].title"= >"The Required | StrLenGeLe: 2, 50." "."comments[*].content"= >"The Required | StrLenGeLe: 2500",]; Validation::validate($params, $validations);Copy the code
4.10 Conditional judgment validator
Conditional validators all start with “If”.
For example, if you want to recruit a number of models, the male requirements are 180 or above, the female requirements are 170 or above.
$validations = [
"sex"= >"StrIn:male,female"."height"= > ["IfStrEq:sex,male|IntGe:180"."IfStrEq:sex,female|IntGe:170",]];Copy the code
The validation rules for “height” are different depending on the value of “sex”.
See Appendix A.9 for A complete list of conditional validators.
4.11 Parallel Verification Rules (or)
Multiple verification rules can be connected in parallel. The relationship between them is “or”
"type"= > ["StrIn:small,middle,large"."IntIn: 1, 2, 3",]Copy the code
This validation requires that the parameter “type” be either a string of “small”, “middle”, or “large”, or an integer of 1, 2, or 3
The parallel connection of verification rules is not a simple “or” relationship. The verification process is as follows:
- Verify the rules in sequence. If one of the rules passes, the parameter passes.
- If all validation rules are ignored (If the validator condition is not met, or there is no Required validator and the parameter does not exist, or there are zero validation rules), the parameter is also validated.
- If neither of the preceding two parameters is met, the parameter verification fails.
These rules are not easy to completely untangle, so it is not recommended to use validation rules in parallel, and try not to design parameters that require such validation.
4.12 About Special Valuesnull
.""
.0
.false
The problem of
These special values are not equivalent, they are different data types (requiring different validators) :
""
It’s a string.0
Is an integer.false
Is a bool type.null
PHP is empty. It has a special meaning in this tool.
If the value of a parameter is null, the tool will treat the parameter as non-existent.
For example, the following two arrays are equivalent to this tool.
$params = [
"name"= >"hello",];Copy the code
with
$params = [
"name"= >"hello"."comment"= >null,];Copy the code
Is equivalent.
4.13 The relationship between basic data types and strings
For the following URL address
http://abc.com/index.php?p1=&&p2=hello&&p3=123
Copy the code
We will get an array of arguments:
$params = [
"p1"= >""."p2"= >"hello"."p3"= >"123",];Copy the code
Note:
- The value of parameter “p1” is an empty string
""
Rather thannull
. - The value of parameter “p3” is a string
"123"
Instead of an integer123
. - HTTP requests in GET mode cannot be passed
null
The value of.
All validators for this tool are strongly typed. “Int*” validates integers, “Float*” validates floating-point, and “Str*” validates strings. Data types do not match and validation cannot be passed. But the string type is an exception.
Because of regular HTTP requests, all basic data types are eventually converted to strings, so:
- The integer
123
And string"123"
Can pass the verification of the validator “Int”; - floating-point
123.0
And string"123.0"
Can pass the validator “Float” validation; - Bool type
true
And string"true"
All can pass the verification of the verifier “Bool”; - but
null
Values and Strings"null"
Never equivalent, string"null"
It’s just a regular string.
4.14 Customizing error Message Output Text
If the parameter is not validated, the Validation::validate() method throws an exception containing the text that describes the failed error message.
But this description text may not be so user friendly, we can customize this text with two pseudo-validators:
Alias
Custom parameter names (this name is combined with the internal error message template to produce the final error message description text)>>>
Use custom error description text (this text completely replaces the error description text generated by the template).
Look at the following example:
$params = [
"title"= >"a",]; Validation::validate($params, ["title"= >"The Required | StrLenGeLe: 2, 50." ",]);// The error description for throwing an exception is: "title" must be between 2 and 50 in length
Validation::validate($params, [
"title"= >"The Required | StrLenGeLe: 2, 50 | Alias: title".// Customize the parameter name
]); // The error that throws an exception is described as: the "header" must be between 2 and 50 in length
Validation::validate($params, [
"title"= >"The Required | StrLenGeLe: 2, 50 | > > > : title length should be between 2 ~ 50." ".// Customize the error message description text
]); // The title should be between 2 and 50 in length
Copy the code
Refer to Appendix A.10 for more detailed information
4.15 the internationalization
From version 0.4:
- Use a new static member variable
$langCode2ErrorTemplates
To translate the error message template, the main purpose is to simplify the format (thanks @githusband for the suggestion). - Old translation form
$langCodeToErrorTemplates
Still valid, existing code does not need to be modified (see next section). If the old and new translation tables are provided at the same time, the new one will be given priority, and the old one will be used if it cannot be found in the new table.
To support internationalization, you need to define a class that inherits \WebGeeker\Validation\Validation and overloads two static member variables:
$langCode2ErrorTemplates
Translation mapping table for error message templates. A complete list of error message templates is available at\WebGeeker\Validation\Validation::$errorTemplates
Found in the member variable$langCodeToTranslations
Used to provide “custom parameter names” (provided byAlias
Specified) and “custom error description text” (specified by>>>
Specified translation translation table.
An example class is provided below:
class MyValidation extends Validation
{
// Error message template translation table
protected static $langCodeToErrorTemplates = [
"zh-tw"= > ['Int'= >'{{param}}' must be an integer './ / 🌝
'IntGt'= >'{{param}}' must be greater than {{min}}'.'Str'= >'{{param}}' must be a string ',]."en-us"= > ['Int'= >'{{param}} must be an integer'.'IntGt'= >'{{param}} must be greater than {{min}}'.'Str'= >'{{param}} must be a string',]];// Text translation mapping table
protected static $langCodeToTranslations = [
"zh-tw"= > ["Variable"= >"Variable"./ / 🌙
"Variables must be integers."= >"Variables must be integers."./ / ⭐]."en-us"= > ["Variable"= >"variable"."Variables must be integers."= >"variable must be an integer",]]; }Copy the code
Note:
- The language code is case-sensitive. You are advised to use all lowercase characters, such as “zh-cn” and “en-us”.
- The name of the language code is custom. You can give it any name you like, such as “ABC” (standard language code is recommended).
Using the MyValidation class for validation, you can translate the text.
MyValidation::setLangCode("zh-tw"); // Set the language code
MyValidation::validate(["var"= >1.0], [
"var"= >"Int".// No Alias, no >>>, only error message template translation (corresponding to 🌝 line)
]); // Raises an exception: "var" must be an integer
MyValidation::validate(["var"= >1.0], [
"var"= >"Int | Alias: variable.".// Alias, in addition to translating error message templates, will also translate parameter names (corresponding to the 🌙 line)
]); // Throws an exception: "variable" must be an integer
MyValidation::validate(["var"= >1.0], [
"var"= >"Int | > > > : variables must be integers".// There are >>>, will translate the custom error description text (corresponding to ⭐ line)
]); // Raises an exception: the variable must be an integer
Copy the code
If the wrong language code is provided, or the translated text is not found, the translation is not performed and the original text is printed.
4.16 Internationalization (prior to version 0.4)
(If you are using version 0.4 or later, the new internationalization scheme (see the previous section) is recommended to be a little more concise.)
To support internationalization, you need to define a class that inherits \WebGeeker\Validation\Validation and overloads two static member variables:
$langCodeToErrorTemplates
Translation mapping table for error message templates. A complete list of error message templates is available at\WebGeeker\Validation\Validation::$errorTemplates
Found in the member variable$langCodeToTranslations
Used to provide “custom parameter names” (provided byAlias
Specified) and “custom error description text” (specified by>>>
Specified translation translation table.
An example class is provided below:
class MyValidation extends Validation
{
// Error message template translation table
protected static $langCodeToErrorTemplates = [
"zh-tw"= > ["{{param}} 'must be an integer"= >"{{param}} 'must be an integer"./ / 🌝
"' {{param}} 'must be a string"= >"' {{param}} 'must be a string",]."en-us"= > ["{{param}} 'must be an integer"= >"{{param}} must be a integer"."' {{param}} 'must be a string"= >"{{param}} must be a string",]];// Text translation mapping table
protected static $langCodeToTranslations = [
"zh-tw"= > ["Variable"= >"Variable"./ / 🌙
"Variables must be integers."= >"Variables must be integers."./ / ⭐]."en-us"= > ["Variable"= >"variable"."Variables must be integers."= >"variable must be an integer",]]; }Copy the code
Note:
- The language code is case-sensitive. You are advised to use all lowercase characters, such as “zh-cn” and “en-us”.
- The name of the language code is custom. You can give it any name you like, such as “ABC” (standard language code is recommended).
Using the MyValidation class for validation, you can translate the text.
MyValidation::setLangCode("zh-tw"); // Set the language code
MyValidation::validate(["var"= >1.0], [
"var"= >"Int".// No Alias, no >>>, only error message template translation (corresponding to 🌝 line)
]); // Raises an exception: "var" must be an integer
MyValidation::validate(["var"= >1.0], [
"var"= >"Int | Alias: variable.".// Alias, in addition to translating error message templates, will also translate parameter names (corresponding to the 🌙 line)
]); // Throws an exception: "variable" must be an integer
MyValidation::validate(["var"= >1.0], [
"var"= >"Int | > > > : variables must be integers".// There are >>>, will translate the custom error description text (corresponding to ⭐ line)
]); // Raises an exception: the variable must be an integer
Copy the code
If the wrong language code is provided, or the translated text is not found, the translation is not performed and the original text is printed.
A Appendix – List of validators
A. 1 integer
Integer validators all begin with “Int”.
Integer validator | The sample | instructions |
---|---|---|
Int | Int | “{{param}}” must be an integer |
IntEq | IntEq:100 | “{{param}}” must equal {{value}} |
IntGt | IntGt:100 | “{{param}}” must be greater than {{min}} |
IntGe | IntGe:100 | “{{param}}” must be greater than or equal to {{min}} |
IntLt | IntLt:100 | “{{param}}” must be less than {{Max}} |
IntLe | IntLe:100 | “{{param}}” must be less than or equal to {{Max}} |
IntGtLt | IntGtLt:1,100 | “{{param}}” must be greater than {{min}} and less than {{Max}}. |
IntGeLe | IntGeLe:1,100 | “{{param}}” must be greater than {{min}} and less than {{Max}}. |
IntGtLe | IntGtLe:1,100 | “{{param}}” must be greater than {{min}} and less than or equal to {{Max}} |
IntGeLt | IntGeLt:1,100 | “{{param}}” must be greater than or equal to {{min}} less than {{Max}} |
IntIn | ,3,5,7,11 IntIn: 2 | “{{param}}” can only take these values: {{valueList}} |
IntNotIn | ,3,5,7,11 IntNotIn: 2 | “{{param}}” cannot take these values: {{valueList}} |
A. two floating point
Double is used internally
Floating-point validators | The sample | instructions |
---|---|---|
Float | Float | ‘{{param}}’ must be a floating point number |
FloatGt | FloatGt: 1.0 | “{{param}}” must be greater than {{min}} |
FloatGe | FloatGe: 1.0 | “{{param}}” must be greater than or equal to {{min}} |
FloatLt | FloatLt: 1.0 | “{{param}}” must be less than {{Max}} |
FloatLe | FloatLe: 1.0 | “{{param}}” must be less than or equal to {{Max}} |
FloatGtLt | ,1.0 FloatGtLt: 0 | “{{param}}” must be greater than {{min}} and less than {{Max}}. |
FloatGeLe | ,1.0 FloatGeLe: 0 | “{{param}}” must be greater than {{min}} and less than {{Max}}. |
FloatGtLe | ,1.0 FloatGtLe: 0 | “{{param}}” must be greater than {{min}} and less than or equal to {{Max}} |
FloatGeLt | ,1.0 FloatGeLt: 0 | “{{param}}” must be greater than or equal to {{min}} less than {{Max}} |
A. 3 bool type
Bool validator | The sample | instructions |
---|---|---|
Bool | Bool | The valid value is:true .false ."true" ."false" (Ignore case) |
BoolSmart | BoolSmart | The valid value is:true .false ."true" ."false" .1 .0 ."1" ."0" ."yes" ."no" ."y" ."n" (Ignore case) |
A.4 The value is A string
String validators | The sample | instructions |
---|---|---|
Str | Str | {{param}} must be a string |
StrEq | StrEq:abc | “{{param}} “must equal “{{value}}” |
StrEqI | StrEqI:abc | “{{param}} “must be equal to “{{value}}” (ignore case) |
StrNe | StrNe:abc | “{{param}} “cannot equal “{{value}}” |
StrNeI | StrNeI:abc | “{{param}} “cannot equal “{{value}}” (ignore case) |
StrIn | StrIn:abc,def,g | “{{param}}” can only take these values: {{valueList}} |
StrInI | StrInI:abc,def,g | “{{param}}” can only take these values: {{valueList}} (ignore case) |
StrNotIn | StrNotIn:abc,def,g | “{{param}}” cannot take these values: {{valueList}} |
StrNotInI | StrNotInI:abc,def,g | {{valueList}} (ignore case) |
StrLen | StrLen:8 | The “{{param}}” length must be equal to {{length}} |
StrLenGe | StrLenGe:8 | {{param}} must be longer than {{min}}. |
StrLenLe | StrLenLe:8 | The length of {{param}} must be less than or equal to {{Max}} |
StrLenGeLe | StrLenGeLe: 6, 8 | The length of {{param}} must be between {{min}} – {{Max}} |
ByteLen | ByteLen:8 | “{{param}}” length (bytes) must equal {{length}} |
ByteLenGe | ByteLenGe:8 | The length of {{param}} must be greater than or equal to {{min}} |
ByteLenLe | ByteLenLe:8 | The length of {{param}} must be less than or equal to {{Max}}. |
ByteLenGeLe | ByteLenGeLe: 6, 8 | The length (bytes) of {{param}} must be between {{min}} – {{Max}} |
Letters | Letters | {{param}} contains only letters |
Alphabet | Alphabet | With the Letters |
Numbers | Numbers | {{param}} can only be pure digits |
Digits | Digits | With the Numbers |
LettersNumbers | LettersNumbers | {{param}} contains only letters and digits |
Numeric | Numeric | {{param}} must be a numeric value. Normally used for large numbers (numbers beyond the range of double are normally represented as strings) (not yet implemented for large numbers), and can be detected by ‘Int’ or ‘Float’ if they are in the normal range |
VarName | VarName | {{param}} contains only letters, digits, and underscores (_), and must start with a letter or underscore (_) |
{{param}} must be a valid email address | ||
Url | Url | {{param}} must be a valid Url |
Ip | Ip | {{param}} must be a valid IP address |
Mac | Mac | {{param}} must be a valid MAC address |
Regexp | Regexp:/^abc$/ | Perl regular expression matching |
A. 5 array type
Array validator | The sample | instructions |
---|---|---|
Arr | Arr | ‘{{param}}’ must be an array |
ArrLen | ArrLen:5 | “{{param}}” array length must be equal to {{length}} |
ArrLenGe | ArrLenGe:1 | “{{param}}” array length must be greater than or equal to {{min}} |
ArrLenLe | ArrLenLe:9 | “{{param}}” array length must be less than or equal to {{Max}} |
ArrLenGeLe | ArrLenGeLe: 1, 9 | The “{{param}}” long array degree must be between {{min}} ~ {{Max}} |
A. 6 type object
Object validator | The sample | instructions |
---|---|---|
Obj | Obj | ‘{{param}}’ must be an object |
A. 7 file type
File validator | The sample | instructions |
---|---|---|
File | File | ‘{{param}}’ must be a file |
FileMaxSize | FileMaxSize:10mb | “{{param}}” must be a file and the file size does not exceed {{size}}. |
FileMinSize | FileMinSize:100kb | “{{param}}” must be a file, and the file size is not smaller than {{size}}. |
FileImage | FileImage | “{{param}}” must be a picture |
FileVideo | FileVideo | {{param}} must be a video file |
FileAudio | FileAudio | ‘{{param}}’ must be an audio file |
FileMimes | FileMimes:mpeg,jpeg,png | “{{param}}” must be a file of these MIME types :{{mimes}} |
A.8 Date and time type
Date and time type validators | The sample | instructions |
---|---|---|
Date | Date | {{param}} must comply with the date format YYYY-MM-DD |
DateFrom | DateFrom:2017-04-13 | “{{param}}” must not be earlier than {{from}}. |
DateTo | DateTo:2017-04-13 | “{{param}}” shall not be later than {{to}} |
DateFromTo | DateFromTo: 2017-04-13, 2017-04-13 | {{param}} must be between {{from}} ~ {{to}} |
DateTime | DateTime | {{param}} must comply with the date and time format YYYY-MM-DD HH: MM: SS |
DateTimeFrom | DateTimeFrom:2017-04-13 12:00:00 | “{{param}}” must not be earlier than {{from}}. |
DateTimeTo | DateTimeTo:2017-04-13 12:00:00 | “{{param}}” must precede {{to}}. |
DateTimeFromTo | 12:00:00 12:00:00 DateTimeFromTo: 2017-04-13, 2017-04-13 | {{param}} must be between {{from}} ~ {{to}} |
A.9 Conditional judgment type
In a validation rule, conditional validators must precede other validators, and multiple conditional validators can be concatenated.
Note that the “condition” in a conditional judgment is usually to check the value of another parameter, whereas the value of the current parameter is verified by other validators in series following the conditional judgment validator.
Conditional judgment validator | The sample | instructions |
---|---|---|
If | If:selected | If the “selected” argument has a value equal to 1, true, ‘1’, ‘true’, ‘yes’ or ‘y'(the string is case-insensitive) |
IfNot | IfNot:selected | If the “selected” argument has a value equal to 0, false, ‘0’, ‘false’, ‘no’, or ‘n'(the string is case-insensitive) |
IfTrue | IfTrue:selected | If the value of “selected” equals true or ‘true'(case ignored) |
IfFalse | IfFalse:selected | If the value of “selected” is equal to false or ‘false'(case ignored) |
IfExist | IfExist:var | If the parameter “var” exists |
IfNotExist | IfNotExist:var | If the parameter “var” does not exist |
IfIntEq | IfIntEq:var,1 | if (var === 1) |
IfIntNe | IfIntNe:var,2 | if (var ! If (var) == 2). If (var) == 2). The other IfIntXx conditions fail If the var parameter does not match the data type |
IfIntGt | IfIntGt:var,0 | if (var > 0) |
IfIntLt | IfIntLt:var,1 | if (var < 0) |
IfIntGe | IfIntGe:var,6 | if (var >= 6) |
IfIntLe | IfIntLe:var,8 | if (var <= 8) |
IfIntIn | Hc-positie IfIntIn: var. 2 | If (in_array (var, hc-positie [2])) |
IfIntNotIn | Hc-positie IfIntNotIn: var. 2 | if (! Hc-positie [2]), the in_array (var) |
IfStrEq | IfStrEq:var,waiting | if (var === ‘waiting’) |
IfStrNe | IfStrNe:var,editing | if (var ! In particular, the If condition is true If the condition parameter var does not match the data type; The other IfStrXx conditions fail If the var data type does not match |
IfStrGt | IfStrGt:var,a | if (var > ‘a’) |
IfStrLt | IfStrLt:var,z | if (var < ‘z’) |
IfStrGe | IfStrGe:var,A | if (var >= ‘0’) |
IfStrLe | IfStrLe:var,Z | if (var <= ‘9’) |
IfStrIn | IfStrIn:var,normal,warning,error | if (in_array(var, [‘normal’, ‘warning’, ‘error’], true)) |
IfStrNotIn | IfStrNotIn:var,warning,error | if (! in_array(var, [‘warning’, ‘error’], true)) |
A.10 Other Validators
Other validators | The sample | instructions |
---|---|---|
Required | Required | The parameters to be validated are required. If the validators are concatenated, they must be the first validator in addition to the conditional validator |
Alias | Alias: indicates the parameter name | Custom parameter name in error text (must be the last validator) |
>>> | >>>: This is custom error text | Custom error text (alternative to the Alias validator, must be the last validator) |
Custom PHP functions | function() {} | This mechanism is not provided, because if you encounter complex parameter verification that this tool does not support, you can directly write PHP code to verify, do not need to go through this tool. |