From 33e5ef59f50145f6c01c6364e5c997cfccc9ebb1 Mon Sep 17 00:00:00 2001 From: Muhammad Syifa Date: Fri, 31 Jul 2020 05:48:50 +0700 Subject: [PATCH 1/9] added explanation about size rule with/without numeric rule --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bd1972a..303855e 100644 --- a/README.md +++ b/README.md @@ -713,7 +713,7 @@ This rule also using `in_array`. You can enable strict checking by invoking vali The field under this rule must have a size greater or equal than the given number. -For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For an array, size corresponds to the count of the array. +For string value, size corresponds to the number of characters. For integer or float value, size corresponds to its numerical value. For an array, size corresponds to the count of the array. If your value is numeric string, you can put `numeric` rule to treat its size by numeric value instead of number of characters. You can also validate uploaded file using this rule to validate minimum size of uploaded file. For example: From 8e3f97b5995bd721d7af1014390bc76b61e47e8d Mon Sep 17 00:00:00 2001 From: Muhammad Syifa Date: Fri, 31 Jul 2020 07:00:56 +0700 Subject: [PATCH 2/9] fix typo ValidationTest --- tests/{ValidatonTest.php => ValidationTest.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/{ValidatonTest.php => ValidationTest.php} (97%) diff --git a/tests/ValidatonTest.php b/tests/ValidationTest.php similarity index 97% rename from tests/ValidatonTest.php rename to tests/ValidationTest.php index 4f8f69d..1a917b5 100644 --- a/tests/ValidatonTest.php +++ b/tests/ValidationTest.php @@ -7,7 +7,7 @@ use PHPUnit\Framework\TestCase; use ReflectionClass; -class ValidatonTest extends TestCase +class ValidationTest extends TestCase { /** * @param string $rules From 2c557d27816ddbab59bccabbc7e298a87ad37615 Mon Sep 17 00:00:00 2001 From: Muhammad Syifa Date: Fri, 31 Jul 2020 08:55:49 +0700 Subject: [PATCH 3/9] setup coveralls --- .travis.yml | 5 ++++- composer.json | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index c4ffee3..7224ddc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,7 @@ before_script: - ./vendor/bin/phpcs -n --standard=PSR2 src/ tests/ script: - - ./vendor/bin/phpunit --coverage-text + - ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml + +after_script: + - travis_retry php vendor/bin/php-coveralls -v diff --git a/composer.json b/composer.json index d61350f..f55a8ba 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,8 @@ }, "require-dev": { "phpunit/phpunit": "^6.5", - "squizlabs/php_codesniffer": "^3" + "squizlabs/php_codesniffer": "^3", + "php-coveralls/php-coveralls": "^2.2" }, "scripts": { "test": [ From 18111d6eaf509468513e35cd245f23129c76fa61 Mon Sep 17 00:00:00 2001 From: Muhammad Syifa Date: Fri, 31 Jul 2020 09:00:40 +0700 Subject: [PATCH 4/9] added coveralls badge --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 303855e..a5d89ec 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ Rakit Validation - PHP Standalone Validation Library ====================================================== [![Build Status](https://img.shields.io/travis/rakit/validation.svg?style=flat-square)](https://travis-ci.org/rakit/validation) +[![Coverage Status](https://coveralls.io/repos/github/rakit/validation/badge.svg?branch=setup_coveralls)](https://coveralls.io/github/rakit/validation?branch=setup_coveralls) [![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://doge.mit-license.org) From 64522b3f048a2bf5b5f0c4ef5385cbcd7ab267b2 Mon Sep 17 00:00:00 2001 From: Muhammad Syifa Date: Fri, 31 Jul 2020 09:08:48 +0700 Subject: [PATCH 5/9] fix coveralls badge branch --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a5d89ec..c5fbaa3 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Rakit Validation - PHP Standalone Validation Library ====================================================== [![Build Status](https://img.shields.io/travis/rakit/validation.svg?style=flat-square)](https://travis-ci.org/rakit/validation) -[![Coverage Status](https://coveralls.io/repos/github/rakit/validation/badge.svg?branch=setup_coveralls)](https://coveralls.io/github/rakit/validation?branch=setup_coveralls) +[![Coverage Status](https://coveralls.io/repos/github/rakit/validation/badge.svg?branch=setup_coveralls)](https://coveralls.io/github/rakit/validation) [![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://doge.mit-license.org) From 810a063572a48708e1300594e6285a6a5c3bf9c3 Mon Sep 17 00:00:00 2001 From: Agbayewa Adetunji Date: Sun, 9 Aug 2020 02:01:58 +0100 Subject: [PATCH 6/9] Improve SizeTraits for Integers Resolves #85 - Users can now validate min and max for integers with this update --- src/Rules/Traits/SizeTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rules/Traits/SizeTrait.php b/src/Rules/Traits/SizeTrait.php index fb4139a..9ada7f5 100644 --- a/src/Rules/Traits/SizeTrait.php +++ b/src/Rules/Traits/SizeTrait.php @@ -16,7 +16,7 @@ trait SizeTrait protected function getValueSize($value) { if ($this->getAttribute() - && $this->getAttribute()->hasRule('numeric') + && ($this->getAttribute()->hasRule('numeric') || $this->getAttribute()->hasRule('integer')) && is_numeric($value) ) { $value = (float) $value; From d61090c64959022057ed60212b8bc9c87d127922 Mon Sep 17 00:00:00 2001 From: Hammed Oyedele Date: Mon, 24 Aug 2020 11:31:13 +0100 Subject: [PATCH 7/9] feat: add boolean rule to the core rules closes #113 --- README.md | 94 +++++++++++++++++++++++-------------------- src/Rules/Boolean.php | 23 +++++++++++ src/Validator.php | 3 +- 3 files changed, 75 insertions(+), 45 deletions(-) create mode 100644 src/Rules/Boolean.php diff --git a/README.md b/README.md index c5fbaa3..ad4c3fb 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,8 @@ composer require "rakit/validation" #### Usage -There are two ways to validating data with this library. Using `make` to make validation object, -then validate it using `validate`. Or just use `validate`. +There are two ways to validating data with this library. Using `make` to make validation object, +then validate it using `validate`. Or just use `validate`. Examples: Using `make`: @@ -112,7 +112,7 @@ if ($validation->fails()) { ``` -In this case, 2 examples above will output the same results. +In this case, 2 examples above will output the same results. But with `make` you can setup something like custom invalid message, custom attribute alias, etc before validation running. @@ -234,7 +234,7 @@ $validation_a->validate(); #### Custom Message for Specific Attribute Rule -Sometimes you may want to set custom message for specific rule attribute. +Sometimes you may want to set custom message for specific rule attribute. To do this you can use `:` as message separator or using chaining methods. Examples: @@ -270,12 +270,12 @@ $validation_a->validate(); ## Translation -Translation is different with custom messages. +Translation is different with custom messages. Translation may needed when you use custom message for rule `in`, `not_in`, `mimes`, and `uploaded_file`. -For example if you use rule `in:1,2,3` we will set invalid message like "The Attribute only allows '1', '2', or '3'" +For example if you use rule `in:1,2,3` we will set invalid message like "The Attribute only allows '1', '2', or '3'" where part "'1', '2', or '3'" is comes from ":allowed_values" tag. -So if you have custom Indonesian message ":attribute hanya memperbolehkan :allowed_values", +So if you have custom Indonesian message ":attribute hanya memperbolehkan :allowed_values", we will set invalid message like "Attribute hanya memperbolehkan '1', '2', or '3'" which is the "or" word is not part of Indonesian language. So, to solve this problem, we can use translation like this: @@ -295,7 +295,7 @@ $validation = $validator->validate($inputs, [ 'nomor' => 'in:1,2,3' ]); -$message = $validation->errors()->first('nomor'); // "Nomor hanya memperbolehkan '1', '2', atau '3'" +$message = $validation->errors()->first('nomor'); // "Nomor hanya memperbolehkan '1', '2', atau '3'" ``` > Actually, our built-in rules only use words 'and' and 'or' that you may need to translates. @@ -319,7 +319,7 @@ Get all messages as flatten array. Examples: ```php -$messages = $errors->all(); +$messages = $errors->all(); // [ // 'Email is not valid email', // 'Password minimum 6 character', @@ -341,7 +341,7 @@ Get only first message from all existing keys. Examples: ```php -$messages = $errors->firstOfAll(); +$messages = $errors->firstOfAll(); // [ // 'email' => Email is not valid email', // 'password' => 'Password minimum 6 character', @@ -354,13 +354,13 @@ $messages = $errors->firstOfAll('
  • :message
  • '); // ] ``` -Argument `$dotNotation` is for array validation. +Argument `$dotNotation` is for array validation. If it is `false` it will return original array structure, if it `true` it will return flatten array with dot notation keys. For example: ```php -$messages = $errors->firstOfAll(':message', false); +$messages = $errors->firstOfAll(':message', false); // [ // 'contacts' => [ // 1 => [ @@ -470,7 +470,7 @@ The field under this validation must be present and not 'empty'. Here are some examples: | Value | Valid | -|---------------|-------| +| ------------- | ----- | | `'something'` | true | | `'0'` | true | | `0` | true | @@ -524,18 +524,18 @@ The field under validation must be present and not empty only when all of the ot
    uploaded_file:min_size,max_size,extension_a,extension_b,... -This rule will validate data from `$_FILES`. +This rule will validate data from `$_FILES`. Field under this rule must be follows rules below to be valid: -* `$_FILES['key']['error']` must be `UPLOAD_ERR_OK` or `UPLOAD_ERR_NO_FILE`. For `UPLOAD_ERR_NO_FILE` you can validate it with `required` rule. +* `$_FILES['key']['error']` must be `UPLOAD_ERR_OK` or `UPLOAD_ERR_NO_FILE`. For `UPLOAD_ERR_NO_FILE` you can validate it with `required` rule. * If min size is given, uploaded file size **MUST NOT** be lower than min size. * If max size is given, uploaded file size **MUST NOT** be higher than max size. * If file types is given, mime type must be one of those given types. Here are some example definitions and explanations: -* `uploaded_file`: uploaded file is optional. When it is not empty, it must be `ERR_UPLOAD_OK`. -* `required|uploaded_file`: uploaded file is required, and it must be `ERR_UPLOAD_OK`. +* `uploaded_file`: uploaded file is optional. When it is not empty, it must be `ERR_UPLOAD_OK`. +* `required|uploaded_file`: uploaded file is required, and it must be `ERR_UPLOAD_OK`. * `uploaded_file:0,1M`: uploaded file size must be between 0 - 1 MB, but uploaded file is optional. * `required|uploaded_file:0,1M,png,jpeg`: uploaded file size must be between 0 - 1MB and mime types must be `image/jpeg` or `image/png`. @@ -600,7 +600,7 @@ The `$_FILES` item under validation must have a MIME type corresponding to one o
    default/defaults -This is special rule that doesn't validate anything. +This is special rule that doesn't validate anything. It just set default value to your attribute if that attribute is empty or not present. For example if you have validation like this @@ -684,15 +684,15 @@ The field under this rule may have alpha characters, as well as spaces. The field under this rule must be included in the given list of values. -This rule is using `in_array` to check the value. -By default `in_array` disable strict checking. +This rule is using `in_array` to check the value. +By default `in_array` disable strict checking. So it doesn't check data type. If you want enable strict checking, you can invoke validator like this: ```php $validation = $validator->validate($data, [ 'enabled' => [ - 'required', + 'required', $validator('in', [true, 1])->strict() ] ]); @@ -712,7 +712,7 @@ This rule also using `in_array`. You can enable strict checking by invoking vali
    min:number -The field under this rule must have a size greater or equal than the given number. +The field under this rule must have a size greater or equal than the given number. For string value, size corresponds to the number of characters. For integer or float value, size corresponds to its numerical value. For an array, size corresponds to the count of the array. If your value is numeric string, you can put `numeric` rule to treat its size by numeric value instead of number of characters. @@ -731,7 +731,7 @@ $validation = $validator->validate([
    max:number -The field under this rule must have a size lower or equal than the given number. +The field under this rule must have a size lower or equal than the given number. Value size calculated in same way like `min` rule. You can also validate uploaded file using this rule to validate maximum size of uploaded file. @@ -749,7 +749,7 @@ $validation = $validator->validate([
    between:min,max -The field under this rule must have a size between min and max params. +The field under this rule must have a size between min and max params. Value size calculated in same way like `min` and `max` rule. You can also validate uploaded file using this rule to validate size of uploaded file. @@ -797,7 +797,7 @@ $validation = $validator->validate($inputs, [ ]); ``` -> For common URL scheme and mailto, we combine `FILTER_VALIDATE_URL` to validate URL format and `preg_match` to validate it's scheme. +> For common URL scheme and mailto, we combine `FILTER_VALIDATE_URL` to validate URL format and `preg_match` to validate it's scheme. Except for JDBC URL, currently it just check a valid JDBC scheme.
    @@ -807,6 +807,12 @@ The field under t rule must be integer.
    +
    boolean + +The field under this rule must be boolean. Accepted input are `true`, `false`, `1`, `0`, `"1"`, and `"0"`. + +
    +
    ip The field under this rule must be valid ipv4 or ipv6. @@ -911,7 +917,7 @@ $validation = $validator->validate($_POST, [ ]); ``` -You can set invalid message by returning a string. +You can set invalid message by returning a string. For example, example above would be: ```php @@ -931,7 +937,7 @@ $validation = $validator->validate($_POST, [ ]); ``` -> Note: `Rakit\Validation\Rules\Callback` instance is binded into your Closure. +> Note: `Rakit\Validation\Rules\Callback` instance is binded into your Closure. So you can access rule properties and methods using `$this`.
    @@ -944,10 +950,10 @@ Field under this rule may be empty. ## Register/Override Rule -Another way to use custom validation rule is to create a class extending `Rakit\Validation\Rule`. +Another way to use custom validation rule is to create a class extending `Rakit\Validation\Rule`. Then register it using `setValidator` or `addValidator`. -For example, you want to create `unique` validator that check field availability from database. +For example, you want to create `unique` validator that check field availability from database. First, lets create `UniqueRule` class: @@ -959,36 +965,36 @@ use Rakit\Validation\Rule; class UniqueRule extends Rule { protected $message = ":attribute :value has been used"; - + protected $fillableParams = ['table', 'column', 'except']; - + protected $pdo; - + public function __construct(PDO $pdo) { $this->pdo = $pdo; } - + public function check($value): bool { // make sure required parameters exists $this->requireParameters(['table', 'column']); - + // getting parameters $column = $this->parameter('column'); $table = $this->parameter('table'); $except = $this->parameter('except'); - + if ($except AND $except == $value) { return true; } - + // do query $stmt = $this->pdo->prepare("select count(*) as count from `{$table}` where `{$column}` = :value"); $stmt->bindParam(':value', $value); $stmt->execute(); $data = $stmt->fetch(PDO::FETCH_ASSOC); - + // true for valid, false for invalid return intval($data['count']) === 0; } @@ -1022,7 +1028,7 @@ $params['column'] = 'email'; $params['except'] = 'exception@mail.com'; ``` -> If you want your custom rule accept parameter list like `in`,`not_in`, or `uploaded_file` rules, +> If you want your custom rule accept parameter list like `in`,`not_in`, or `uploaded_file` rules, you just need to override `fillParameters(array $params)` method in your custom rule class. Note that `unique` rule that we created above also can be used like this: @@ -1046,25 +1052,25 @@ use Rakit\Validation\Rule; class UniqueRule extends Rule { ... - + public function table($table) { $this->params['table'] = $table; return $this; } - + public function column($column) { $this->params['column'] = $column; return $this; } - + public function except($value) { $this->params['except'] = $value; return $this; } - + ... } @@ -1098,11 +1104,11 @@ class YourCustomRule extends Rule protected $implicit = true; } -``` +``` #### Modify Value -In some case, you may want your custom rule to be able to modify it's attribute value like our `default/defaults` rule. So in current and next rules checks, your modified value will be used. +In some case, you may want your custom rule to be able to modify it's attribute value like our `default/defaults` rule. So in current and next rules checks, your modified value will be used. To do this, you should implements `Rakit\Validation\Rules\Interfaces\ModifyValue` and create method `modifyValue($value)` to your custom rule class. diff --git a/src/Rules/Boolean.php b/src/Rules/Boolean.php new file mode 100644 index 0000000..24d49d1 --- /dev/null +++ b/src/Rules/Boolean.php @@ -0,0 +1,23 @@ +validators[$key])? $this->validators[$key] : null; + return isset($this->validators[$key]) ? $this->validators[$key] : null; } /** @@ -137,6 +137,7 @@ protected function registerBaseValidators() 'between' => new Rules\Between, 'url' => new Rules\Url, 'integer' => new Rules\Integer, + 'boolean' => new Rules\Boolean, 'ip' => new Rules\Ip, 'ipv4' => new Rules\Ipv4, 'ipv6' => new Rules\Ipv6, From f147c66a8741ee9c9aa4b988d72933af71d869c1 Mon Sep 17 00:00:00 2001 From: Hammed Oyedele Date: Mon, 24 Aug 2020 11:53:00 +0100 Subject: [PATCH 8/9] ci: write test for the boolean rule --- tests/Rules/BooleanTest.php | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/Rules/BooleanTest.php diff --git a/tests/Rules/BooleanTest.php b/tests/Rules/BooleanTest.php new file mode 100644 index 0000000..8c4b2e2 --- /dev/null +++ b/tests/Rules/BooleanTest.php @@ -0,0 +1,37 @@ +rule = new Boolean; + } + + public function testValids() + { + $this->assertTrue($this->rule->check(\true)); + $this->assertTrue($this->rule->check(\false)); + $this->assertTrue($this->rule->check(1)); + $this->assertTrue($this->rule->check(0)); + $this->assertTrue($this->rule->check('1')); + $this->assertTrue($this->rule->check('0')); + $this->assertTrue($this->rule->check('y')); + $this->assertTrue($this->rule->check('n')); + } + + public function testInvalids() + { + $this->assertFalse($this->rule->check(11)); + $this->assertFalse($this->rule->check([])); + $this->assertFalse($this->rule->check('foo123')); + $this->assertFalse($this->rule->check('123foo')); + $this->assertFalse($this->rule->check([123])); + $this->assertFalse($this->rule->check('123.456')); + $this->assertFalse($this->rule->check('-123.456')); + } +} From c52a4be059be970bbaeba031a413857105034441 Mon Sep 17 00:00:00 2001 From: Hammed Oyedele Date: Mon, 24 Aug 2020 11:53:20 +0100 Subject: [PATCH 9/9] fix: make all boolean tests pass --- src/Rules/Boolean.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rules/Boolean.php b/src/Rules/Boolean.php index 24d49d1..8c52e18 100644 --- a/src/Rules/Boolean.php +++ b/src/Rules/Boolean.php @@ -18,6 +18,6 @@ class Boolean extends Rule */ public function check($value): bool { - return \in_array($value, [\true, \false, "true", "false", "0", "1", "y", "n"]); + return \in_array($value, [\true, \false, "true", "false", 1, 0, "0", "1", "y", "n"], \true); } }