Skip to content

Commit

Permalink
Fix numeric comparison validation rules to work with numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Jun 13, 2024
1 parent ee513f6 commit d654740
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"require-dev" : {
"fakerphp/faker": "^1.14",
"friendsofphp/php-cs-fixer": "^3.0",
"inertiajs/inertia-laravel": "dev-master#4508fd1",
"inertiajs/inertia-laravel": "^1.2",
"livewire/livewire": "^3.0",
"mockery/mockery": "^1.6",
"nesbot/carbon": "^2.63",
Expand Down
7 changes: 3 additions & 4 deletions src/Attributes/Validation/GreaterThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class GreaterThan extends StringValidationAttribute
{
protected FieldReference $field;
protected FieldReference|int|float $field;

public function __construct(
string|FieldReference $field,
int|float|string|FieldReference $field,
) {
$this->field = $this->parseFieldReference($field);
$this->field = is_numeric($field) ? $field : $this->parseFieldReference($field);
}


public static function keyword(): string
{
return 'gt';
Expand Down
6 changes: 3 additions & 3 deletions src/Attributes/Validation/GreaterThanOrEqualTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class GreaterThanOrEqualTo extends StringValidationAttribute
{
protected FieldReference $field;
protected int|float|FieldReference $field;

public function __construct(
string|FieldReference $field,
int|float|string|FieldReference $field,
) {
$this->field = $this->parseFieldReference($field);
$this->field = is_numeric($field) ? $field : $this->parseFieldReference($field);
}


Expand Down
6 changes: 3 additions & 3 deletions src/Attributes/Validation/LessThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class LessThan extends StringValidationAttribute
{
protected FieldReference $field;
protected int|float|FieldReference $field;

public function __construct(
string|FieldReference $field,
int|float|string|FieldReference $field,
) {
$this->field = $this->parseFieldReference($field);
$this->field = is_numeric($field) ? $field : $this->parseFieldReference($field);
}


Expand Down
6 changes: 3 additions & 3 deletions src/Attributes/Validation/LessThanOrEqualTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class LessThanOrEqualTo extends StringValidationAttribute
{
protected FieldReference $field;
protected int|float|FieldReference $field;

public function __construct(
string|FieldReference $field,
int|float|string|FieldReference $field,
) {
$this->field = $this->parseFieldReference($field);
$this->field = is_numeric($field) ? $field : $this->parseFieldReference($field);
}


Expand Down
20 changes: 20 additions & 0 deletions tests/Datasets/RulesDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,21 @@ function fixature(
expected: 'gt:field',
);

yield fixature(
attribute: new GreaterThan(10),
expected: 'gt:10',
);

yield fixature(
attribute: new GreaterThanOrEqualTo('field'),
expected: 'gte:field',
);

yield fixature(
attribute: new GreaterThanOrEqualTo('10'),
expected: 'gte:10',
);

yield fixature(
attribute: new Image(),
expected: 'image',
Expand Down Expand Up @@ -306,11 +316,21 @@ function fixature(
expected: 'lt:field',
);

yield fixature(
attribute: new LessThan(10),
expected: 'lt:10',
);

yield fixature(
attribute: new LessThanOrEqualTo('field'),
expected: 'lte:field',
);

yield fixature(
attribute: new LessThanOrEqualTo(10),
expected: 'lte:10',
);

yield fixature(
attribute: new ListType(),
expected: 'list',
Expand Down

0 comments on commit d654740

Please sign in to comment.