Skip to content

Commit

Permalink
Rewrite validation rule
Browse files Browse the repository at this point in the history
  • Loading branch information
axlon committed Jan 20, 2025
1 parent f44fbeb commit d4e4d79
Show file tree
Hide file tree
Showing 17 changed files with 527 additions and 781 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 4.0.0

### Changed

- Country codes in incorrect casing are no longer accepted
- Validation now passes on `null`. Add `required` where appropriate to enforce presence
- Validation based on a country field is now done via the `postal_code` rule

### Removed

- Support for Lumen
Expand All @@ -15,3 +21,4 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Custom error replacers `:countries` and `:examples`
- Manual validation through facade
- Validation overriding
- Validation rule `postal_code_with`, functionality has been merged into `postal_code`
42 changes: 12 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Worldwide postal code validation for Laravel, based on Google's Address Data Ser
- [Installation](#installation)
- [Usage](#usage)
- [Available rules](#available-rules)
- [Fluent API](#fluent-api)
- [Adding an error message](#adding-an-error-message)
- [Changelog](#changelog)
- [Contributing](#contributing)
Expand Down Expand Up @@ -59,58 +58,41 @@ package manually, you can do this by adding the following line to your `config/a
```

## Usage

Postal code validation perfectly integrates into your Laravel application, you can use it just like you would any
framework validation rule.

### Available rules

This package adds the following validation rules:

#### postal_code:foo,bar,...

The field under validation must be a valid postal code in at least one of the given countries. Arguments must be
countries in [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) format.

```php
'postal_code' => 'postal_code:NL,DE,FR,BE'
```

#### postal_code_with:foo,bar,...
The field under validation must be a postal code in at least one of the countries in the given fields _only if_ at least
one of the specified fields is present.

```php
'billing.country' => 'required|string|max:2',
...
'shipping.country' => 'nullable|string|max:2',
'shipping.postal_code' => 'postal_code_with:billing.country,shipping.country'
Validator::validate($data, [
'address.postal_code' => 'required|postal_code:NL,BE',
]);
```

### Fluent API
If you prefer using a fluent object style over string based rules, that's also available:
Alternatively, you may use an object-oriented approach:

```php
'postal_code' => [
PostalCode::for('NL')->or('BE'),
],
```
use Axlon\PostalCodeValidation\Rules\PostalCode;

The same goes for the `postal_code_with` rule:

```php
'billing.country' => 'required|string|max:2',
...
'shipping.country' => 'nullable|string|max:2',
'shipping.postal_code' => [
PostalCode::with('billing.country')->or('shipping.country')
],
Validator::validate($data, [
'address.postal_code' => ['required', PostalCode::of('NL', 'BE')],
]);
```

### Adding an error message

To add a meaningful error message, add the following lines to `resources/lang/{your language}/validation.php`:
To add a meaningful error message, add the following to `resources/lang/{language}/validation.php`:

```php
'postal_code' => ':Attribute is not a valid postal code.',
'postal_code_with' => ':Attribute is not a valid postal code.',
```

## Changelog
Expand Down
133 changes: 0 additions & 133 deletions phpstan-baseline.neon

This file was deleted.

1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
includes:
- phar://phpstan.phar/conf/bleedingEdge.neon
- ./phpstan-baseline.neon

parameters:
level: 10
Expand Down
45 changes: 0 additions & 45 deletions src/Extensions/PostalCode.php

This file was deleted.

58 changes: 0 additions & 58 deletions src/Extensions/PostalCodeFor.php

This file was deleted.

4 changes: 0 additions & 4 deletions src/PostalCodeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ public function passes(string $countryCode, ?string ...$postalCodes): bool
*/
public function patternFor(string $countryCode): ?string
{
$countryCode = strtoupper($countryCode);

if (array_key_exists($countryCode, self::ALIASES)) {
$countryCode = self::ALIASES[$countryCode];
}
Expand All @@ -98,8 +96,6 @@ public function patternFor(string $countryCode): ?string
*/
public function supports(string $countryCode): bool
{
$countryCode = strtoupper($countryCode);

return array_key_exists($countryCode, $this->patterns)
|| array_key_exists($countryCode, self::ALIASES);
}
Expand Down
Loading

0 comments on commit d4e4d79

Please sign in to comment.