kdabrow / validation-codes
Validation codes for API
Installs: 589
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 1
Forks: 0
Open Issues: 0
Type:package
Requires
- php: >=8.2.0
- illuminate/support: ^11.0
- illuminate/validation: ^11.0
Requires (Dev)
- orchestra/testbench: ^9.0
This package is auto-updated.
Last update: 2024-10-27 15:30:08 UTC
README
Validation Codes for Laravel
This package enhances Laravel's validation error responses (status 422) by adding corresponding validation rule codes. After installation, the response format is as follows:
{ "message": "validation errors", "errors": { "user_id": [ "Field user_id is required" ] }, "codes": { "user_id": [ "E104" ] } }
Error codes allow clients of your API to easily interpret returned validation errors in the way they want or need.
Installation
First, install the package using Composer:
How It Works
This package extends Laravel's default validation system by overwriting the default Handler to return the correct JSON response.
Configuration
Overwriting Validation Codes
To publish the configuration and language files containing the codes, use Laravel's command:
php artisan vendor:publish --tag=validation_codes
You can then change the validation codes corresponding to the given rules in the published file, which looks like this:
<?php return [ 'fallback_error' => 'E0', // This error code is returned when an error code isn't found in this file 'accepted' => 'E1', 'accepted_if' => 'E2', 'active_url' => 'E3', // ... ];
Returning Only Validation Codes (Without Messages)
To return only validation codes, set show_only_codes
to true
in the config/validation_codes.php
file. The response will be:
{ "codes": { "user_id": [ "E104" ] } }
Caution: This might be a breaking change for your API.
Ensure your Exception\Handler
extends Kdabrow\ValidationCodes\Handler
.
Adding an Error Code to Custom Validation Rules
Add a static method getCode
to your custom validation rule. For example:
<?php use Illuminate\Contracts\Validation\ValidationRule; class YourCustomValidationRule implements ValidationRule { public function validate(string $attribute, mixed $value, \Closure $fail): void { // validation logic } public static function getCode(): string { return 'E10000'; // The validation code to return } }
Adding an Error Code to a Validation Rule that Extends Validator
Add a fourth parameter to the extend
function:
<?php use Illuminate\Support\Facades\Validator; use Illuminate\Support\ServiceProvider; class YourServiceProvider extends ServiceProvider { public function boot(): void { Validator::extend('your_rule', YourRule::class, 'message', 'E10000'); } }
Adding an Error Code to an Anonymous Validation Function
This is not supported.
Testing
To run tests, use the following command:
docker compose exec php vendor/bin/phpunit