Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed May 22, 2024
1 parent 86a75dd commit 40822f3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 59 deletions.
52 changes: 25 additions & 27 deletions docs/guide/en/creating-custom-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ final class RgbColor implements RuleInterface
}
```

> **Note:** [Readonly properties] are supported only starting from PHP 8.1.
> **Note:** [readonly properties] are supported only starting from PHP 8.1.
Besides required interface method implementations it only contains customizable error message. Of course, more features
can be added - conditional validation, client options, etc. But this is a bare minimum to start with.

### Creating a handler

The 2nd step is creating the handler. Let's define what is exactly a valid [RGB color]:
The 2nd step is creating the handler. Let's define what is exactly a valid RGB color:

- It's an array (list to be exact).
- Contains exactly 3 items.
Expand Down Expand Up @@ -96,7 +96,7 @@ final class RgbColorHandler implements RuleHandlerInterface
#### More specific error messages

Prefer more specific error messages to broad ones. Even this requires a bigger amount of messages and code, it helps to
quicker understand what exactly is wrong with input data. [RGB color] is quite simple and compact structure, but in case
quicker understand what exactly is wrong with input data. RGB color is quite simple and compact structure, but in case
of more complex data it will definitely pay off.

Keeping this in mind the rule can be rewritten something like this:
Expand Down Expand Up @@ -130,9 +130,10 @@ final class RgbColor implements RuleInterface
}
```

> **Notes:**
>- [Readonly properties] are supported only starting from PHP 8.1.
>- Formatting used in `$incorrectItemTypeMessage` and `$incorrectItemValueMessage` requires [`intl` PHP] extension.
> **Note:** [readonly properties] are supported only starting from PHP 8.1.
> **Note:** Formatting used in `$incorrectItemTypeMessage` and `$incorrectItemValueMessage` requires `intl` PHP
> extension.
The handler needs to be changed accordingly. Let's also add error parameters to be able to use them as placeholders in
message templates:
Expand Down Expand Up @@ -200,7 +201,7 @@ final class RgbColorHandler implements RuleHandlerInterface
```

> **Note:** It's also a good idea to utilize the features of used language version. For example, for PHP >= 8.1 we can
> simplify checking that a given array is a list with [array_is_list()] function.
> simplify checking that a given array is a list with [array_is_list] function.
#### Using built-in rules if possible

Expand All @@ -209,7 +210,7 @@ unnecessary to create a custom rule.

##### Replacing with `Composite`

The example with [RGB color] can be significantly simplified after realizing that it's also possible to achieve the same
The example with RGB color can be significantly simplified after realizing that it's also possible to achieve the same
effect by just using only built-in rules:

```php
Expand All @@ -223,7 +224,7 @@ $rules = [
];
```

Making them reusable isn't much harder - the whole set can be placed inside a [`Composite`] rule and used as a single
Making them reusable isn't much harder - the whole set can be placed inside a `Composite` rule and used as a single
regular rule.

```php
Expand All @@ -247,7 +248,7 @@ final class RgbColorRuleSet extends Composite
$result = (new Validator())->validate([205, 92, 92], new RgbColorRuleSet());
```

##### Replacing with separate rules and [`when`]
##### Replacing with separate rules and `when`

Below is an attempt at using validation context for validating attributes depending on each other:

Expand Down Expand Up @@ -341,7 +342,7 @@ final class Yaml implements RuleInterface
}
```

> **Note:** [Readonly properties] are supported only starting from PHP 8.1.
> **Note:** [readonly properties] are supported only starting from PHP 8.1.
Handler:

Expand Down Expand Up @@ -386,10 +387,10 @@ final class YamlHandler implements RuleHandlerInterface
}
```

> **Notes:**
>- Using [`yaml_parse()`] additionally requires [`yaml PHP`] extension.
>- Processing untrusted user input with [`yaml_parse()`] can be dangerous with certain settings. Please refer to
> docs for more details.
> **Note:** Using [yaml_parse] additionally requires `yaml` PHP extension.
> **Note:** Processing untrusted user input with `yaml_parse()` can be dangerous with certain settings. Please refer to
> [yaml_parse] docs for more details.
### Wrapping validation

Expand Down Expand Up @@ -433,23 +434,20 @@ final class OnHandler implements RuleHandlerInterface
}
```

This code snippet is taken from [Yii Validator Scenarios] extension. Read more in [Scenarios] section.
This code snippet is taken from [Yii Validator Scenarios] extension by [Sergei Predvoditelev]. Read more in [Scenarios]
section.

## Making an extension

With a custom rule, you can go even further. If it's not too project-specific, and you feel that it might be useful to
someone else - make it available as an [`extension`].
someone else - make it available as an extension.

[Scenarios]: extensions.md#scenarios
[Yii Validator Scenarios]: https://github.com/vjik/yii-validator-scenarios
[RGB color]: https://en.wikipedia.org/wiki/RGB_color_model
[Readonly properties]: https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.readonly-properties
[`intl` PHP]: https://www.php.net/manual/en/book.intl.php
[array_is_list()]: https://www.php.net/manual/en/function.array-is-list.php
[`Composite`]: built-in-rules-composite.md
[readonly properties]: https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.readonly-properties
[array_is_list]: https://www.php.net/manual/en/function.array-is-list.php
[YAML]: https://en.wikipedia.org/wiki/YAML
[`yaml_parse()`]: https://www.php.net/manual/en/function.yaml-parse.php
[`yaml PHP`]: https://www.php.net/manual/en/book.yaml.php
[yaml_parse]: https://www.php.net/manual/en/function.yaml-parse.php
[scenarios from Yii 2]: https://www.yiiframework.com/doc/guide/2.0/en/structure-models#scenarios
[`extension`]: https://www.yiiframework.com/doc/guide/2.0/en/structure-extensions#creating-extensions
[`when`]: conditional-validation.md#when
[Yii Validator Scenarios]: https://github.com/vjik/yii-validator-scenarios
[Sergei Predvoditelev]: https://github.com/vjik
[Scenarios]: extensions.md#scenarios
5 changes: 2 additions & 3 deletions docs/guide/en/customizing-error-messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ new Length(
);
```

A full list of supported placeholders with descriptions is available in [PHPDoc] for each message.
A full list of supported placeholders with descriptions is available in PHPDoc for each message.

## Translating error messages

Expand Down Expand Up @@ -138,8 +138,7 @@ $form = new ChangePasswordForm();
$result = (new Validator())->validate($form);
```

[PHPDoc]: https://www.phpdoc.org/
[PR]: https://github.com/yiisoft/validator/pulls
[Yii Translator]: https://github.com/yiisoft/translator
[Yii Config]: https://github.com/yiisoft/config
[Yii DI]: https://github.com/yiisoft/di
[PR]: https://github.com/yiisoft/validator/pulls
9 changes: 6 additions & 3 deletions docs/guide/en/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ approach is discouraged and the recommended way with this package is using separ
Sure, this will lead to some code duplication, but it's acceptable and will pay off in the future.
Anyway, we decided to make it available through an extension, but use it with caution.

[Yii Validator Scenarios] adds special `On` rule which allows to wrap other rules by declaring specific scenarios.
[Yii Validator Scenarios] package from a core team member [Sergei Predvoditelev] adds
special `On` rule which allows to wrap other rules by declaring specific scenarios.

An example of the class using scenarios:

Expand Down Expand Up @@ -58,7 +59,8 @@ $result = (new Validator())->validate($userDto, context: $context);

## Wrapper for Symfony rules

[Yii Validator Symfony Rule] adapts [constraints from Symfony framework] to be used as rules in Yii Validator.
[Yii Validator Symfony Rule] package from a core team member [Sergei Predvoditelev]
adapts [constraints from Symfony framework] to be used as rules in Yii Validator.

Using it is straightforward, all you have to do is wrap a Symfony constraint (or a list of them) with `SymfonyRule` rule
provided by this extension.
Expand Down Expand Up @@ -91,8 +93,9 @@ final class Car
public int $number = 13;
}
```

[scenarios]: https://www.yiiframework.com/doc/guide/2.0/en/structure-models#scenarios
[Yii Validator Scenarios]: https://github.com/vjik/yii-validator-scenarios
[Sergei Predvoditelev]: https://github.com/vjik
[Yii Validator Symfony Rule]: https://github.com/vjik/yii-validator-symfony-rule
[constraints from Symfony framework]: https://symfony.com/doc/current/reference/constraints.html
28 changes: 13 additions & 15 deletions docs/guide/en/result.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ multiple attributes depending on each other for example. Use the following `Resu
$result->getCommonErrorMessages();
```

The output will be:
The output for example above:

```php
[
Expand All @@ -78,7 +78,7 @@ This list can be also filtered by a specific attribute. Only top-level attribute
$result->getAttributeErrorMessages('email');
```

The output will be:
The output for example above:

```php
[
Expand Down Expand Up @@ -113,7 +113,7 @@ An example of output:

Note that the result is always a 2-dimensional array with attribute names as keys at the first nesting level. This means
that further nesting of attributes is not supported (but could be achieved
by using [`getErrorMessagesIndexedByPath()`]).
by using [`getErrorMessagesIndexedByPath()`](#error-messages-indexed-by-path)).
Returning to the previous example, when `name` and `email` belong to a `user` attribute, the output will be:

```php
Expand All @@ -128,13 +128,14 @@ Returning to the previous example, when `name` and `email` belong to a `user` at
];
```

Also keep in mind that attribute names must be strings, even when used with [`Each`]:
Also keep in mind that attribute names must be strings, even when used with `Each`:

```php
$rule = new Each([new Number(min: 21)]),
```

With input containing non-string keys for top level attributes, for example `[21, 22, 23, 20]`, `InvalidArgumentException` will be thrown.
With input containing non-string keys for top level attributes, for example, `[21, 22, 23, 20]`,
`InvalidArgumentException` will be thrown.

Even array `['1' => 21, '2' => 22, '3' => 23, '4' => 20]` will cause an error, because PHP [will cast keys to the int type].

Expand All @@ -151,7 +152,7 @@ But if given array with string keys `['1a' => 21, '2b' => 22, '3c' => 23, '4d' =
### Error messages indexed by path

This is probably the most advanced representation offered by built-in methods. The grouping is done by path - a
concatenated attribute sequence showing the location of errored value within a data structure. If the separator is customizable,
concatenated attribute sequence showing the location of errored value within a data structure. A separator is customizable,
dot notation is set as the default one. Use the following `Result` API call:

```php
Expand All @@ -173,7 +174,7 @@ An example of output:
];
```

A path can contain integer elements too (when using the [`Each`] rule for example):
A path can contain integer elements too (when using the `Each` rule for example):

```php
[
Expand Down Expand Up @@ -207,9 +208,9 @@ $rules = [
];
```

However, when using the [`Nested`] rule with multiple attributes per rule set, special characters need to be escaped with
However, when using the `Nested` rule with multiple attributes per rule set, special characters need to be escaped with
a backslash (`\`) for value paths to be correct and to be possible to reverse them back from string to individual
items. See the [Using keys containing separator/shortcut] section for more details.
items. See the [Using keys containing separator / shortcut] section for more details.

This can be used as an alternative to using a custom separator.

Expand All @@ -224,7 +225,7 @@ use Yiisoft\Validator\Result;
$result->getAttributeErrorMessagesIndexedByPath('user');
```

The output will be:
The output for example above:

```php
[
Expand All @@ -235,7 +236,7 @@ The output will be:

## Error objects list

When even these representations are not enough, an initial unmodified list of error objects can be accessed by
When even these representations are not enough, an initial unmodified list of error objects can be accessed via
this method:

```php
Expand Down Expand Up @@ -285,8 +286,5 @@ use Yiisoft\Validator\Result;
$result->getAttributeErrors('email');
```

[`Each`]: built-in-rules-each.md
[`Nested`]: built-in-rules-nested.md
[`getErrorMessagesIndexedByPath()`]: #error-messages-indexed-by-path
[Using keys containing separator / shortcut]: built-in-rules-nested.md#using-keys-containing-separator--shortcut
[will cast keys to the int type]: https://www.php.net/manual/en/language.types.array.php
[Using keys containing separator/shortcut]: built-in-rules-nested.md#using-keys-containing-separator--shortcut
22 changes: 11 additions & 11 deletions docs/guide/en/using-validator.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Using validator

`Validator` allows to check data in any format. Here are some of the most common use cases.
Validator allows to check data in any format. Here are some of the most common use cases.

## Data

Expand All @@ -21,7 +21,7 @@ $rules = [
$result = (new Validator())->validate($value, $rules);
```

> **Note:** Use [`Each`] rule to validate multiple values of the same type.
> **Note:** Use [Each] rule to validate multiple values of the same type.
### Array

Expand Down Expand Up @@ -62,7 +62,7 @@ $rules = [
$result = (new Validator())->validate($data, $rules);
```

> **Note:** Use [`Nested`] rule to validate nested arrays and [`Each`] rule to validate multiple arrays.
> **Note:** Use [Nested] rule to validate nested arrays and [Each] rule to validate multiple arrays.
### Object

Expand Down Expand Up @@ -102,9 +102,9 @@ $person = new Person(name: 'John', age: 17, email: 'john@example.com', phone: nu
$result = (new Validator())->validate($person);
```

> **Notes:**
>- [Readonly properties] are supported only starting from PHP 8.1.
>- Use [`Nested`] rule to validate related objects and [`Each`] rule to validate multiple objects.
> **Note:** [readonly properties] are supported only starting from PHP 8.1.
> **Note:** Use [Nested] rule to validate related objects and [Each] rule to validate multiple objects.
### Custom data set

Expand Down Expand Up @@ -152,7 +152,7 @@ $result = (new Validator())->validate($data, $rules);

### Passing single rule

For a single rule there is an option to omit the array:
For a single rule, there is an option to omit the array:

```php
use Yiisoft\Validator\Rule\Number;
Expand All @@ -165,7 +165,7 @@ $result = (new Validator())->validate($value, $rule);

### Providing rules via dedicated object

It could help to reuse the same set of rules in different locations. Two ways are possible - using PHP attributes
Could help reuse the same set of rules across different places. Two ways are possible - using PHP attributes
and specifying explicitly via interface method implementation.

#### Using PHP attributes
Expand Down Expand Up @@ -251,6 +251,6 @@ $data = new Person(name: 'John', age: 18);
$result = (new Validator())->validate($data);
```

[`Each`]: built-in-rules-each.md
[`Nested`]: built-in-rules-nested.md
[Readonly properties]: https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.readonly-properties
[Each]: built-in-rules-each.md
[Nested]: built-in-rules-nested.md
[readonly properties]: https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.readonly-properties

0 comments on commit 40822f3

Please sign in to comment.