Skip to content

Commit

Permalink
Rename "Attribute" rule to "Property"
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
henriquemoody committed Jan 28, 2024
1 parent 715bf4b commit 250fd93
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 110 deletions.
12 changes: 6 additions & 6 deletions docs/feature-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ $usernameValidator = v::alnum()->noWhitespace()->length(1, 15);
$usernameValidator->validate('alganet'); // true
```

## Validating object attributes
## Validating object properties

Given this simple object:

Expand All @@ -38,11 +38,11 @@ $user->name = 'Alexandre';
$user->birthdate = '1987-07-01';
```

Is possible to validate its attributes in a single chain:
Is possible to validate its properties in a single chain:

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

$userValidator->validate($user); // true
```
Expand Down Expand Up @@ -180,7 +180,7 @@ try {

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

```no-highlight
Array
Expand Down Expand Up @@ -230,7 +230,7 @@ in the chain fails.

## Validator name

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

```php
Expand Down
8 changes: 4 additions & 4 deletions docs/list-of-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@

- [AllOf](rules/AllOf.md)
- [AnyOf](rules/AnyOf.md)
- [Attribute](rules/Attribute.md)
- [Call](rules/Call.md)
- [Each](rules/Each.md)
- [Key](rules/Key.md)
Expand All @@ -169,6 +168,7 @@
- [Nullable](rules/Nullable.md)
- [OneOf](rules/OneOf.md)
- [Optional](rules/Optional.md)
- [Property](rules/Property.md)
- [When](rules/When.md)

## Numbers
Expand Down Expand Up @@ -197,9 +197,9 @@

## Objects

- [Attribute](rules/Attribute.md)
- [Instance](rules/Instance.md)
- [ObjectType](rules/ObjectType.md)
- [Property](rules/Property.md)

## Strings

Expand Down Expand Up @@ -240,10 +240,10 @@

## Structures

- [Attribute](rules/Attribute.md)
- [Key](rules/Key.md)
- [KeyNested](rules/KeyNested.md)
- [KeySet](rules/KeySet.md)
- [Property](rules/Property.md)

## Types

Expand Down Expand Up @@ -277,7 +277,6 @@
- [AnyOf](rules/AnyOf.md)
- [ArrayType](rules/ArrayType.md)
- [ArrayVal](rules/ArrayVal.md)
- [Attribute](rules/Attribute.md)
- [Base](rules/Base.md)
- [Base64](rules/Base64.md)
- [Between](rules/Between.md)
Expand Down Expand Up @@ -386,6 +385,7 @@
- [PostalCode](rules/PostalCode.md)
- [PrimeNumber](rules/PrimeNumber.md)
- [Printable](rules/Printable.md)
- [Property](rules/Property.md)
- [Punct](rules/Punct.md)
- [Readable](rules/Readable.md)
- [Regex](rules/Regex.md)
Expand Down
47 changes: 0 additions & 47 deletions docs/rules/Attribute.md

This file was deleted.

2 changes: 1 addition & 1 deletion docs/rules/Key.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ Version | Description
See also:

- [ArrayVal](ArrayVal.md)
- [Attribute](Attribute.md)
- [Each](Each.md)
- [KeyNested](KeyNested.md)
- [KeySet](KeySet.md)
- [KeyValue](KeyValue.md)
- [Property](Property.md)
2 changes: 1 addition & 1 deletion docs/rules/KeyNested.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ Version | Description
***
See also:

- [Attribute](Attribute.md)
- [Key](Key.md)
- [KeyValue](KeyValue.md)
- [Property](Property.md)

[Yii2 ArrayHelper]: https://github.com/yiisoft/yii2/blob/68c30c1/framework/helpers/BaseArrayHelper.php "Yii2 ArrayHelper"
2 changes: 1 addition & 1 deletion docs/rules/ObjectType.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Version | Description
See also:

- [ArrayType](ArrayType.md)
- [Attribute](Attribute.md)
- [BoolType](BoolType.md)
- [BoolVal](BoolVal.md)
- [CallableType](CallableType.md)
Expand All @@ -33,6 +32,7 @@ See also:
- [IntType](IntType.md)
- [NullType](NullType.md)
- [Number](Number.md)
- [Property](Property.md)
- [ResourceType](ResourceType.md)
- [StringType](StringType.md)
- [StringVal](StringVal.md)
Expand Down
48 changes: 48 additions & 0 deletions docs/rules/Property.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Property

- `Property(string $name)`
- `Property(string $name, Validatable $rule)`
- `Property(string $name, Validatable $rule, bool $mandatory)`

Validates an object property, even private ones.

```php
$obj = new stdClass;
$obj->foo = 'bar';

v::property('foo')->validate($obj); // true
```

You can also validate the property itself:

```php
v::property('foo', v::equals('bar'))->validate($obj); // true
```

Third parameter makes the property presence optional:

```php
v::property('lorem', v::stringType(), false)->validate($obj); // true
```

The name of this validator is automatically set to the property name.

## Categorization

- Nesting
- Objects
- Structures

## Changelog

Version | Description
--------|-------------
3.0.0 | Renamed from `Attribute` to `Property`
0.3.9 | Created

***
See also:

- [Key](Key.md)
- [KeyNested](KeyNested.md)
- [ObjectType](ObjectType.md)
2 changes: 1 addition & 1 deletion library/ChainedValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function arrayType(): ChainedValidator;

public function arrayVal(): ChainedValidator;

public function attribute(
public function property(
string $reference,
?Validatable $validator = null,
bool $mandatory = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Respect\Validation\Exceptions;

final class AttributeException extends NestedValidationException implements NonOmissibleException
final class PropertyException extends NestedValidationException implements NonOmissibleException
{
public const NOT_PRESENT = 'not_present';
public const INVALID = 'invalid';
Expand All @@ -19,12 +19,12 @@ final class AttributeException extends NestedValidationException implements NonO
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::NOT_PRESENT => 'Attribute {{name}} must be present',
self::INVALID => 'Attribute {{name}} must be valid',
self::NOT_PRESENT => 'Property {{name}} must be present',
self::INVALID => 'Property {{name}} must be valid',
],
self::MODE_NEGATIVE => [
self::NOT_PRESENT => 'Attribute {{name}} must not be present',
self::INVALID => 'Attribute {{name}} must not validate',
self::NOT_PRESENT => 'Property {{name}} must not be present',
self::INVALID => 'Property {{name}} must not validate',
],
];

Expand Down
6 changes: 3 additions & 3 deletions library/Rules/Attribute.php → library/Rules/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
use function is_object;
use function property_exists;

final class Attribute extends AbstractRelated
final class Property extends AbstractRelated
{
public function __construct(string $reference, ?Validatable $rule = null, bool $mandatory = true)
public function __construct(string $name, ?Validatable $rule = null, bool $mandatory = true)
{
parent::__construct($reference, $rule, $mandatory);
parent::__construct($name, $rule, $mandatory);
}

public function getReferenceValue(mixed $input): mixed
Expand Down
2 changes: 1 addition & 1 deletion library/StaticValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function arrayType(): ChainedValidator;

public static function arrayVal(): ChainedValidator;

public static function attribute(
public static function property(
string $reference,
?Validatable $validator = null,
bool $mandatory = true
Expand Down
20 changes: 10 additions & 10 deletions tests/integration/assert-with-attributes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ exceptionFullMessage(static function (): void {
];
$object = json_decode((string) json_encode($array));
v::create()
->attribute(
->property(
'mysql',
v::create()
->attribute('host', v::stringType(), true)
->attribute('user', v::stringType(), true)
->attribute('password', v::stringType(), true)
->attribute('schema', v::stringType(), true),
->property('host', v::stringType(), true)
->property('user', v::stringType(), true)
->property('password', v::stringType(), true)
->property('schema', v::stringType(), true),
true
)
->attribute(
->property(
'postgresql',
v::create()
->attribute('host', v::stringType(), true)
->attribute('user', v::stringType(), true)
->attribute('password', v::stringType(), true)
->attribute('schema', v::stringType(), true),
->property('host', v::stringType(), true)
->property('user', v::stringType(), true)
->property('password', v::stringType(), true)
->property('schema', v::stringType(), true),
true
)
->setName('the given data')
Expand Down
22 changes: 11 additions & 11 deletions tests/integration/issue-1376.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ $input = (object) [
'author' => 'foo',
];

$postValidator = v::attribute('title', v::length(2, 3)->stringType())
->attribute('description', v::stringType())
->attribute('author', v::intType()->length(1, 2))
->attribute('user', v::intVal()->length(1, 2));
$postValidator = v::property('title', v::length(2, 3)->stringType())
->property('description', v::stringType())
->property('author', v::intType()->length(1, 2))
->property('user', v::intVal()->length(1, 2));
try {
$postValidator->assert($input);
} catch (NestedValidationException $e) {
Expand All @@ -39,30 +39,30 @@ try {
?>
--EXPECT--
- All of the required rules must pass for `stdClass { +$author="foo" }`
- Attribute title must be present
- Attribute description must be present
- Property title must be present
- Property description must be present
- All of the required rules must pass for author
- author must be of type integer
- author must have a length between 1 and 2
- Attribute user must be present
- Property user must be present

array(2) {
["level"]=>
int(0)
["message"]=>
string(31) "Attribute title must be present"
string(30) "Property title must be present"
}
array(2) {
["level"]=>
int(0)
["message"]=>
string(37) "Attribute description must be present"
string(36) "Property description must be present"
}
array(2) {
["level"]=>
int(0)
["message"]=>
string(30) "Attribute author must be valid"
string(29) "Property author must be valid"
}
array(2) {
["level"]=>
Expand All @@ -86,5 +86,5 @@ array(2) {
["level"]=>
int(0)
["message"]=>
string(30) "Attribute user must be present"
string(29) "Property user must be present"
}
4 changes: 2 additions & 2 deletions tests/integration/readme/example_1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ $user = new stdClass();
$user->name = 'Alexandre';
$user->birthdate = '1987-07-01';

$userValidator = v::attribute('name', v::stringType()->length(1, 32))
->attribute('birthdate', v::dateTime()->minAge(18));
$userValidator = v::property('name', v::stringType()->length(1, 32))
->property('birthdate', v::dateTime()->minAge(18));

$userValidator->assert($user);

Expand Down
Loading

0 comments on commit 250fd93

Please sign in to comment.