Skip to content

Commit

Permalink
Rename "negative" to "inverted" mode
Browse files Browse the repository at this point in the history
The original name was heavily influenced by the fact that we get those
messages when using the "Not" rule; however, that rule inverts the
validation despite the current validation mode. It can be confusing at
times with certain rules, so naming it as "inverted" makes more sense
than "negative".
  • Loading branch information
henriquemoody committed Dec 2, 2024
1 parent 2aa5e39 commit b894593
Show file tree
Hide file tree
Showing 23 changed files with 128 additions and 128 deletions.
4 changes: 2 additions & 2 deletions library/Message/StandardRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ private function getTemplateMessage(Result $result): string
continue;
}

if ($result->mode == Mode::NEGATIVE) {
return $template->negative;
if ($result->mode == Mode::INVERTED) {
return $template->inverted;
}

return $template->default;
Expand Down
2 changes: 1 addition & 1 deletion library/Message/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Template
{
public function __construct(
public readonly string $default,
public readonly string $negative,
public readonly string $inverted,
public readonly string $id = Validatable::TEMPLATE_STANDARD,
) {
}
Expand Down
2 changes: 1 addition & 1 deletion library/Mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
enum Mode
{
case DEFAULT;
case NEGATIVE;
case INVERTED;
}
2 changes: 1 addition & 1 deletion library/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function withInvertedMode(): self
{
return $this->clone(
isValid: !$this->isValid,
mode: $this->mode == Mode::DEFAULT ? Mode::NEGATIVE : Mode::DEFAULT,
mode: $this->mode == Mode::DEFAULT ? Mode::INVERTED : Mode::DEFAULT,
nextSibling: $this->nextSibling?->withInvertedMode(),
children: array_map(static fn (Result $child) => $child->withInvertedMode(), $this->children),
);
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/rules/betweenExclusive.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'vendor/autoload.php';

run([
'Default' => [v::betweenExclusive(1, 10), 12],
'Negative' => [v::not(v::betweenExclusive(1, 10)), 5],
'Inverted' => [v::not(v::betweenExclusive(1, 10)), 5],
'With template' => [v::betweenExclusive(1, 10), 12, 'Bewildered bees buzzed between blooming begonias'],
'With name' => [v::betweenExclusive(1, 10)->setName('Range'), 10],
]);
Expand All @@ -19,7 +19,7 @@ Default
'betweenExclusive' => '12 must be greater than 1 and less than 10',
]

Negative
Inverted
⎺⎺⎺⎺⎺⎺⎺⎺
5 must not be greater than 1 and less than 10
- 5 must not be greater than 1 and less than 10
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/rules/consecutive.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'vendor/autoload.php';

run([
'Default' => [v::consecutive(v::alwaysValid(), v::trueVal()), false],
'Negative' => [v::not(v::consecutive(v::alwaysValid(), v::trueVal())), true],
'Inverted' => [v::not(v::consecutive(v::alwaysValid(), v::trueVal())), true],
'Default with inverted failing rule' => [v::consecutive(v::alwaysValid(), v::not(v::trueVal())), true],
'With wrapped name, default' => [
v::consecutive(v::alwaysValid(), v::trueVal()->setName('Wrapped'))->setName('Wrapper'),
Expand Down Expand Up @@ -58,7 +58,7 @@ Default
'trueVal' => '`false` must evaluate to `true`',
]

Negative
Inverted
⎺⎺⎺⎺⎺⎺⎺⎺
`true` must not evaluate to `true`
- `true` must not evaluate to `true`
Expand Down
28 changes: 14 additions & 14 deletions tests/integration/rules/each.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@ require 'vendor/autoload.php';

$empty = [];
$nonIterable = null;
$negative = [1, 2, 3];
$inverted = [1, 2, 3];
$default = ['a', 'b', 'c'];

run([
// Simple
'Non-iterable' => [v::each(v::intType()), $nonIterable],
'Empty' => [v::each(v::intType()), $empty],
'Default' => [v::each(v::intType()), $default],
'Negative' => [v::not(v::each(v::intType())), $negative],
'Inverted' => [v::not(v::each(v::intType())), $inverted],

// With name
'With name, non-iterable' => [v::each(v::intType()->setName('Wrapped'))->setName('Wrapper'), $nonIterable],
'With name, empty' => [v::each(v::intType()->setName('Wrapped'))->setName('Wrapper'), $empty],
'With name, default' => [v::each(v::intType()->setName('Wrapped'))->setName('Wrapper'), $default],
'With name, negative' => [
'With name, inverted' => [
v::not(v::each(v::intType()->setName('Wrapped'))->setName('Wrapper'))->setName('Not'),
$negative,
$inverted,
],
'With wrapper name, default' => [v::each(v::intType())->setName('Wrapper'), $default],
'With wrapper name, negative' => [
'With wrapper name, inverted' => [
v::not(v::each(v::intType())->setName('Wrapper'))->setName('Not'),
$negative,
$inverted,
],
'With Not name, negative' => [
'With Not name, inverted' => [
v::not(v::each(v::intType()))->setName('Not'),
$negative,
$inverted,
],

// With template
'With template, non-iterable' => [v::each(v::intType()), $nonIterable, 'You should have passed an iterable'],
'With template, empty' => [v::each(v::intType()), $empty, 'You should have passed an non-empty'],
'With template, default' => [v::each(v::intType()), $default, 'All items should have been integers'],
'with template, negative' => [v::not(v::each(v::intType())), $negative, 'All items should not have been integers'],
'with template, inverted' => [v::not(v::each(v::intType())), $inverted, 'All items should not have been integers'],

// With array template
'With array template, default' => [
Expand Down Expand Up @@ -95,7 +95,7 @@ Default
'intType.3' => '"c" must be of type integer',
]

Negative
Inverted
⎺⎺⎺⎺⎺⎺⎺⎺
1 must not be of type integer
- Each item in `[1, 2, 3]` must not validate
Expand Down Expand Up @@ -139,7 +139,7 @@ Wrapped must be of type integer
'intType.3' => 'Wrapped must be of type integer',
]

With name, negative
With name, inverted
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
Wrapped must not be of type integer
- Each item in Wrapped must not validate
Expand Down Expand Up @@ -167,7 +167,7 @@ Wrapper must be of type integer
'intType.3' => 'Wrapper must be of type integer',
]

With wrapper name, negative
With wrapper name, inverted
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
Wrapper must not be of type integer
- Each item in Wrapper must not validate
Expand All @@ -181,7 +181,7 @@ Wrapper must not be of type integer
'intType.3' => 'Wrapper must not be of type integer',
]

With Not name, negative
With Not name, inverted
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
Not must not be of type integer
- Each item in Not must not validate
Expand Down Expand Up @@ -219,7 +219,7 @@ All items should have been integers
'each' => 'All items should have been integers',
]

with template, negative
with template, inverted
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
All items should not have been integers
- All items should not have been integers
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/rules/hetu.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'vendor/autoload.php';

run([
'Default' => [v::hetu(), '010106A901O'],
'Negative' => [v::not(v::hetu()), '010106A9012'],
'Inverted' => [v::not(v::hetu()), '010106A9012'],
'With template' => [v::hetu(), '010106A901O', 'That is not a HETU'],
'With name' => [v::hetu()->setName('Hetu'), '010106A901O'],
]);
Expand All @@ -19,7 +19,7 @@ Default
'hetu' => '"010106A901O" must be a valid Finnish personal identity code',
]

Negative
Inverted
⎺⎺⎺⎺⎺⎺⎺⎺
"010106A9012" must not be a valid Finnish personal identity code
- "010106A9012" must not be a valid Finnish personal identity code
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/rules/iterableType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'vendor/autoload.php';

run([
'Default' => [v::iterableType(), null],
'Negative' => [v::not(v::iterableType()), [1, 2, 3]],
'Inverted' => [v::not(v::iterableType()), [1, 2, 3]],
'With template' => [v::iterableType(), null, 'Not an iterable at all'],
'With name' => [v::iterableType()->setName('Options'), null],
]);
Expand All @@ -19,7 +19,7 @@ Default
'iterableType' => '`null` must be of type iterable',
]

Negative
Inverted
⎺⎺⎺⎺⎺⎺⎺⎺
`[1, 2, 3]` must not of type iterable
- `[1, 2, 3]` must not of type iterable
Expand Down
24 changes: 12 additions & 12 deletions tests/integration/rules/key.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ run([
// Simple
'Missing key' => [v::key('foo', v::intType()), []],
'Default' => [v::key('foo', v::intType()), ['foo' => 'string']],
'Negative' => [v::not(v::key('foo', v::intType())), ['foo' => 12]],
'Double-negative with missing key' => [
'Inverted' => [v::not(v::key('foo', v::intType())), ['foo' => 12]],
'Double-inverted with missing key' => [
v::not(v::not(v::key('foo', v::intType()))),
[],
],
Expand All @@ -22,7 +22,7 @@ run([
v::key('foo', v::intType()->setName('Wrapped'))->setName('Wrapper'),
['foo' => 'string'],
],
'With wrapped name, negative' => [
'With wrapped name, inverted' => [
v::not(v::key('foo', v::intType()->setName('Wrapped'))->setName('Wrapper'))->setName('Not'),
['foo' => 12],
],
Expand All @@ -34,18 +34,18 @@ run([
v::key('foo', v::intType())->setName('Wrapper'),
[],
],
'With wrapper name, negative' => [
'With wrapper name, inverted' => [
v::not(v::key('foo', v::intType())->setName('Wrapper'))->setName('Not'),
['foo' => 12],
],
'With "Not" name, negative' => [
'With "Not" name, inverted' => [
v::not(v::key('foo', v::intType()))->setName('Not'),
['foo' => 12],
],

// With custom template
'With template, default' => [v::key('foo', v::intType()), ['foo' => 'string'], 'That key is off-key'],
'With template, negative' => [v::not(v::key('foo', v::intType())), ['foo' => 12], 'No off-key key'],
'With template, inverted' => [v::not(v::key('foo', v::intType())), ['foo' => 12], 'No off-key key'],
]);
?>
--EXPECT--
Expand All @@ -65,15 +65,15 @@ foo must be of type integer
'foo' => 'foo must be of type integer',
]

Negative
Inverted
⎺⎺⎺⎺⎺⎺⎺⎺
foo must not be of type integer
- foo must not be of type integer
[
'foo' => 'foo must not be of type integer',
]

Double-negative with missing key
Double-inverted with missing key
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
foo must be present
- foo must be present
Expand All @@ -97,7 +97,7 @@ Wrapped must be of type integer
'foo' => 'Wrapped must be of type integer',
]

With wrapped name, negative
With wrapped name, inverted
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
Wrapped must not be of type integer
- Wrapped must not be of type integer
Expand All @@ -121,15 +121,15 @@ foo must be present
'foo' => 'foo must be present',
]

With wrapper name, negative
With wrapper name, inverted
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
foo must not be of type integer
- foo must not be of type integer
[
'foo' => 'foo must not be of type integer',
]

With "Not" name, negative
With "Not" name, inverted
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
foo must not be of type integer
- foo must not be of type integer
Expand All @@ -145,7 +145,7 @@ That key is off-key
'foo' => 'That key is off-key',
]

With template, negative
With template, inverted
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
No off-key key
- No off-key key
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/rules/keyExists.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'vendor/autoload.php';

run([
'Default mode' => [v::keyExists('foo'), ['bar' => 'baz']],
'Negative mode' => [v::not(v::keyExists('foo')), ['foo' => 'baz']],
'Inverted mode' => [v::not(v::keyExists('foo')), ['foo' => 'baz']],
'Custom name' => [v::keyExists('foo')->setName('Custom name'), ['bar' => 'baz']],
'Custom template' => [v::keyExists('foo'), ['bar' => 'baz'], 'Custom template for `{{name}}`'],
]);
Expand All @@ -19,7 +19,7 @@ foo must be present
'foo' => 'foo must be present',
]

Negative mode
Inverted mode
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
foo must not be present
- foo must not be present
Expand Down
Loading

0 comments on commit b894593

Please sign in to comment.