Skip to content

Commit

Permalink
In type fixes (#44)
Browse files Browse the repository at this point in the history
* docs: Added missing documentation links

* update: Updated in and not in types to match Laravel type changes

* update: Updated minimum version for type fix
  • Loading branch information
erik-perri authored Apr 23, 2024
1 parent d0c969b commit 22658a2
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 33 deletions.
2 changes: 1 addition & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Upgrading from v5.0 to v5.1

- Minimum Laravel version increased from `11.0.3` to `11.4`.
- Minimum Laravel version increased from `11.0.3` to `11.5`.

### Upgrading from v4.3 to v5.0

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"minimum-stability": "stable",
"require": {
"php": "^8.2",
"laravel/framework": "^11.4"
"laravel/framework": "^11.5"
},
"require-dev": {
"ext-json": "*",
Expand Down
39 changes: 24 additions & 15 deletions src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sourcetoad\RuleHelper;

use BackedEnum;
use Brick\Math\BigNumber;
use DateTimeInterface;
use Illuminate\Contracts\Support\Arrayable;
Expand All @@ -25,6 +26,7 @@
use Illuminate\Validation\Rules\RequiredIf;
use Illuminate\Validation\Rules\Unique;
use Stringable;
use UnitEnum;

class Rule
{
Expand Down Expand Up @@ -561,9 +563,9 @@ public static function image(): string
* list of values provided to the *in* rule.
*
* @link https://laravel.com/docs/11.x/validation#rule-in
* @param Arrayable<array-key, mixed>|array<array-key, mixed>|string $values
* @param Arrayable<array-key, BackedEnum|UnitEnum|string>|array<BackedEnum|UnitEnum|string>|BackedEnum|UnitEnum|string $values
*/
public static function in(Arrayable|array|string $values): In
public static function in(Arrayable|BackedEnum|UnitEnum|array|string $values): In
{
return LaravelRule::in($values);
}
Expand Down Expand Up @@ -632,7 +634,10 @@ public static function json(): string
}

/**
* The field under validation must be a list style array.
* The field under validation must be an array that is a list. An array is considered a list if its keys consist of
* consecutive numbers from 0 to count($array) - 1.
*
* @link https://laravel.com/docs/11.x/validation#rule-list
*/
public static function list(): string
{
Expand Down Expand Up @@ -804,9 +809,9 @@ public static function multipleOf(int|float $value): string
* The field under validation must not be included in the given list of values.
*
* @link https://laravel.com/docs/11.x/validation#rule-not-in
* @param Arrayable<array-key, mixed>|array<array-key, mixed>|string $values
* @param Arrayable<array-key, BackedEnum|UnitEnum|string>|array<BackedEnum|UnitEnum|string>|BackedEnum|UnitEnum|string $values
*/
public static function notIn(Arrayable|array|string $values): NotIn
public static function notIn(Arrayable|BackedEnum|UnitEnum|array|string $values): NotIn
{
return LaravelRule::notIn($values);
}
Expand Down Expand Up @@ -866,35 +871,39 @@ public static function present(): string
}

/**
* The field under validation must be present but can be empty if *anotherField* under validation is equal to a
* specified value.
* The field under validation must be present if the *anotherField* field is equal to any *value*.
*
* @link https://laravel.com/docs/11.x/validation#rule-present-if
*/
public static function presentIf(string $anotherField, string ...$value): string
{
return sprintf('present_if:%s,%s', $anotherField, implode(',', $value));
}

/**
* The field under validation must be present but can be empty unless the *anotherField* field is equal to any
* *value*.
* The field under validation must be present unless the *anotherField* field is equal to any *value*.
*
* @link https://laravel.com/docs/11.x/validation#rule-present-unless
*/
public static function presentUnless(string $anotherField, string ...$value): string
{
return sprintf('present_unless:%s,%s', $anotherField, implode(',', $value));
}

/**
* The field under validation must be present but can be empty *only if* any of the other specified fields are
* present and not empty.
* The field under validation must be present *only if* any of the other specified fields are present.
*
* @link https://laravel.com/docs/11.x/validation#rule-present-with
*/
public static function presentWith(string ...$field): string
{
return 'present_with:'.implode(',', $field);
}

/**
* The field under validation must be present but can be empty *only if* all the other specified fields are present
* and not empty.
* The field under validation must be present *only if* all the other specified fields are present.
*
* @link https://laravel.com/docs/11.x/validation#rule-present-with-all
*/
public static function presentWithAll(string ...$field): string
{
Expand Down Expand Up @@ -996,7 +1005,7 @@ public static function requiredIf(mixed $callback): RequiredIf
}

/**
* The field under validation must be present and not empty if the `field` field is equal to yes, on, 1, "1", true,
* The field under validation must be present and not empty if the *field* field is equal to yes, on, 1, "1", true,
* or "true".
*
* @link https://laravel.com/docs/11.x/validation#rule-required-if-accepted
Expand Down Expand Up @@ -1027,7 +1036,7 @@ public static function requiredIfAny(RequiredIf ...$rules): RequiredIf
}

/**
* The field under validation must be present and not empty if the `field` field is equal to "no", "off", 0, "0",
* The field under validation must be present and not empty if the *field* field is equal to "no", "off", 0, "0",
* false, or "false".
*
* @link https://laravel.com/docs/11.x/validation#rule-required-if-declined
Expand Down
40 changes: 24 additions & 16 deletions src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
namespace Sourcetoad\RuleHelper;

use ArrayIterator;
use BackedEnum;
use Brick\Math\BigNumber;
use DateTimeInterface;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Validation\InvokableRule;
use Illuminate\Contracts\Validation\Rule as RuleContract;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Validation\ConditionalRules;
use Illuminate\Validation\Rules\Dimensions;
use Illuminate\Validation\Rules\Password;
use Illuminate\Validation\Rules\RequiredIf;
use IteratorAggregate;
use Stringable;
use UnitEnum;

/**
* @implements Arrayable<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>
Expand Down Expand Up @@ -645,9 +646,9 @@ public function image(): self
* list of values provided to the *in* rule.
*
* @link https://laravel.com/docs/11.x/validation#rule-in
* @param Arrayable<array-key, mixed>|array<array-key, mixed>|string $values
* @param Arrayable<array-key, BackedEnum|UnitEnum|string>|array<BackedEnum|UnitEnum|string>|BackedEnum|UnitEnum|string $values
*/
public function in(Arrayable|array|string $values): self
public function in(Arrayable|BackedEnum|UnitEnum|array|string $values): self
{
return $this->rule(Rule::in($values));
}
Expand Down Expand Up @@ -716,7 +717,10 @@ public function json(): self
}

/**
* The field under validation must be a list style array.
* The field under validation must be an array that is a list. An array is considered a list if its keys consist of
* consecutive numbers from 0 to count($array) - 1.
*
* @link https://laravel.com/docs/11.x/validation#rule-list
*/
public function list(): self
{
Expand Down Expand Up @@ -888,9 +892,9 @@ public function multipleOf(int|float $value): self
* The field under validation must not be included in the given list of values.
*
* @link https://laravel.com/docs/11.x/validation#rule-not-in
* @param Arrayable<array-key, mixed>|array<array-key, mixed>|string $values
* @param Arrayable<array-key, BackedEnum|UnitEnum|string>|array<BackedEnum|UnitEnum|string>|BackedEnum|UnitEnum|string $values
*/
public function notIn(Arrayable|array|string $values): self
public function notIn(Arrayable|BackedEnum|UnitEnum|array|string $values): self
{
return $this->rule(Rule::notIn($values));
}
Expand Down Expand Up @@ -958,35 +962,39 @@ public function present(): self
}

/**
* The field under validation must be present but can be empty if *anotherField* under validation is equal to a
* specified value.
* The field under validation must be present if the *anotherField* field is equal to any *value*.
*
* @link https://laravel.com/docs/11.x/validation#rule-present-if
*/
public function presentIf(string $anotherField, string ...$value): self
{
return $this->rule(Rule::presentIf($anotherField, ...$value));
}

/**
* The field under validation must be present but can be empty unless the *anotherField* field is equal to any
* *value*.
* The field under validation must be present unless the *anotherField* field is equal to any *value*.
*
* @link https://laravel.com/docs/11.x/validation#rule-present-unless
*/
public function presentUnless(string $anotherField, string ...$value): self
{
return $this->rule(Rule::presentUnless($anotherField, ...$value));
}

/**
* The field under validation must be present but can be empty *only if* any of the other specified fields are
* present and not empty.
* The field under validation must be present *only if* any of the other specified fields are present.
*
* @link https://laravel.com/docs/11.x/validation#rule-present-with
*/
public function presentWith(string ...$field): self
{
return $this->rule(Rule::presentWith(...$field));
}

/**
* The field under validation must be present but can be empty *only if* all the other specified fields are present
* and not empty.
* The field under validation must be present *only if* all the other specified fields are present.
*
* @link https://laravel.com/docs/11.x/validation#rule-present-with-all
*/
public function presentWithAll(string ...$field): self
{
Expand Down Expand Up @@ -1088,7 +1096,7 @@ public function requiredIf(mixed $callback): self
}

/**
* The field under validation must be present and not empty if the `field` field is equal to yes, on, 1, "1", true,
* The field under validation must be present and not empty if the *field* field is equal to yes, on, 1, "1", true,
* or "true".
*
* @link https://laravel.com/docs/11.x/validation#rule-required-if-accepted
Expand All @@ -1115,7 +1123,7 @@ public function requiredIfAny(RequiredIf ...$rules): self
}

/**
* The field under validation must be present and not empty if the `field` field is equal to "no", "off", 0, "0",
* The field under validation must be present and not empty if the *field* field is equal to "no", "off", 0, "0",
* false, or "false".
*
* @link https://laravel.com/docs/11.x/validation#rule-required-if-declined
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,24 @@ public static function ruleDataProvider(): array
],
'fails' => true,
],
'in enum valid' => [
'data' => [
'field-a' => ExampleStringEnum::Valid->value,
],
'rules' => fn() => [
'field-a' => RuleSet::create()->in(ExampleStringEnum::Valid),
],
'fails' => false,
],
'in enum invalid' => [
'data' => [
'field-a' => ExampleStringEnum::Another->value,
],
'rules' => fn() => [
'field-a' => RuleSet::create()->in(ExampleStringEnum::Valid),
],
'fails' => true,
],
'inArray valid' => [
'data' => [
'field-a' => 'a',
Expand Down Expand Up @@ -1664,6 +1682,24 @@ public static function ruleDataProvider(): array
],
'fails' => true,
],
'notIn enum valid' => [
'data' => [
'field-a' => ExampleStringEnum::Another->value,
],
'rules' => fn() => [
'field-a' => RuleSet::create()->notIn(ExampleStringEnum::Valid),
],
'fails' => false,
],
'notIn enum invalid' => [
'data' => [
'field-a' => ExampleStringEnum::Valid->value,
],
'rules' => fn() => [
'field-a' => RuleSet::create()->notIn(ExampleStringEnum::Valid),
],
'fails' => true,
],
'notRegex valid' => [
'data' => 'value-1',
'rules' => fn() => RuleSet::create()->notRegex('/[a-z]+$/'),
Expand Down

0 comments on commit 22658a2

Please sign in to comment.