Skip to content

Commit

Permalink
Adds information about handling Casting Errors (#664)
Browse files Browse the repository at this point in the history
fixes #653
  • Loading branch information
butschster authored Nov 24, 2023
1 parent b835f2c commit 5f8be3f
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions docs/en/filters/configuration.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Filters — Getting started

The `spiral/filters` is a powerful component for filtering and optional validating input data. It allows you to define a
set of rules for each input field, and then use those rules to ensure that the input data is in the correct format and
meets any other requirements you have set. You can use filters to validate data from various sources like HTTP requests,
set of rules for each input field, and then use those rules to ensure that the input data is in the correct format and
meets any other requirements you have set. You can use filters to validate data from various sources like HTTP requests,
gRPC requests, console commands, and others.

**Here is a simple example of a filter:**
Expand Down Expand Up @@ -155,7 +155,8 @@ For more advanced scenarios, where data needs to be transformed into custom type

### Understanding the Caster

Before diving into the application of casters, it's essential to comprehend the `Spiral\Filters\Model\Mapper\CasterInterface`.
Before diving into the application of casters, it's essential to comprehend
the `Spiral\Filters\Model\Mapper\CasterInterface`.

```php Spiral\Filters\Model\Mapper\CasterInterface
use Spiral\Filters\Model\FilterInterface;
Expand Down Expand Up @@ -257,9 +258,36 @@ class AppBootloader extends Bootloader
}
```

After registering the caster, whenever you define filters with properties that match the caster's supported types,
After registering the caster, whenever you define filters with properties that match the caster's supported types,
Spiral will automatically employ the registered caster to transform the data.

### Handling Casting Errors

When dealing with request data filters, it's crucial to ensure that these parameters match the expected data types. For
instance, a parameter expected as a `string` should not be processed if it comes in a different format, like an `array`
or an `integer`. The Spiral Framework offers an effective solution to handle such type mismatches gracefully.

You can use the `Spiral\Filters\Attribute\CastingErrorMessage` attribute for any property that requires type validation:

```php app/src/Endpoint/Web/Filter/UserFilter.php
namespace App\Endpoint\Web\Filter;

use Spiral\Filters\Attribute\Input\Query;
use Spiral\Filters\Model\Filter;
use Spiral\Filters\Attribute\CastingErrorMessage;

final class UserFilter extends Filter
{
#[Query(key: 'username')]
#[CastingErrorMessage('Invalid type')]
public string $username;
}
```

In this scenario, the `username` is expected to be a string. However, there might be instances where the input data is
of the wrong type, such as an `array` or an `integer`. In such cases, the filter will catch the exception and return the
validation error message.

## Validation

By default, filters do not perform validation. However, if you want to validate a filter, you can implement the
Expand Down

0 comments on commit 5f8be3f

Please sign in to comment.