-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to use certain rules as class attributes
There are a few cases in which we want to validate the object as a whole, and that validation could be attached to the class as a PHP attribute. This commit enables that capability and changes a few rules to be class attributes.
- Loading branch information
1 parent
848724e
commit 042c8ee
Showing
16 changed files
with
62 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,14 +10,14 @@ | |
use Respect\Validation\Test\Stubs\WithAttributes; | ||
|
||
test('Default', expectAll( | ||
fn() => v::attributes()->assert(new WithAttributes('', '[email protected]', '2024-06-23')), | ||
fn() => v::attributes()->assert(new WithAttributes('', '2024-06-23', '[email protected]')), | ||
'`.name` must not be empty', | ||
'- `.name` must not be empty', | ||
['name' => '`.name` must not be empty'], | ||
)); | ||
|
||
test('Inverted', expectAll( | ||
fn() => v::attributes()->assert(new WithAttributes('John Doe', '[email protected]', '2024-06-23', '+1234567890')), | ||
fn() => v::attributes()->assert(new WithAttributes('John Doe', '2024-06-23', '[email protected]', '+1234567890')), | ||
'`.phone` must be a valid telephone number or must be null', | ||
'- `.phone` must be a valid telephone number or must be null', | ||
['phone' => '`.phone` must be a valid telephone number or must be null'], | ||
|
@@ -31,39 +31,56 @@ | |
)); | ||
|
||
test('Nullable', expectAll( | ||
fn() => v::attributes()->assert(new WithAttributes('John Doe', '[email protected]', '2024-06-23', 'not a phone number')), | ||
fn() => v::attributes()->assert(new WithAttributes('John Doe', '2024-06-23', '[email protected]', 'not a phone number')), | ||
'`.phone` must be a valid telephone number or must be null', | ||
'- `.phone` must be a valid telephone number or must be null', | ||
['phone' => '`.phone` must be a valid telephone number or must be null'], | ||
)); | ||
|
||
test('Multiple attributes, all failed', expectAll( | ||
fn() => v::attributes()->assert(new WithAttributes('', 'not an email', 'not a date', 'not a phone number')), | ||
fn() => v::attributes()->assert(new WithAttributes('', 'not a date', 'not an email', 'not a phone number')), | ||
'`.name` must not be empty', | ||
<<<'FULL_MESSAGE' | ||
- `Respect\Validation\Test\Stubs\WithAttributes { +$name="" +$email="not an email" +$birthdate="not a date" +$phone ... }` must pass all the rules | ||
- `Respect\Validation\Test\Stubs\WithAttributes { +$name="" +$birthdate="not a date" +$email="not an email" +$phone ... }` must pass the rules | ||
- `.name` must not be empty | ||
- `.email` must be a valid email address | ||
- `.birthdate` must pass all the rules | ||
- `.birthdate` must be a valid date in the format "2005-12-30" | ||
- For comparison with now, `.birthdate` must be a valid datetime | ||
- `.email` must be a valid email address or must be null | ||
- `.phone` must be a valid telephone number or must be null | ||
FULL_MESSAGE, | ||
[ | ||
'__root__' => '`Respect\Validation\Test\Stubs\WithAttributes { +$name="" +$email="not an email" +$birthdate="not a date" +$phone ... }` must pass all the rules', | ||
'__root__' => '`Respect\Validation\Test\Stubs\WithAttributes { +$name="" +$birthdate="not a date" +$email="not an email" +$phone ... }` must pass the rules', | ||
'name' => '`.name` must not be empty', | ||
'email' => '`.email` must be a valid email address', | ||
'birthdate' => [ | ||
'__root__' => '`.birthdate` must pass all the rules', | ||
'date' => '`.birthdate` must be a valid date in the format "2005-12-30"', | ||
'dateTimeDiffLessThanOrEqual' => 'For comparison with now, `.birthdate` must be a valid datetime', | ||
], | ||
'email' => '`.email` must be a valid email address or must be null', | ||
'phone' => '`.phone` must be a valid telephone number or must be null', | ||
], | ||
)); | ||
|
||
test('Failed attributes on the class', expectAll( | ||
fn() => v::attributes()->assert(new WithAttributes('John Doe', '2024-06-23')), | ||
'`.email` must be defined', | ||
<<<'FULL_MESSAGE' | ||
- `Respect\Validation\Test\Stubs\WithAttributes { +$name="John Doe" +$birthdate="2024-06-23" +$email=null +$phone=n ... }` must pass at least one of the rules | ||
- `.email` must be defined | ||
- `.phone` must be defined | ||
FULL_MESSAGE, | ||
[ | ||
'anyOf' => [ | ||
'__root__' => '`Respect\Validation\Test\Stubs\WithAttributes { +$name="John Doe" +$birthdate="2024-06-23" +$email=null +$phone=n ... }` must pass at least one of the rules', | ||
'email' => '`.email` must be defined', | ||
'phone' => '`.phone` must be defined', | ||
], | ||
], | ||
)); | ||
|
||
test('Multiple attributes, one failed', expectAll( | ||
fn() => v::attributes()->assert(new WithAttributes('John Doe', '[email protected]', '22 years ago')), | ||
fn() => v::attributes()->assert(new WithAttributes('John Doe', '22 years ago', '[email protected]')), | ||
'`.birthdate` must be a valid date in the format "2005-12-30"', | ||
'- `.birthdate` must be a valid date in the format "2005-12-30"', | ||
['birthdate' => '`.birthdate` must be a valid date in the format "2005-12-30"'], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,7 +140,7 @@ | |
'tags' => ['objectType', 'withoutAttributes'], | ||
], | ||
'object with Rule attributes' => [ | ||
'value' => [new WithAttributes('John Doe', '[email protected]', '1912-06-23')], | ||
'value' => [new WithAttributes('John Doe', '1912-06-23', '[email protected]')], | ||
'tags' => ['objectType', 'withAttributes'], | ||
], | ||
'anonymous class' => [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,25 +55,26 @@ public static function providerForObjectsWithValidPropertyValues(): array | |
'All' => [ | ||
new WithAttributes( | ||
'John Doe', | ||
'[email protected]', | ||
'2020-06-23', | ||
'[email protected]', | ||
'+31206241111', | ||
'Amstel 1 1011 PN AMSTERDAM Noord-Holland' | ||
), | ||
], | ||
'Only required' => [new WithAttributes('Jane Doe', '[email protected]', '2017-11-30')], | ||
'Only required' => [new WithAttributes('Jane Doe', '2017-11-30', '[email protected]')], | ||
]; | ||
} | ||
|
||
/** @return array<array{object}> */ | ||
public static function providerForObjectsWithInvalidPropertyValues(): array | ||
{ | ||
return [ | ||
[new WithAttributes('', 'not an email', 'not a date', 'not a phone number')], | ||
[new WithAttributes('', '[email protected]', '1912-06-23', '+1234567890')], | ||
[new WithAttributes('John Doe', 'not an email', '1912-06-23', '+1234567890')], | ||
[new WithAttributes('John Doe', '[email protected]', 'not a date', '+1234567890')], | ||
[new WithAttributes('John Doe', '[email protected]', '1912-06-23', 'not a phone number')], | ||
[new WithAttributes('Jane Doe', '2017-11-30')], | ||
[new WithAttributes('', 'not a date', 'not an email', 'not a phone number')], | ||
[new WithAttributes('', '1912-06-23', '[email protected]', '+1234567890')], | ||
[new WithAttributes('John Doe', '1912-06-23', 'not an email', '+1234567890')], | ||
[new WithAttributes('John Doe', 'not a date', '[email protected]', '+1234567890')], | ||
[new WithAttributes('John Doe', '1912-06-23', '[email protected]', 'not a phone number')], | ||
]; | ||
} | ||
} |