|
1 | | -# validation-error-codes |
| 1 | +# Validation Codes for Laravel |
| 2 | + |
| 3 | +This package enhances Laravel's validation error responses (status 422) by adding corresponding validation rule codes. After installation, the response format is as follows: |
| 4 | + |
| 5 | +```json |
| 6 | +{ |
| 7 | + "message": "validation errors", |
| 8 | + "errors": { |
| 9 | + "user_id": [ |
| 10 | + "Field user_id is required" |
| 11 | + ] |
| 12 | + }, |
| 13 | + "codes": { |
| 14 | + "user_id": [ |
| 15 | + "E104" |
| 16 | + ] |
| 17 | + } |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +## Installation |
| 22 | +First install the package using Composer: |
| 23 | +```shell |
| 24 | +composer require kdabrow/validation-codes |
| 25 | +``` |
| 26 | + |
| 27 | +Afterward, extend your `Exception\Handler` file with `Kdabrow\ValidationCodes\Handler`. |
| 28 | + |
| 29 | +## How It Works |
| 30 | + |
| 31 | +This package extends Laravel's default validation system by overwriting the default Handler to return the correct JSON response. |
| 32 | + |
| 33 | +## Configuration |
| 34 | + |
| 35 | +### Overwriting Validation Codes |
| 36 | + |
| 37 | +To publish the configuration and language files containing the codes, use Laravel's command: |
| 38 | + |
| 39 | +```shell |
| 40 | +php artisan publish --tag=validation_codes |
| 41 | +``` |
| 42 | + |
| 43 | +You can then change the validation codes corresponding to the given rules in the published file, which looks like this: |
| 44 | + |
| 45 | +```php |
| 46 | +<?php |
| 47 | + |
| 48 | +return [ |
| 49 | + 'fallback_error' => 'E0', // This error code is returned while error code isn't found in this file |
| 50 | + 'accepted' => 'E1', |
| 51 | + 'accepted_if' => 'E2', |
| 52 | + 'active_url' => 'E3', |
| 53 | + // ... |
| 54 | +]; |
| 55 | +``` |
| 56 | + |
| 57 | +### Returning Only Validation Codes (Without Messages) |
| 58 | + |
| 59 | +To return only validation codes, set `show_only_codes` to `true` in the `config/validation_codes.php` file. The response will be: |
| 60 | + |
| 61 | +```json |
| 62 | +{ |
| 63 | + "codes": { |
| 64 | + "user_id": [ |
| 65 | + "E104" |
| 66 | + ] |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +**Caution:** This might be a breaking change for your API. |
| 72 | + |
| 73 | +**Ensure your `Exception\Handler` extends `Kdabrow\ValidationCodes\Handler`.** |
| 74 | + |
| 75 | +### Adding an Error Code to Custom Validation Rules |
| 76 | + |
| 77 | +Add a static method `getCode` to your custom validation rule. For example: |
| 78 | + |
| 79 | +```php |
| 80 | +<?php |
| 81 | + |
| 82 | +use Illuminate\Contracts\Validation\ValidationRule; |
| 83 | + |
| 84 | +class YourCustomValidationRule implements ValidationRule |
| 85 | +{ |
| 86 | + public function validate(string $attribute, mixed $value, \Closure $fail): void |
| 87 | + { |
| 88 | + // validation logic |
| 89 | + } |
| 90 | + |
| 91 | + public static function getCode(): string |
| 92 | + { |
| 93 | + return 'E10000'; // The validation code to return |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +### Adding an Error Code to a Validation Rule that Extends Validator |
| 99 | + |
| 100 | +Add a fourth parameter to the `extend` function: |
| 101 | + |
| 102 | +```php |
| 103 | +<?php |
| 104 | + |
| 105 | +use Illuminate\Support\Facades\Validator; |
| 106 | +use Illuminate\Support\ServiceProvider; |
| 107 | + |
| 108 | +class YourServiceProvider extends ServiceProvider |
| 109 | +{ |
| 110 | + public function boot(): void |
| 111 | + { |
| 112 | + Validator::extend('your_rule', YourRule::class, 'message', 'E10000'); |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +### Adding an Error Code to an Anonymous Validation Function |
| 118 | + |
| 119 | +This is not supported. |
| 120 | + |
| 121 | +## Testing |
| 122 | + |
| 123 | +To run tests, use the following command: |
| 124 | + |
| 125 | +```shell |
| 126 | +docker compose exec php vendor/bin/phpunit |
| 127 | +``` |
0 commit comments