Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
21 changes: 12 additions & 9 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@ learn that just by looking at other rules. Pick the simple ones like `ArrayType`
to begin.

Before writing anything, feature or bug fix:

- Check if there is already an issue related to it (opened or closed) and if
someone is already working on that;
- If there is not, [open an issue][] and notify everybody that you're going
to work on that;
- If there is, create a comment to notify everybody that you're going to
work on that.
- If there is not, [open an issue][] and notify everybody that you're going
to work on that;
- If there is, create a comment to notify everybody that you're going to
work on that.
- Make sure that what you need is not done yet

## Adding a new validator

A common validator (rule) on Respect\Validation is composed of three classes:

* `library/Rules/YourRuleName.php`: the rule itself
* `tests/unit/Rules/YourRuleNameTest.php`: tests for the rule
- `library/Rules/YourRuleName.php`: the rule itself
- `tests/unit/Rules/YourRuleNameTest.php`: tests for the rule

The classes are pretty straightforward. In the sample below, we're going to
create a validator that validates if a string is equal to "Hello World".
Expand Down Expand Up @@ -65,8 +66,8 @@ use Respect\Validation\Message\Template;
use Respect\Validation\Rules\Core\Simple;

#[Template(
'{{name}} must be a Hello World',
'{{name}} must not be a Hello World',
'{{subject}} must be a Hello World',
'{{subject}} must not be a Hello World',
)]
final class HelloWorld extends Simple
{
Expand Down Expand Up @@ -155,6 +156,7 @@ After run `composer install` on the library's root directory you must run PHPUni
### Linux

You can test the project using the commands:

```sh
$ vendor/bin/phpunit
```
Expand All @@ -168,6 +170,7 @@ $ composer phpunit
### Windows

You can test the project using the commands:

```sh
> vendor\bin\phpunit
```
Expand All @@ -187,5 +190,5 @@ and changing it according to your needs.
[data provider]: https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers "PHPUnit Data Providers"
[open an issue]: http://github.com/Respect/Validation/issues
[PHP-FIG]: http://www.php-fig.org "PHP Framework Interop Group"
[project documentation]: https://respect-validation.readthedocs.io/ "Respect\Validation documentation"
[project documentation]: https://respect-validation.readthedocs.io/ "Respect\\Validation documentation"
[pull requests]: http://help.github.com/pull-requests "GitHub pull requests"
30 changes: 16 additions & 14 deletions docs/02-feature-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ if (v::intType()->positive()->isValid($input)) {
```

Note that you can combine multiple rules for a complex validation.

## Validating using exceptions

The `assert()` method throws an exception when validation fails. You can handle those exceptions with `try/catch` for more robust error handling.
Expand All @@ -33,24 +34,24 @@ v::intType()->positive()->assert($input);

Respect\Validation offers over 150 rules, many of which are designed to address common scenarios. Here’s a quick guide to some specific use cases and the rules that make validation straightforward.

* Using rules as **PHP Attributes**: [Attributes](rules/Attributes.md).
* Validating **Arrays**: [Key](rules/Key.md), [KeyOptional](rules/KeyOptional.md), [KeyExists](rules/KeyExists.md).
* Validating **Array structures**: [KeySet](rules/KeySet.md).
* Validating **Object properties**: [Property](rules/Property.md), [PropertyOptional](rules/PropertyOptional.md), [PropertyExists](rules/PropertyExists.md).
* Using **Conditional validation**: [NullOr](rules/NullOr.md), [UndefOr](rules/UndefOr.md), [When](rules/When.md).
* Using **Grouped validation**: [AllOf](rules/AllOf.md), [AnyOf](rules/AnyOf.md), [NoneOf](rules/NoneOf.md), [OneOf](rules/OneOf.md)
* Validating **Each** value in the input: [Each](rules/Each.md).
* Validating the **Length** of the input: [Length](rules/Length.md).
* Validating the **Maximum** value in the input: [Max](rules/Max.md).
* Validating the **Minimum** value in the input: [Min](rules/Min.md).
* Handling **Special cases**: [Lazy](rules/Lazy.md), [Circuit](rules/Circuit.md), [Call](rules/Call.md).
- Using rules as **PHP Attributes**: [Attributes](rules/Attributes.md).
- Validating **Arrays**: [Key](rules/Key.md), [KeyOptional](rules/KeyOptional.md), [KeyExists](rules/KeyExists.md).
- Validating **Array structures**: [KeySet](rules/KeySet.md).
- Validating **Object properties**: [Property](rules/Property.md), [PropertyOptional](rules/PropertyOptional.md), [PropertyExists](rules/PropertyExists.md).
- Using **Conditional validation**: [NullOr](rules/NullOr.md), [UndefOr](rules/UndefOr.md), [When](rules/When.md).
- Using **Grouped validation**: [AllOf](rules/AllOf.md), [AnyOf](rules/AnyOf.md), [NoneOf](rules/NoneOf.md), [OneOf](rules/OneOf.md)
- Validating **Each** value in the input: [Each](rules/Each.md).
- Validating the **Length** of the input: [Length](rules/Length.md).
- Validating the **Maximum** value in the input: [Max](rules/Max.md).
- Validating the **Minimum** value in the input: [Min](rules/Min.md).
- Handling **Special cases**: [Lazy](rules/Lazy.md), [Circuit](rules/Circuit.md), [Call](rules/Call.md).

### Custom templates

Define your own error message when the validation fails:

```php
v::between(1, 256)->assert($input, '{{name}} is not what I was expecting');
v::between(1, 256)->assert($input, '{{subject}} is not what I was expecting');
```

### Custom templates per rule
Expand All @@ -67,6 +68,7 @@ v::alnum()->lowercase()->assert($input, [
### Custom exception objects

Integrate your own exception objects when the validation fails:

```php
v::alnum()->assert($input, new DomainException('Not a valid username'));
```
Expand All @@ -87,7 +89,7 @@ v::alnum()->lowercase()->assert(

## Inverting validation rules

Use the `not` prefix to invert a validation rule.
Use the `not` prefix to invert a validation rule.

```php
v::notEquals('main')->assert($input);
Expand All @@ -109,7 +111,7 @@ $validator->assert('alexandre gaigalas');

## Customising validator names

Template messages include the placeholder `{{name}}`, which defaults to the input. Use `setName()` to replace it with a more descriptive label.
Template messages include the placeholder `{{subject}}`, which defaults to the input. Use `setName()` to replace it with a more descriptive label.

```php
v::dateTime('Y-m-d')
Expand Down
4 changes: 3 additions & 1 deletion docs/03-handling-exceptions.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Handling exceptions

The `Validator::assert()` method simplifies exception handling by throwing `ValidationException` exceptions when validation fails. These exceptions provide detailed feedback on what went wrong.

## Full exception message

The `getFullMessage()` method will return a full comprehensive explanation of rules that didn't pass in a nested Markdown list format.
Expand Down Expand Up @@ -51,6 +52,7 @@ Array
```

When validating with [Key](rules/Key.md) or [Property](rules/Property.md) the keys of will correspond to the name of the key or property that failed the validation.

## Custom templates

You can tailor the messages to better suit your needs.
Expand Down Expand Up @@ -100,7 +102,7 @@ use Respect\Validation\Validator as v;

$validator = v::alnum()->lowercase();
$validator->setTemplates([
'__root__' => '{{name}} is not valid',
'__root__' => '{{subject}} is not valid',
'alnum' => 'Usernames must contain only letters and digits',
'lowercase' => 'Usernames must be lowercase',
]);
Expand Down
2 changes: 1 addition & 1 deletion docs/05-message-placeholder-conversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ to convert them to string.
We use the `ParameterStringifier` to convert those parameters into a string.
Our default implementation will convert all parameters with
[Respect\Stringifier](https://github.com/Respect/Stringifier) unless the
parameter is called `name` and it is already a string.
parameter is called `subject` and it is already a string.

It is possible to overwrite that behavior by creating a custom implementation of
the `ParameterStringifier` and passing it to the `Factory`:
Expand Down
8 changes: 4 additions & 4 deletions docs/07-custom-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ You can also create and use your own rules. To do this, you will need to create
a rule and an exception to go with the rule.

To create a rule, you need to create a class that implements the `Rule` interface
and is within the Rules `namespace`. It is convenient to just extend the `Simple` or
and is within the Rules `namespace`. It is convenient to just extend the `Simple` or
`Standard` class. When the rule is called the logic inside the validate method will be
executed. Here's how the class should look:

Expand All @@ -15,8 +15,8 @@ use Respect\Validation\Message\Template;
use Respect\Validation\Rules\Core\Simple;

#[Template(
'{{name}} is something',
'{{name}} is not something',
'{{subject}} is something',
'{{subject}} is not something',
)]
final class Something extends Simple
{
Expand All @@ -27,7 +27,7 @@ final class Something extends Simple
}
```

The `'{{name}} is not something` message would be used then you call the rule
The `'{{subject}} is not something` message would be used then you call the rule
with the `not()`.

All classes in Validation are created by the `Factory` class. If you want
Expand Down
25 changes: 13 additions & 12 deletions docs/rules/AllOf.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ v::allOf(v::intVal(), v::positive())->isValid(15); // true

Used when some rules must be failed.

| Mode | Template |
|------------|------------------------------|
| `default` | {{name}} must pass the rules |
| `inverted` | {{name}} must pass the rules |
| Mode | Template |
| ---------- | ------------------------------- |
| `default` | {{subject}} must pass the rules |
| `inverted` | {{subject}} must pass the rules |

### `AllOf::TEMPLATE_ALL`

Used when all rules have failed.

| Mode | Template |
|------------|----------------------------------|
| `default` | {{name}} must pass all the rules |
| `inverted` | {{name}} must pass all the rules |
| Mode | Template |
| ---------- | ----------------------------------- |
| `default` | {{subject}} must pass all the rules |
| `inverted` | {{subject}} must pass all the rules |

## Template placeholders

| Placeholder | Description |
|-------------|------------------------------------------------------------------|
| `name` | The validated input or the custom validator name (if specified). |
| ----------- | ---------------------------------------------------------------- |
| `subject` | The validated input or the custom validator name (if specified). |

## Categorization

Expand All @@ -42,11 +42,12 @@ Used when all rules have failed.
## Changelog

| Version | Description |
|--------:|-----------------------------------------|
| ------: | --------------------------------------- |
| 3.0.0 | Require at least two rules to be passed |
| 0.3.9 | Created |

***
---

See also:

- [AnyOf](AnyOf.md)
Expand Down
25 changes: 13 additions & 12 deletions docs/rules/Alnum.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@ of extra chars passed as the parameter.

### `Alnum::TEMPLATE_STANDARD`

| Mode | Template |
|------------|-----------------------------------------------------------|
| `default` | {{name}} must contain only letters (a-z) and digits (0-9) |
| `inverted` | {{name}} must not contain letters (a-z) or digits (0-9) |
| Mode | Template |
| ---------- | ------------------------------------------------------------ |
| `default` | {{subject}} must contain only letters (a-z) and digits (0-9) |
| `inverted` | {{subject}} must not contain letters (a-z) or digits (0-9) |

### `Alnum::TEMPLATE_EXTRA`

| Mode | Template |
|------------|---------------------------------------------------------------------------------|
| `default` | {{name}} must contain only letters (a-z), digits (0-9), and {{additionalChars}} |
| `inverted` | {{name}} must not contain letters (a-z), digits (0-9), or {{additionalChars}} |
| Mode | Template |
| ---------- | ---------------------------------------------------------------------------------- |
| `default` | {{subject}} must contain only letters (a-z), digits (0-9), and {{additionalChars}} |
| `inverted` | {{subject}} must not contain letters (a-z), digits (0-9), or {{additionalChars}} |

## Template placeholders

| Placeholder | Description |
|-------------------|------------------------------------------------------------------|
| ----------------- | ---------------------------------------------------------------- |
| `additionalChars` | Additional characters that are considered valid. |
| `name` | The validated input or the custom validator name (if specified). |
| `subject` | The validated input or the custom validator name (if specified). |

## Categorization

Expand All @@ -56,11 +56,12 @@ of extra chars passed as the parameter.
## Changelog

| Version | Description |
|--------:|-------------------------------------------|
| ------: | ----------------------------------------- |
| 2.0.0 | Removed support to whitespaces by default |
| 0.3.9 | Created |

***
---

See also:

- [Alpha](Alpha.md)
Expand Down
25 changes: 13 additions & 12 deletions docs/rules/Alpha.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ v::alpha()->uppercase()->isValid('example'); // false

### `Alpha::TEMPLATE_STANDARD`

| Mode | Template |
|------------|------------------------------------------|
| `default` | {{name}} must contain only letters (a-z) |
| `inverted` | {{name}} must not contain letters (a-z) |
| Mode | Template |
| ---------- | ------------------------------------------- |
| `default` | {{subject}} must contain only letters (a-z) |
| `inverted` | {{subject}} must not contain letters (a-z) |

### `Alpha::TEMPLATE_EXTRA`

| Mode | Template |
|------------|------------------------------------------------------------------|
| `default` | {{name}} must contain only letters (a-z) and {{additionalChars}} |
| `inverted` | {{name}} must not contain letters (a-z) or {{additionalChars}} |
| Mode | Template |
| ---------- | ------------------------------------------------------------------- |
| `default` | {{subject}} must contain only letters (a-z) and {{additionalChars}} |
| `inverted` | {{subject}} must not contain letters (a-z) or {{additionalChars}} |

## Template placeholders

| Placeholder | Description |
|-------------------|------------------------------------------------------------------|
| ----------------- | ---------------------------------------------------------------- |
| `additionalChars` | Additional characters that are considered valid. |
| `name` | The validated input or the custom validator name (if specified). |
| `subject` | The validated input or the custom validator name (if specified). |

## Categorization

Expand All @@ -51,11 +51,12 @@ v::alpha()->uppercase()->isValid('example'); // false
## Changelog

| Version | Description |
|--------:|-------------------------------------------|
| ------: | ----------------------------------------- |
| 2.0.0 | Removed support to whitespaces by default |
| 0.3.9 | Created |

***
---

See also:

- [Alnum](Alnum.md)
Expand Down
25 changes: 13 additions & 12 deletions docs/rules/AlwaysInvalid.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ v::alwaysInvalid()->isValid('whatever'); // false

### `AlwaysInvalid::TEMPLATE_STANDARD`

| Mode | Template |
|------------|--------------------------|
| `default` | {{name}} must be valid |
| `inverted` | {{name}} must be invalid |
| Mode | Template |
| ---------- | --------------------------- |
| `default` | {{subject}} must be valid |
| `inverted` | {{subject}} must be invalid |

### `AlwaysInvalid::TEMPLATE_SIMPLE`

| Mode | Template |
|------------|---------------------|
| `default` | {{name}} is invalid |
| `inverted` | {{name}} is valid |
| Mode | Template |
| ---------- | ---------------------- |
| `default` | {{subject}} is invalid |
| `inverted` | {{subject}} is valid |

## Template placeholders

| Placeholder | Description |
|-------------|------------------------------------------------------------------|
| `name` | The validated input or the custom validator name (if specified). |
| ----------- | ---------------------------------------------------------------- |
| `subject` | The validated input or the custom validator name (if specified). |

## Categorization

Expand All @@ -37,10 +37,11 @@ v::alwaysInvalid()->isValid('whatever'); // false
## Changelog

| Version | Description |
|--------:|-------------|
| ------: | ----------- |
| 0.5.0 | Created |

***
---

See also:

- [AlwaysValid](AlwaysValid.md)
Expand Down
Loading