Skip to content

Commit 4389e4d

Browse files
committed
Rename "Attribute" rule to "Property"
Because now we have the concept of attributes in PHP, the rule with the name "Attribute" makes no sense because it doesn't validate attributes but properties. In the future, it might be possible that Validation will have a rule called "Attribute" to validate PHP attributes. Signed-off-by: Henrique Moody <[email protected]>
1 parent 8f545c1 commit 4389e4d

16 files changed

+112
-111
lines changed

docs/feature-guide.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ $usernameValidator = v::alnum()->noWhitespace()->length(1, 15);
2828
$usernameValidator->validate('alganet'); // true
2929
```
3030

31-
## Validating object attributes
31+
## Validating object properties
3232

3333
Given this simple object:
3434

@@ -38,11 +38,11 @@ $user->name = 'Alexandre';
3838
$user->birthdate = '1987-07-01';
3939
```
4040

41-
Is possible to validate its attributes in a single chain:
41+
Is possible to validate its properties in a single chain:
4242

4343
```php
44-
$userValidator = v::attribute('name', v::stringType()->length(1, 32))
45-
->attribute('birthdate', v::date()->minAge(18));
44+
$userValidator = v::property('name', v::stringType()->length(1, 32))
45+
->property('birthdate', v::date()->minAge(18));
4646

4747
$userValidator->validate($user); // true
4848
```
@@ -180,7 +180,7 @@ try {
180180

181181
The `getMessages()` returns an array in which the keys are the name of the
182182
validators, or its reference in case you are using [Key](rules/Key.md) or
183-
[Attribute](rules/Attribute.md) rule:
183+
[Property](rules/Property.md) rule:
184184

185185
```no-highlight
186186
Array
@@ -230,7 +230,7 @@ in the chain fails.
230230

231231
## Validator name
232232

233-
On `v::attribute()` and `v::key()`, `{{name}}` is the attribute/key name. For others,
233+
On `v::property()` and `v::key()`, `{{name}}` is the property/key name. For others,
234234
is the same as the input. You can customize a validator name using:
235235

236236
```php

docs/list-of-rules.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@
157157

158158
- [AllOf](rules/AllOf.md)
159159
- [AnyOf](rules/AnyOf.md)
160-
- [Attribute](rules/Attribute.md)
161160
- [Call](rules/Call.md)
162161
- [Each](rules/Each.md)
163162
- [Key](rules/Key.md)
@@ -169,6 +168,7 @@
169168
- [Nullable](rules/Nullable.md)
170169
- [OneOf](rules/OneOf.md)
171170
- [Optional](rules/Optional.md)
171+
- [Property](rules/Property.md)
172172
- [When](rules/When.md)
173173

174174
## Numbers
@@ -197,9 +197,9 @@
197197

198198
## Objects
199199

200-
- [Attribute](rules/Attribute.md)
201200
- [Instance](rules/Instance.md)
202201
- [ObjectType](rules/ObjectType.md)
202+
- [Property](rules/Property.md)
203203

204204
## Strings
205205

@@ -240,10 +240,10 @@
240240

241241
## Structures
242242

243-
- [Attribute](rules/Attribute.md)
244243
- [Key](rules/Key.md)
245244
- [KeyNested](rules/KeyNested.md)
246245
- [KeySet](rules/KeySet.md)
246+
- [Property](rules/Property.md)
247247

248248
## Types
249249

@@ -277,7 +277,6 @@
277277
- [AnyOf](rules/AnyOf.md)
278278
- [ArrayType](rules/ArrayType.md)
279279
- [ArrayVal](rules/ArrayVal.md)
280-
- [Attribute](rules/Attribute.md)
281280
- [Base](rules/Base.md)
282281
- [Base64](rules/Base64.md)
283282
- [Between](rules/Between.md)
@@ -386,6 +385,7 @@
386385
- [PostalCode](rules/PostalCode.md)
387386
- [PrimeNumber](rules/PrimeNumber.md)
388387
- [Printable](rules/Printable.md)
388+
- [Property](rules/Property.md)
389389
- [Punct](rules/Punct.md)
390390
- [Readable](rules/Readable.md)
391391
- [Regex](rules/Regex.md)

docs/rules/Attribute.md

-47
This file was deleted.

docs/rules/Key.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ Version | Description
4444
See also:
4545

4646
- [ArrayVal](ArrayVal.md)
47-
- [Attribute](Attribute.md)
4847
- [Each](Each.md)
4948
- [KeyNested](KeyNested.md)
5049
- [KeySet](KeySet.md)
5150
- [KeyValue](KeyValue.md)
51+
- [Property](Property.md)

docs/rules/KeyNested.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ Version | Description
4545
***
4646
See also:
4747

48-
- [Attribute](Attribute.md)
4948
- [Key](Key.md)
5049
- [KeyValue](KeyValue.md)
50+
- [Property](Property.md)
5151

5252
[Yii2 ArrayHelper]: https://github.com/yiisoft/yii2/blob/68c30c1/framework/helpers/BaseArrayHelper.php "Yii2 ArrayHelper"

docs/rules/ObjectType.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ Version | Description
2424
See also:
2525

2626
- [ArrayType](ArrayType.md)
27-
- [Attribute](Attribute.md)
2827
- [BoolType](BoolType.md)
2928
- [BoolVal](BoolVal.md)
3029
- [CallableType](CallableType.md)
@@ -33,6 +32,7 @@ See also:
3332
- [IntType](IntType.md)
3433
- [NullType](NullType.md)
3534
- [Number](Number.md)
35+
- [Property](Property.md)
3636
- [ResourceType](ResourceType.md)
3737
- [StringType](StringType.md)
3838
- [StringVal](StringVal.md)

docs/rules/Property.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Property
2+
3+
- `Property(string $name)`
4+
- `Property(string $name, Validatable $rule)`
5+
- `Property(string $name, Validatable $rule, bool $mandatory)`
6+
7+
Validates an object property, even private ones.
8+
9+
```php
10+
$obj = new stdClass;
11+
$obj->foo = 'bar';
12+
13+
v::property('foo')->validate($obj); // true
14+
```
15+
16+
You can also validate the property itself:
17+
18+
```php
19+
v::property('foo', v::equals('bar'))->validate($obj); // true
20+
```
21+
22+
Third parameter makes the property presence optional:
23+
24+
```php
25+
v::property('lorem', v::stringType(), false)->validate($obj); // true
26+
```
27+
28+
The name of this validator is automatically set to the property name.
29+
30+
## Categorization
31+
32+
- Nesting
33+
- Objects
34+
- Structures
35+
36+
## Changelog
37+
38+
Version | Description
39+
--------|-------------
40+
3.0.0 | Renamed from `Attribute` to `Property`
41+
0.3.9 | Created
42+
43+
***
44+
See also:
45+
46+
- [Key](Key.md)
47+
- [KeyNested](KeyNested.md)
48+
- [ObjectType](ObjectType.md)

library/ChainedValidator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function arrayType(): ChainedValidator;
3030

3131
public function arrayVal(): ChainedValidator;
3232

33-
public function attribute(
33+
public function property(
3434
string $reference,
3535
?Validatable $validator = null,
3636
bool $mandatory = true

library/Exceptions/AttributeException.php library/Exceptions/PropertyException.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
namespace Respect\Validation\Exceptions;
1111

1212
/**
13-
* Exceptions to be thrown by the Attribute Rule.
13+
* Exceptions to be thrown by the Property Rule.
1414
*
1515
* @author Alexandre Gomes Gaigalas <[email protected]>
1616
* @author Emmerson Siqueira <[email protected]>
1717
* @author Henrique Moody <[email protected]>
1818
*/
19-
final class AttributeException extends NestedValidationException implements NonOmissibleException
19+
final class PropertyException extends NestedValidationException implements NonOmissibleException
2020
{
2121
public const NOT_PRESENT = 'not_present';
2222
public const INVALID = 'invalid';
@@ -26,12 +26,12 @@ final class AttributeException extends NestedValidationException implements NonO
2626
*/
2727
protected $defaultTemplates = [
2828
self::MODE_DEFAULT => [
29-
self::NOT_PRESENT => 'Attribute {{name}} must be present',
30-
self::INVALID => 'Attribute {{name}} must be valid',
29+
self::NOT_PRESENT => 'Property {{name}} must be present',
30+
self::INVALID => 'Property {{name}} must be valid',
3131
],
3232
self::MODE_NEGATIVE => [
33-
self::NOT_PRESENT => 'Attribute {{name}} must not be present',
34-
self::INVALID => 'Attribute {{name}} must not validate',
33+
self::NOT_PRESENT => 'Property {{name}} must not be present',
34+
self::INVALID => 'Property {{name}} must not validate',
3535
],
3636
];
3737

library/Rules/Attribute.php library/Rules/Property.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
use function property_exists;
1818

1919
/**
20-
* Validates an object attribute, event private ones.
20+
* Validates an object property, event private ones.
2121
*
2222
* @author Alexandre Gomes Gaigalas <[email protected]>
2323
* @author Emmerson Siqueira <[email protected]>
2424
* @author Henrique Moody <[email protected]>
2525
*/
26-
final class Attribute extends AbstractRelated
26+
final class Property extends AbstractRelated
2727
{
28-
public function __construct(string $reference, ?Validatable $rule = null, bool $mandatory = true)
28+
public function __construct(string $name, ?Validatable $rule = null, bool $mandatory = true)
2929
{
30-
parent::__construct($reference, $rule, $mandatory);
30+
parent::__construct($name, $rule, $mandatory);
3131
}
3232

3333
/**

library/StaticValidator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static function arrayType(): ChainedValidator;
3030

3131
public static function arrayVal(): ChainedValidator;
3232

33-
public static function attribute(
33+
public static function property(
3434
string $reference,
3535
?Validatable $validator = null,
3636
bool $mandatory = true

tests/integration/assert-with-attributes.phpt

+10-10
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@ try {
2828
];
2929
$object = json_decode((string) json_encode($array));
3030
v::create()
31-
->attribute(
31+
->property(
3232
'mysql',
3333
v::create()
34-
->attribute('host', v::stringType(), true)
35-
->attribute('user', v::stringType(), true)
36-
->attribute('password', v::stringType(), true)
37-
->attribute('schema', v::stringType(), true),
34+
->property('host', v::stringType(), true)
35+
->property('user', v::stringType(), true)
36+
->property('password', v::stringType(), true)
37+
->property('schema', v::stringType(), true),
3838
true
3939
)
40-
->attribute(
40+
->property(
4141
'postgresql',
4242
v::create()
43-
->attribute('host', v::stringType(), true)
44-
->attribute('user', v::stringType(), true)
45-
->attribute('password', v::stringType(), true)
46-
->attribute('schema', v::stringType(), true),
43+
->property('host', v::stringType(), true)
44+
->property('user', v::stringType(), true)
45+
->property('password', v::stringType(), true)
46+
->property('schema', v::stringType(), true),
4747
true
4848
)
4949
->setName('the given data')

tests/integration/issue-1376.phpt

+11-11
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ $input = (object) [
1515
'author' => 'foo',
1616
];
1717

18-
$postValidator = v::attribute('title', v::length(2, 3)->stringType())
19-
->attribute('description', v::stringType())
20-
->attribute('author', v::intType()->length(1, 2))
21-
->attribute('user', v::intVal()->length(1, 2));
18+
$postValidator = v::property('title', v::length(2, 3)->stringType())
19+
->property('description', v::stringType())
20+
->property('author', v::intType()->length(1, 2))
21+
->property('user', v::intVal()->length(1, 2));
2222
try {
2323
$postValidator->assert($input);
2424
} catch (NestedValidationException $e) {
@@ -39,30 +39,30 @@ try {
3939
?>
4040
--EXPECT--
4141
- All of the required rules must pass for `[object] (stdClass: { "author": "foo" })`
42-
- Attribute title must be present
43-
- Attribute description must be present
42+
- Property title must be present
43+
- Property description must be present
4444
- All of the required rules must pass for author
4545
- author must be of type integer
4646
- author must have a length between 1 and 2
47-
- Attribute user must be present
47+
- Property user must be present
4848

4949
array(2) {
5050
["level"]=>
5151
int(0)
5252
["message"]=>
53-
string(31) "Attribute title must be present"
53+
string(30) "Property title must be present"
5454
}
5555
array(2) {
5656
["level"]=>
5757
int(0)
5858
["message"]=>
59-
string(37) "Attribute description must be present"
59+
string(36) "Property description must be present"
6060
}
6161
array(2) {
6262
["level"]=>
6363
int(0)
6464
["message"]=>
65-
string(30) "Attribute author must be valid"
65+
string(29) "Property author must be valid"
6666
}
6767
array(2) {
6868
["level"]=>
@@ -86,5 +86,5 @@ array(2) {
8686
["level"]=>
8787
int(0)
8888
["message"]=>
89-
string(30) "Attribute user must be present"
89+
string(29) "Property user must be present"
9090
}

0 commit comments

Comments
 (0)