Skip to content

Commit 01f76d8

Browse files
authored
Strict boolean and numeric as individual functions (#58)
* update: increased minimum laravel version * update: added strict boolean option * update: added strict numeric option * refactor: switch to separate rule function rather than flag * docs: added deprecation note
1 parent 6552777 commit 01f76d8

File tree

5 files changed

+128
-7
lines changed

5 files changed

+128
-7
lines changed

UPGRADE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## v6
44

5+
### Upgrading from v6.1 to v6.2
6+
7+
- Minimum Laravel version increased from `12.16` to `12.21`.
8+
- The `distinct` rule helper parameters have been deprecated in favor of the `distinctStrict` and `distinctIgnoreCase`
9+
helpers. The parameters are planned to be removed in a future major version.
10+
511
### Upgrading from v6.0 to v6.1
612

713
- Minimum Laravel version increased from `12.0` to `12.16`.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"minimum-stability": "stable",
77
"require": {
88
"php": "^8.2||^8.3||^8.4",
9-
"laravel/framework": "^12.16"
9+
"laravel/framework": "^12.21"
1010
},
1111
"require-dev": {
1212
"ext-json": "*",

src/Rule.php

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,16 @@ public static function boolean(): string
255255
return 'boolean';
256256
}
257257

258+
/**
259+
* The field under validation must a boolean.
260+
*
261+
* @link https://laravel.com/docs/12.x/validation#rule-boolean
262+
*/
263+
public static function booleanStrict(): string
264+
{
265+
return 'boolean:strict';
266+
}
267+
258268
/**
259269
* The field under validation must pass a Gate check for the specified ability.
260270
*
@@ -426,19 +436,43 @@ public static function dimensions(array $constraints = []): Dimensions
426436
*
427437
* @link https://laravel.com/docs/12.x/validation#rule-distinct
428438
*/
429-
public static function distinct(bool $strict = false, bool $ignoreCase = false): string
430-
{
439+
public static function distinct(
440+
/** @deprecated Use `distinctStrict()` instead */
441+
bool $strict = false,
442+
/** @deprecated Use `distinctIgnoreCase()` instead */
443+
bool $ignoreCase = false,
444+
): string {
431445
if ($ignoreCase) {
432-
return 'distinct:ignore_case';
446+
return self::distinctIgnoreCase();
433447
}
434448

435449
if ($strict) {
436-
return 'distinct:strict';
450+
return self::distinctStrict();
437451
}
438452

439453
return 'distinct';
440454
}
441455

456+
/**
457+
* When validating arrays, the field under validation must not have any duplicate values.
458+
*
459+
* @link https://laravel.com/docs/12.x/validation#rule-distinct
460+
*/
461+
public static function distinctIgnoreCase(): string
462+
{
463+
return 'distinct:ignore_case';
464+
}
465+
466+
/**
467+
* When validating arrays, the field under validation must not have any duplicate values.
468+
*
469+
* @link https://laravel.com/docs/12.x/validation#rule-distinct
470+
*/
471+
public static function distinctStrict(): string
472+
{
473+
return 'distinct:strict';
474+
}
475+
442476
/**
443477
* The field under validation must not end with one of the given values.
444478
*
@@ -973,6 +1007,17 @@ public static function numeric(): string
9731007
return 'numeric';
9741008
}
9751009

1010+
/**
1011+
* The field under validation must be numeric and typed as an integer or float.
1012+
*
1013+
* @link https://laravel.com/docs/12.x/validation#rule-numeric
1014+
* @link https://www.php.net/manual/en/function.is-numeric.php
1015+
*/
1016+
public static function numericStrict(): string
1017+
{
1018+
return 'numeric:strict';
1019+
}
1020+
9761021
/**
9771022
* The field under validation must be a string with an adequate level of complexity for a password. Defaults to a
9781023
* minimum of 8 characters if no size is provided and {@see Password::defaults} was not used.

src/RuleSet.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,16 @@ public function boolean(): self
299299
return $this->rule(Rule::boolean());
300300
}
301301

302+
/**
303+
* The field under validation must a boolean.
304+
*
305+
* @link https://laravel.com/docs/12.x/validation#rule-boolean
306+
*/
307+
public function booleanStrict(): self
308+
{
309+
return $this->rule(Rule::booleanStrict());
310+
}
311+
302312
/**
303313
* The field under validation must pass a Gate check for the specified ability.
304314
*
@@ -479,11 +489,35 @@ public function dimensions(array $constraints = [], ?callable $modifier = null):
479489
*
480490
* @link https://laravel.com/docs/12.x/validation#rule-distinct
481491
*/
482-
public function distinct(bool $strict = false, bool $ignoreCase = false): self
483-
{
492+
public function distinct(
493+
/** @deprecated Use `distinctStrict()` instead */
494+
bool $strict = false,
495+
/** @deprecated Use `distinctIgnoreCase()` instead */
496+
bool $ignoreCase = false,
497+
): self {
484498
return $this->rule(Rule::distinct($strict, $ignoreCase));
485499
}
486500

501+
/**
502+
* When validating arrays, the field under validation must not have any duplicate values.
503+
*
504+
* @link https://laravel.com/docs/12.x/validation#rule-distinct
505+
*/
506+
public function distinctIgnoreCase(): self
507+
{
508+
return $this->rule(Rule::distinctIgnoreCase());
509+
}
510+
511+
/**
512+
* When validating arrays, the field under validation must not have any duplicate values.
513+
*
514+
* @link https://laravel.com/docs/12.x/validation#rule-distinct
515+
*/
516+
public function distinctStrict(): self
517+
{
518+
return $this->rule(Rule::distinctStrict());
519+
}
520+
487521
/**
488522
* The field under validation must not end with one of the given values.
489523
*
@@ -1060,6 +1094,17 @@ public function numeric(): self
10601094
return $this->rule(Rule::numeric());
10611095
}
10621096

1097+
/**
1098+
* The field under validation must be numeric and typed as an integer or float.
1099+
*
1100+
* @link https://laravel.com/docs/12.x/validation#rule-numeric
1101+
* @link https://www.php.net/manual/en/function.is-numeric.php
1102+
*/
1103+
public function numericStrict(): self
1104+
{
1105+
return $this->rule(Rule::numericStrict());
1106+
}
1107+
10631108
/**
10641109
* The field under validation must be a string with an adequate level of complexity for a password. Defaults to a
10651110
* minimum of 8 characters if no size is provided and {@see Password::defaults} was not used.

tests/Unit/RuleTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,21 @@ public static function ruleDataProvider(): array
589589
'rules' => fn() => RuleSet::create()->boolean(),
590590
'fails' => true,
591591
],
592+
'booleanStrict valid' => [
593+
'data' => true,
594+
'rules' => fn() => RuleSet::create()->booleanStrict(),
595+
'fails' => false,
596+
],
597+
'booleanStrict invalid' => [
598+
'data' => 'false',
599+
'rules' => fn() => RuleSet::create()->booleanStrict(),
600+
'fails' => true,
601+
],
602+
'booleanStrict invalid number' => [
603+
'data' => '0',
604+
'rules' => fn() => RuleSet::create()->booleanStrict(),
605+
'fails' => true,
606+
],
592607
'can valid' => [
593608
'data' => 'value-a',
594609
'rules' => function () {
@@ -2000,6 +2015,16 @@ public static function ruleDataProvider(): array
20002015
'rules' => fn() => RuleSet::create()->numeric(),
20012016
'fails' => true,
20022017
],
2018+
'numericStrict valid' => [
2019+
'data' => 1.25,
2020+
'rules' => fn() => RuleSet::create()->numericStrict(),
2021+
'fails' => false,
2022+
],
2023+
'numericStrict invalid' => [
2024+
'data' => '1.25',
2025+
'rules' => fn() => RuleSet::create()->numericStrict(),
2026+
'fails' => true,
2027+
],
20032028
'password using default valid' => [
20042029
'data' => 'pass1!',
20052030
'rules' => function () {

0 commit comments

Comments
 (0)