Skip to content

Commit

Permalink
update: Added present based on rule helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-perri committed Nov 22, 2023
1 parent 98e487f commit 89f44c1
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 1 deletion.
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.1",
"laravel/framework": "^10.14"
"laravel/framework": "^10.32"
},
"require-dev": {
"ext-json": "*",
Expand Down
36 changes: 36 additions & 0 deletions src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,42 @@ public static function present(): string
return 'present';
}

/**
* The field under validation must be present but can be empty if *anotherField* under validation is equal to a
* specified value.
*/
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*.
*/
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.
*/
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.
*/
public static function presentWithAll(string ...$field): string
{
return 'present_with_all:'.implode(',', $field);
}

/**
* The field under validation must be empty or not present.
*
Expand Down
36 changes: 36 additions & 0 deletions src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,42 @@ public function present(): self
return $this->rule(Rule::present());
}

/**
* The field under validation must be present but can be empty if *anotherField* under validation is equal to a
* specified value.
*/
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*.
*/
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.
*/
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.
*/
public function presentWithAll(string ...$field): self
{
return $this->rule(Rule::presentWithAll(...$field));
}

/**
* The field under validation must be empty or not present.
*
Expand Down
113 changes: 113 additions & 0 deletions tests/Unit/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,119 @@ public function ruleDataProvider(): array
],
'fails' => true,
],
'presentIf valid' => [
'data' => [
'field-b' => '',
],
'rules' => fn() => [
'field-a' => RuleSet::create()->presentIf('field-b', 'a'),
],
'fails' => false,
],
'presentIf valid with value' => [
'data' => [
'field-a' => '',
'field-b' => 'a',
],
'rules' => fn() => [
'field-a' => RuleSet::create()->presentIf('field-b', 'a'),
],
'fails' => false,
],
'presentIf invalid' => [
'data' => [
'field-b' => 'a',
],
'rules' => fn() => [
'field-a' => RuleSet::create()->presentIf('field-b', 'a'),
],
'fails' => true,
],
'presentUnless valid' => [
'data' => [
'field-a' => '',
],
'rules' => fn() => [
'field-a' => RuleSet::create()->presentUnless('field-b', 'a'),
],
'fails' => false,
],
'presentUnless valid with value' => [
'data' => [
'field-b' => 'a',
],
'rules' => fn() => [
'field-a' => RuleSet::create()->presentUnless('field-b', 'a'),
],
'fails' => false,
],
'presentUnless invalid' => [
'data' => [
'field-b' => 'b',
],
'rules' => fn() => [
'field-a' => RuleSet::create()->presentUnless('field-b', 'a'),
],
'fails' => true,
],
'presentWith valid' => [
'data' => [
'field-a' => '',
'field-b' => 'b',
],
'rules' => fn() => [
'field-a' => RuleSet::create()->presentWith('field-b'),
],
'fails' => false,
],
'presentWith valid without' => [
'data' => [
'field-c' => 'c',
],
'rules' => fn() => [
'field-a' => RuleSet::create()->presentWith('field-b'),
],
'fails' => false,
],
'presentWith invalid' => [
'data' => [
'field-b' => 'b',
],
'rules' => fn() => [
'field-a' => RuleSet::create()->presentWith('field-b'),
],
'fails' => true,
],
'presentWithAll valid' => [
'data' => [
'field-a' => '',
'field-b' => 'b',
'field-c' => 'c',
],
'rules' => fn() => [
'field-a' => RuleSet::create()->presentWithAll('field-b', 'field-c'),
],
'fails' => false,
],
'presentWithAll valid without' => [
'data' => [
'field-c' => 'c',
],
'rules' => fn() => [
'field-a' => RuleSet::create()->presentWithAll('field-b', 'field-c'),
],
'fails' => false,
],
'presentWithAll invalid' => [
'data' => [
'field-b' => 'b',
'field-c' => 'c',
],
'rules' => fn() => [
'field-a' => RuleSet::create()->presentWithAll('field-b', 'field-c'),
],
'fails' => true,
],
'prohibited valid' => [
'data' => [
'field-a' => '',
Expand Down

0 comments on commit 89f44c1

Please sign in to comment.