Validation -- a feature rich PHP parameter validator

Keywords: PHP regex composer

Validation -- a feature rich PHP parameter validator

Validation is used to check the validity of background parameters. Easy to use and intuitive.

https://github.com/gitHusband/Validation
If you have any opinions or ideas, we can communicate and discuss together!

As for why I wrote this tool:

    1. For background parameters, in theory, the validity of each parameter should be checked, especially those parameters that need to be forwarded to other API interfaces or stored in the database. For example, the database basically has restrictions on data length and type, and the verification of length is simple and cumbersome. Using this tool can greatly simplify the code.
    1. If there are too many parameters, the amount of code verified is bound to be too large, messy and not intuitive. Using this tool, you only need to customize a set of validation rule array, which is beautiful and intuitive.
    1. For some API interfaces requiring version control, the parameters of each version may be different. Using this tool, you can more intuitively record the overall format of parameters, and the parameters of each version can also be simplified into a set of verification rules.
    1. You can easily customize each validation method to return different error messages
    1. I can't think of it for the time being. I'll make it up for you when I think of it.

The following is a brief introduction to the usage of this tool:

// Instantiate the class and accept a configuration array, but it is not necessary
$validation = new Validation($config);

// Set validation rules and validate data
if($validation->set_rules($rule)->validate($data)) {
    // The test results are obtained here. If there are verified parameters, the value will be modified to true if successful, and the value will be modified to error if failed,
    // If there is no verified parameter, keep the original value unchanged.
    return $validation->get_result();
}else {
    // There are two parameters corresponding to different error message formats. There are four error messages to choose from.
    return $validation->get_error(true, false);
}
catalogue

1. Introduction

1.1 features

  • The meaning is clear and easy to understand. Use *, >, <, > =, len >, int, (n), (s) and other function flags, for example, (n) represents in_array, and must be a number
  • Support regular expression validation
  • Condition verification is supported. If the condition is met, subsequent rules will continue to be verified. If not, it indicates that this field is optional
  • Support serial connection (one parameter and multiple rules are satisfied at the same time, & &) and parallel connection (one parameter and multiple rules meet one of them, ||) verification
  • Support custom function validation
  • Support free parameter transfer during function verification, @ root (original data), @ parent (parent data of verification field), @ me (current field), @ anything (any field)
  • Support the verification of infinitely nested data structures, including associative arrays and indexed arrays
  • Support special validation rules
  • Support custom configuration, such as rule separator "|", parameter separator "," and so on
  • Support internationalization configuration, default English, support custom methods to return error information
  • It supports one-time verification of all parameters (default), or it can be set to end verification immediately after parameter verification fails
  • It supports user-defined error messages, error messages in multiple formats, infinite nesting or one-dimensional array error message formats
  • I can't think of it for the time being. I'll make it up for you when I think of it.

2. Installation

composer require githusband/validation

3. Complete example

// This is a global function. The age field in the rule will use this function, which needs to be defined in advance
function check_age($data, $gender, $param) {
    if($gender == "male") {
        if($data > $param) return false;
    }else {
        if($data < $param) return false;
    }

    return true;
}

function validate() {
    // Validation rules
    $rule = [
        "id" => "*|int|/^\d+$/",
        "name" => "*|string|len<=>:8,32",
        "gender" => "*|(s):male,female",
        "dob" => "*|dob",
        "age" => "*|check_age:@gender,30 >> @me is wrong",
        "height[||]" => [
            "*|=::@height_unit,cm|<=>=:100,200 >> @me should be in [100,200] when height_unit is cm",
            "*|=::@height_unit,m|<=>=:1,2 >> @me should be in [1,2] when height_unit is m",
        ],
        "height_unit" => "*|(s):cm,m",
        "weight[||]" => [
            "*|=::@weight_unit,kg|<=>=:40,100 >> @me should be in [40,100] when height_unit is kg",
            "*|=::@weight_unit,lb|<=>:88,220 >> @me should be in [88,220] when height_unit is lb",
        ],
        "weight_unit" => "*|(s):kg,lb",
        "email" => "*|email",
        "phone" => "*|/(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/ >> phone number error",
        "ip" => "O|ip",
        "mac" => "O|mac",
        "education" => [
            "primary_school" => "*|=:Qiankeng Xiaoxue",
            "junior_middle_school" => "*|!=:Foshan Zhongxue",
            "high_school" => "if?=::@junior_middle_school,Mianhu Zhongxue|*|len>:18",
            "university" => "if0?=::@junior_middle_school,Mianhu Zhongxue|*|len>:8",
        ],
        "company" => [
            "name" => "*|len<=>:8,64",
            "website" => "*|url",
            "country" => "O|len>=:6",
            "addr" => "*|len>:16",
            "postcode" => "O|len<:16|check_postcode::@parent",
            "colleagues.*" => [
                "name" => "*|string|len<=>:3,32",
                "position" => "*|(s):Reception,Financial,PHP,JAVA"
            ],
            "boss" => [
                "*|=:Mike",
                "*|(s):Johnny,David",
                "O|(s):Johnny,David"
            ]
        ],
        // [O] Indicates that an array or object is optional
        "favourite_food[O].*" => [
            "name" => "*|string",
            "place_name" => "O|string" 
        ]
    ];
    
    // Data to be verified
    $data = [
        "id" => "ABBC",
        "name" => "12",
        "gender" => "female2",
        "dob" => "2000-01-01",
        "age" => 11,
        "height" => 1.65,
        "height_unit" => "cm",
        "weight" => 80,
        "weight_unit" => "lb1",
        "email" => "10000@qq.com.123@qq",
        "phone" => "15620004000-",
        "ip" => "192.168.1.1111",
        "mac" => "06:19:C2:FA:36:2B111",
        "education" => [
            "primary_school" => "???Qiankeng Xiaoxue",
            "junior_middle_school" => "Foshan Zhongxue",
            "high_school" => "Mianhu Gaozhong",
            "university" => "Foshan",
        ],
        "company" => [
            "name" => "Qianken",
            "website" => "https://www.qiankeng.com1",
            "country" => "US",
            "addr" => "Foshan Nanhai",
            "postcode" => "532000",
            "colleagues" => [
                [
                    "name" => 1,
                    "position" => "Reception"
                ],
                [
                    "name" => 2,
                    "position" => "Financial1"
                ],
                [
                    "name" => 3,
                    "position" => "JAVA"
                ],
                [
                    "name" => "Kurt",
                    "position" => "PHP1"
                ],
            ],
            "boss" => [
                "Mike1",
                "David",
                "Johnny2",
                "Extra",
            ]
        ]
    ];
    
    // Simple custom configuration is not complete
    $validation_conf = [
        'language' => 'zh-cn',
        'validation_global' => true,
    ];
    
    // When instantiating a class, don't forget to reference the class file in advance
    // Accepts a configuration array, but it is not necessary
    $validation = new Validation($validation_conf);
    
    // Custom validation functions are supported, but not necessary
    $validation->add_method('check_postcode', function($company) {
        if(isset($company['country']) && $company['country'] == "US"){
            if(!isset($company['postcode']) || $company['postcode'] != "123"){
                // Three ways of returning errors are supported
                // return false;
                // return "#### check_postcode method error message(@me)";
                return array(
                    'error_type' => 'server_error',
                    'message' => '*** check_postcode method error message(@me)',
                    "extra" => "extra message"
                );
            }
        }
    
        // Only when true is returned, the verification is successful, otherwise it fails
        return true;
    });
    
    // Set validation rules and validate data
    if($validation->set_rules($rule)->validate($data)) {
        // The test results are obtained here. If there are verified parameters, the value will be modified to true if successful, and the value will be modified to error if failed,
        // If there is no verified parameter, keep the original value unchanged.
        return $validation->get_result();
    }else {
        // There are two parameters corresponding to different error message formats. There are four error messages to choose from.
        return $validation->get_error(true, false);
    }
}

// You can change the get_ The two parameters of error to find the appropriate error reporting format
// The $data in the example basically does not meet the $rule. You can change the value of $data to verify whether the rule is correct
print_r(validate());

Theoretically, the tool is used to verify complex data structures, but if you want to verify a single string, you can also, for example

$validation->set_rules("*|string")->validate("Hello World!");

Note: the complete test class Tests.php and unit test class Unit.php have been built in the src/Test directory.

Complete test class:

// Verify that the test is successful and return the test result:
php Tests.php run
// The verification test is successful, and the error message is returned:
php Tests.php error

Unit test class:

Contains tests for all functions, including only some built-in functions

In principle, run the unit test again after modifying the code to ensure normal function. If the test reports an error, locate the problem and solve it again.

// Test all examples:
php Unit.php run
// Test a single example, such as regular expression:
php Unit.php run test_regular_expression

4. Function introduction

4.1 clear meaning

For ease of understanding, some * * * signs * * * are used to represent the actual function.

// The name is required. It must be a string. The length must be greater than 3 and less than or equal to 32
"name" => "*|string|len<=>:3,32",

For example:

signmeaning
*It is required and cannot be empty
OOptional, can not be set or empty
O!Optional. It is not allowed to be set. Once set, it cannot be blank
>:20The number must be greater than 20
len<=>:2,16The character length must be greater than 2 and less than or equal to 16
ipMust be an ip address

See Appendix 1 for complete functions

4.2 support regular expression validation

It starts with "/" and ends with "/", indicating that it is a regular expression

// id must be a number
"id" => "*|/^\d+$/",

4.3 condition verification

The conditional verification flag is "if[01]? \?"

Positive condition: if? And if1?

  • If the condition holds, continue to verify the subsequent rules
  • If the condition is not true, it indicates that this field is optional: 1. If this field is empty, the verification success will be returned immediately; 2. If this field is not empty, continue to verify the subsequent rules
$rule = [
    "gender" => "*|(s):male,female",
    // If the gender is female, it is required to be older than 22 years old. If it is male, it is not required to be older
    "age" => "if?=::@gender,female|*|>:22",
],

No condition: if0?

  • If the condition is not true, continue to verify the subsequent rules
  • If the condition is true, the field is optional: 1. If the field is empty, the verification success will be returned immediately; 2. If this field is not empty, continue to verify the subsequent rules
$rule = [
    "gender" => "*|(s):male,female",
    // If the gender is not female, it is required to be over 22 years old. If it is female, there is no requirement for age
    "age" => "if0?=::@gender,female|*|>:22",
],

4.4 support series and parallel verification

  • Concatenation: one parameter and multiple rules are satisfied at the same time. The flag is|
  • Parallel connection: one parameter satisfies one of multiple rules, and the flag is {field name} + [|]
// parallel connection
"height[||]" => [
    // If the height unit is cm, the height must be greater than or equal to 100 and less than or equal to 200 
    "*|=::@height_unit,cm|<=>=:100,200 >> @me should be in [100,200] when height_unit is cm",
    // If the height unit is m, the height must be greater than or equal to 1 and less than or equal to 2
    "*|=::@height_unit,m|<=>=:1,2 >> @me should be in [1,2] when height_unit is m",
],
// In series, the height unit is required and must be cm or m
"height_unit" => "*|(s):cm,m",

4.5 user defined function verification

This tool has built-in many verification functions, such as *, >, len > =, IP, etc

If the built-in function cannot meet the requirements, or the validation rules are too complex, you can use custom function validation

User defined function validation supports two methods:

  • Custom interface: add_method
$validation->add_method('check_postcode', function($company) {
    if(isset($company['country']) && $company['country'] == "US"){
        if(!isset($company['postcode']) || $company['postcode'] != "123"){
            return false;
        }
    }

    return true;
});
  • Global function

The priority of the three functions is

add_ Method > built in functions > global functions

If the function does not exist, an error is reported.

The use of functions allows free transfer of parameters, and each parameter is separated by ","

parametermeaning
@rootThe representative parameter is the entire validation data $data
@parentRepresents that the parameter is the parent element of the current field
@meRepresents that the parameter is the current field
@field_nameThe representative parameter is the field name in the whole validation data_ Name field
valueRepresents that the parameter is a value string and can be null

The flags of the parameter are ':' and '::'

If @ me does not exist, the @ me parameter will be automatically added to the first parameter

If @ me does not exist, it will not be automatically added

4.6 verification of infinitely nested data structures

Support the verification of infinitely nested data structures, including associative arrays and index arrays, such as:

$data = [
    "name" => "Johnny",
    "favourite_color" => {
        "white",
        "red"
    },
    "favourite_fruits" => {
        [
            "name" => "apple",
            "color" => "red",
            "shape" => "circular"
        ],
        [
            "name" => "banana",
            "color" => "yellow",
            "shape" => "long strip"
        ],
    }
]

// To verify the above $data, the rule can be written like this
$rule = [
    "name" => "*|len>:4",
    "favourite_color" => [
        "*|len>:4",
        "*|len>:4",
    ],
    "favourite_fruits.*" => [
        "name" => "*|len>:4",
        "color" => "*|len>:4",
        "shape" => "*|len>:4"
    ]
]

Did you find it?

Associative arrays and ordinary index arrays can be written normally. If the elements in the index array are associative arrays, you need to add the ". *" flag after the field.

Optional array rule

Sometimes, the array is optional, but once set, the child elements must be verified according to the rules. At this time, you only need to add "[O]" flag after the array field name to indicate that the array is optional, such as:

"favourite_fruits[O].*" => [
    "name" => "*|len>:4",
    "color" => "*|len>:4",
    "shape" => "*|len>:4"
]

4.7 support special validation rules

Special rules supported are:

"[O]" - indicates that a single field or array is optional.
Note that it indicates that a single field can be far away. You can add O to the field rule.

$rule = [
    "name" => "O|string",
    "gender" => [ "[O]" => "string" ],
    // favourite_fruit is optional and must be an array if it exists
    "favourite_fruit[O]" => [
        "name" => "*|string",
        "color" => "*|string"
    ],
    // Equivalent to writing on
    "favourite_meat" => [
        "[O]" => [
            "name" => "*|string",
            "from" => "*|string"
        ]
    ],
];

"[|]" - indicates that a single field is a or rule, and multiple rules can meet one of them.

$rule = [
    // name can be a Boolean value or a Boolean string
    "name[||]" => [
        "*|bool",
        "*|bool_str",
    ],
    // Equivalent to writing on
    "height" => [
        "[||]" => [
            "*|int|>:100",
            "*|string",
        ]
    ]
];

". *" - indicates that the field is an indexed array.

When the flag of the index array starts with. It can be omitted if the flag does not follow the field name

$rule = [
    "person" => [
        // Indicates that person is an indexed array and person. * is an associative array
        // In this case, you can omit... And write only*
        "*" => [
            "name" => "*|string",
            // Indicates that person. * relation is an associative array
            "relation" => [
                "father" => "*|string",
                "mother" => "O|string",
                "brother" => [
                    // Indicates that person. * relationship. * brother is an optional index array
                    "[O].*" => [
                        // Indicates that person. * relationship. * brother. * is an index array
                        "*" => [
                            "name" => "*|string",
                            "level" => [
                                "[||]" => [
                                    "*|int",
                                    "*|string",
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            "fruit" => [
                "*" => [
                    "*" => [
                        "name" => "*|string",
                        "color" => "O|string",
                    ]

                ]
            ],
        ]
    ],
];

// The format of validation data is as follows
$data = [
    "person" => [
        [
            "name" => "Devin", 
            "relation" => [
                "father" => "fDevin", 
                "mother" => "mDevin",
                "brother" => [
                    [
                        ["name" => "Tom", "level" => 1],
                        ["name" => "Mike", "level" => "Second"],
                    ]
                ]
            ],
            "fruit" => [
                [
                    ["name" => "Apple", "color" => "Red"],
                    ["name" => "Banana", "color" => "Yellow"],
                ],
                [
                    ["name" => "Cherry", "color" => "Red"],
                    ["name" => "Orange", "color" => "Yellow"],
                ]
            ]
        ],
        [
            "name" => "Johnny", 
            "relation" => ["father" => "fJohnny", "mother" => "mJohnny"],
            "fruit" => [
                [
                    ["name" => "Apple", "color" => "Red"],
                    ["name" => "Banana", "color" => "Yellow"],
                ],
                [
                    ["name" => "Cherry", "color" => "Red"],
                    ["name" => "Orange", "color" => "Yellow"],
                ]
            ]
        ],
    ],
]

4.8 support custom configuration

User defined configurations are supported:

$_config = array(
    'language' => 'en-us',                  // Language, default is en-us
    'lang_path' => '',                      // Customer Language file path
    'validation_global' => true,            // If true, validate all rules; If false, stop validating when one rule was invalid
    'auto_field' => "data",                 // If root data is string or numberic array, add the auto_field to the root data, can validate these kind of data type.
    'reg_msg' => '/ >> (.*)$/',             // Set special error msg by user 
    'reg_preg' => '/^(\/.+\/.*)$/',         // If match this, using regular expression instead of method
    'reg_if' => '/^if[01]?\?/',             // If match this, validate this condition first
    'reg_if_true' => '/^if1?\?/',           // If match this, validate this condition first, if true, then validate the field
    'reg_if_false' => '/^if0\?/',           // If match this, validate this condition first, if false, then validate the field
    'symbol_rule_separator' => '|',         // Rule reqarator for one field
    'symbol_param_classic' => ':',          // If set function by this symbol, will add a @me parameter at first 
    'symbol_param_force' => '::',           // If set function by this symbol, will not add a @me parameter at first 
    'symbol_param_separator' => ',',        // Parameters separator, such as @me,@field1,@field2
    'symbol_field_name_separator' => '.',   // Field name separator, suce as "fruit.apple"
    'symbol_required' => '*',               // Symbol of required field
    'symbol_optional' => 'O',               // Symbol of optional field, can be unset or empty
    'symbol_unset' => 'O!',                 // Symbol of optional field, can only be unset
    'symbol_or' => '[||]',                  // Symbol of or rule
    'symbol_array_optional' => '[O]',       // Symbol of array optional rule
    'symbol_numeric_array' => '.*',         // Symbol of association array rule
);

For example:

$validation_conf = array(
    'language' => 'en-us',                  // Language, default is en-us
    'lang_path' => '/my_path/',             // Customer Language file path
    'validation_global' => true,            // If true, validate all rules; If false, stop validating when one rule was invalid.
    'auto_field' => "param",                // If root data is string or numberic array, add the auto_field to the root data, can validate these kind of data type.
    'reg_msg' => '/ >>>(.*)$/',             // Set special error msg by user 
    'reg_preg' => '/^Reg:(\/.+\/.*)$/',     // If match this, using regular expression instead of method
    'reg_if' => '/^IF[yn]?\?/',             // If match this, validate this condition first
    'reg_if_true' => '/^IFy?\?/',           // If match this, validate this condition first, if true, then validate the field
    'reg_if_false' => '/^IFn\?/',           // If match this, validate this condition first, if false, then validate the field
    'symbol_or' => '[or]',                  // Symbol of or rule
    'symbol_rule_separator' => '&&',        // Rule reqarator for one field
    'symbol_param_classic' => '~',          // If set function by this symbol, will add a @me parameter at first 
    'symbol_param_force' => '~~',           // If set function by this symbol, will not add a @me parameter at first 
    'symbol_param_separator' => ',',        // Parameters separator, such as @me,@field1,@field2
    'symbol_field_name_separator' => '->',  // Field name separator, suce as "fruit.apple"
    'symbol_required' => '!*',              // Symbol of required field
    'symbol_optional' => 'o',               // Symbol of optional field, can be unset or empty
    'symbol_unset' => 'O!',                 // Symbol of optional field, can only be unset
    'symbol_array_optional' => '[o]',       // Symbol of array optional
    'symbol_numeric_array' => '[N]',        // Symbol of association array
);

The relevant rules can be written as follows:

$rule = [
    "id" => "!*&&int&&Reg:/^\d+$/i",
    "name" => "!*&&string&&len<=>~8,32",
    "gender" => "!*&&(s)~male,female",
    "dob" => "!*&&dob",
    "age" => "!*&&check_age~@gender,30 >>>@me is wrong",
    "height_unit" => "!*&&(s)~cm,m",
    "height[or]" => [
        "!*&&=~~@height_unit,cm&&<=>=~100,200 >>>@me should be in [100,200] when height_unit is cm",
        "!*&&=~~@height_unit,m&&<=>=~1,2 >>>@me should be in [1,2] when height_unit is m",
    ],
    "education" => [
        "primary_school" => "!*&&=~Qiankeng Xiaoxue",
        "junior_middle_school" => "!*&&!=~Foshan Zhongxue",
        "high_school" => "IF?=~~@junior_middle_school,Mianhu Zhongxue&&!*&&len>~10",
        "university" => "IFn?=~~@junior_middle_school,Qiankeng Zhongxue&&!*&&len>~10",
    ],
    "company" => [
        "name" => "!*&&len<=>~8,64",
        "country" => "o&&len>=~3",
        "addr" => "!*&&len>~16",
        "colleagues[N]" => [
            "name" => "!*&&string&&len<=>~3,32",
            "position" => "!*&&(s)~Reception,Financial,PHP,JAVA"
        ],
        "boss" => [
            "!*&&=~Mike",
            "!*&&(s)~Johnny,David",
            "o&&(s)~Johnny,David"
        ]
    ],
    "favourite_food[o][N]" => [
        "name" => "!*&&string",
        "place_name" => "o&&string" 
    ]
];

4.9 support international configuration

The configuration file name and class name are named by the big hump.

Standard language codes such as "zh CN" and "en US" are supported when calling.

Currently, the supported languages are "zh CN" and "en US". The default language is "en US" English.

// Call interface
$validation->set_language('zh-cn'); //The ZhCn.php configuration file will be loaded

// Or add configuration when instantiating the class
$validation_conf = [
    'language' => 'zh-cn',
];

$validation = new Validation($validation_conf);

Custom internationalization file

Such as / MyPath/MyLang.php.

The contents are as follows:

<?php

class MyLang
{
    public $error_template = array(
        'check_custom' => '@me error!(CustomLang File)'
    );
}

Modify language file path

// You should add CustomLang.php in '/MyPath/'
$validation->set_config(array('lang_path' => /MyPath/'))->set_language('MyLang');

In fact, the internationalization configuration configures the error information returned by each verification function, and you can freely overwrite and increase the error information returned by your verification function.

// Must be an object
$lang_config = (object)array();
$lang_config->error_template = array(
    'check_id' => '@me error!(customed)'
);

$validation->custom_language($lang_config);

The above adds a check to the error template_ ID, if check_ If the ID function is verified incorrectly, the information is returned

'@me error!(customed)'

4.10 support one-time verification of all parameters

It supports one-time verification of all parameters (default), or it can be set to end verification immediately after parameter verification fails

// Call interface
$validation->set_validation_global(false);

// Or add configuration when instantiating the class
$validation_conf = [
    'validation_global' => false,
];

$validation = new Validation($validation_conf);

4.11 support custom error messages

The flag of custom error message is "> >", and pay attention to the space before and after.

For example:

"phone" => "*|/(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/ >> phone number error",

Custom functions can also customize error messages, with priority lower than "> >"

When the return value of the function = = = true, the verification is successful; otherwise, the verification fails

Therefore, the function allows three types of error returns:

  1. Directly return false
  2. Returns an error message string
  3. Returns the error information array. By default, there are two fields, error_type and message, supporting custom fields
function check_age($data, $gender, $param) {
    if($gender == "male") {
        // if($data > $param) return false;
        if($data > $param) return "@me should be greater than @p1 when gender is male";
    }else {
        if($data < $param) return array(
            'error_type' => 'server_error',
            'message' => '@me should be less than @p1 when gender is female',
            "extra" => "extra message"
        );
    }

    return true;
}

4.12 support multiple error message formats

In the case that verification returns immediately once an error occurs, there are two error message formats that can be returned:

Returns an error message string

$validation->get_error(false, true);

"id Must be integer"

Returns the error information array. By default, there are two fields, error_type and message, supporting custom fields

$validation->get_error(false, false);

{
    "error_type": "validation",
    "message": "id Must be integer"
}

If all fields are verified, two error message formats can be returned:

See Appendix 2 for details

Appendix 1

signmeaning
*If necessary, @me cannot be empty
OOptional, can not be set or empty
O!Optional. It is not allowed to be set. Once set, it cannot be blank
=@me must be equal to @ p1
!=@me must not be equal to @ p1
==@me must be all equal to @ p1
!==@me must not be all equal to @ p1
>@me must be greater than @ p1
<@me must be less than @ p1
>=@me must be greater than or equal to @ p1
<=@me must be less than or equal to @ p1
<>@me must be greater than @ p1 and less than @ p2
<=>@me must be greater than @ p1 and less than or equal to @ p2
<>=@me must be greater than or equal to @ p1 and less than @ p2
<=>=@me must be greater than or equal to @ p1 and less than or equal to @ p2
(n)@me must be a number within @p1
!(n)@me must be a number and not in this range @p1
(s)@me must be a string within @p1
!(s)@me must be a string and not within @p1
len=@me length must be equal to @ p1
len!=@me length must not be equal to @ p1
len>@me length must be greater than @ p1
len<@me length must be less than @ p1
len>=@me length must be greater than or equal to @ p1
len<=@me length must be less than or equal to @ p1
len<>@me length must be greater than @ p1 and less than @ p2
len<=>@me length must be greater than @ p1 and less than or equal to @ p2
len<>=@me length must be greater than or equal to @ p1 and less than @ p2
len<=>=@me length must be greater than or equal to @ p1 and less than or equal to @ p2
int@me must be an integer
float@me must be a decimal
string@me must be a string
arr@me must be an array,
bool@me must be Boolean
bool=@me must be Boolean and equal to @ p1
bool_str@me must be a Boolean string
bool_str=@me must be a Boolean string and equal to @ p1
email@me must be a mailbox
url@me must be a web address
ip@me must be an IP address
mac@me must be a MAC address
dob@me must be the correct date
file_base64@me must be the base64 code of the correct file
uuid@me must be UUID

Appendix 2

First kind

$validation->get_error(false, true);

{
    "id": "id Must be integer",
    "height": "height should be in [100,200] when height_unit is cm or height should be in [1,2] when height_unit is m",
    "company.postcode": "*** check_postcode method error message(company.postcode)",
    "company.colleagues.0.name": "company.colleagues.0.name Must be a string",
    "company.colleagues.1.name": "company.colleagues.1.name Must be a string",
    "company.colleagues.1.position": "company.colleagues.1.position Must be a string and within Reception,Financial,PHP,JAVA",
    "company.colleagues.2.name": "company.colleagues.2.name Must be a string",
    "company.colleagues.3.position": "company.colleagues.3.position Must be a string and within Reception,Financial,PHP,JAVA",
    "company.boss.0": "company.boss.0 Must be equal to Mike",
    "company.boss.2": "company.boss.2 Must be a string and within Johnny,David"
}

Second

$validation->get_error(false, false);

{
    "id": {
        "error_type": "validation",
        "message": "id Must be integer"
    },
    "height": {
        "error_type": "validation",
        "message": "height should be in [100,200] when height_unit is cm or height should be in [1,2] when height_unit is m"
    },
    "company.postcode": {
        "error_type": "server_error",
        "message": "*** check_postcode method error message(company.postcode)",
        "extra": "extra message"
    },
    "company.colleagues.0.name": {
        "error_type": "validation",
        "message": "company.colleagues.0.name Must be a string"
    },
    "company.colleagues.1.name": {
        "error_type": "validation",
        "message": "company.colleagues.1.name Must be a string"
    },
    "company.colleagues.1.position": {
        "error_type": "validation",
        "message": "company.colleagues.1.position Must be a string and within Reception,Financial,PHP,JAVA"
    },
    "company.colleagues.2.name": {
        "error_type": "validation",
        "message": "company.colleagues.2.name Must be a string"
    },
    "company.colleagues.3.position": {
        "error_type": "validation",
        "message": "company.colleagues.3.position Must be a string and within Reception,Financial,PHP,JAVA"
    },
    "company.boss.0": {
        "error_type": "validation",
        "message": "company.boss.0 Must be equal to Mike"
    },
    "company.boss.2": {
        "error_type": "validation",
        "message": "company.boss.2 Must be a string and within Johnny,David"
    }
}

Third

$validation->get_error(true, true);

{
    "id": "id Must be integer",
    "height": "height should be in [100,200] when height_unit is cm or height should be in [1,2] when height_unit is m",
    "company": {
        "postcode": "*** check_postcode method error message(company.postcode)",
        "colleagues": [
            {
                "name": "company.colleagues.0.name Must be a string"
            },
            {
                "name": "company.colleagues.1.name Must be a string",
                "position": "company.colleagues.1.position Must be a string and within Reception,Financial,PHP,JAVA"
            },
            {
                "name": "company.colleagues.2.name Must be a string"
            },
            {
                "position": "company.colleagues.3.position Must be a string and within Reception,Financial,PHP,JAVA"
            }
        ],
        "boss": {
            "0": "company.boss.0 Must be equal to Mike",
            "2": "company.boss.2 Must be a string and within Johnny,David"
        }
    }
}

Fourth

$validation->get_error(true, false);

{
    "id": {
        "error_type": "validation",
        "message": "id Must be integer"
    },
    "height": {
        "error_type": "validation",
        "message": "height should be in [100,200] when height_unit is cm or height should be in [1,2] when height_unit is m"
    },
    "company": {
        "postcode": {
            "error_type": "server_error",
            "message": "*** check_postcode method error message(company.postcode)",
            "extra": "extra message"
        },
        "colleagues": [
            {
                "name": {
                    "error_type": "validation",
                    "message": "company.colleagues.0.name Must be a string"
                }
            },
            {
                "name": {
                    "error_type": "validation",
                    "message": "company.colleagues.1.name Must be a string"
                },
                "position": {
                    "error_type": "validation",
                    "message": "company.colleagues.1.position Must be a string and within Reception,Financial,PHP,JAVA"
                }
            },
            {
                "name": {
                    "error_type": "validation",
                    "message": "company.colleagues.2.name Must be a string"
                }
            },
            {
                "position": {
                    "error_type": "validation",
                    "message": "company.colleagues.3.position Must be a string and within Reception,Financial,PHP,JAVA"
                }
            }
        ],
        "boss": {
            "0": {
                "error_type": "validation",
                "message": "company.boss.0 Must be equal to Mike"
            },
            "2": {
                "error_type": "validation",
                "message": "company.boss.2 Must be a string and within Johnny,David"
            }
        }
    }
}

Posted by pstevereynolds on Fri, 19 Nov 2021 19:19:15 -0800