Skip to content

Commit f052ac2

Browse files
authored
Merge pull request #1 from karoldabro/wip
Init commit
2 parents cc4d15f + 2f44f7a commit f052ac2

26 files changed

Lines changed: 21963 additions & 1 deletion

.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/tests export-ignore
2+
phpunit.xml export-ignore
3+
docker-compose.yml export-ignore
4+
_ide_helper.php export-ignore

.github/workflows/laravel.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: tests
2+
3+
on:
4+
push:
5+
branches: [ "master" ]
6+
pull_request:
7+
branches: [ "master" ]
8+
9+
jobs:
10+
tests83:
11+
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
16+
with:
17+
php-version: '8.3'
18+
- uses: actions/checkout@v3
19+
- name: Install Dependencies
20+
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
21+
- name: Create Database
22+
run: |
23+
mkdir -p database
24+
touch database/database.sqlite
25+
- name: Execute tests (Unit and Feature tests) via PHPUnit
26+
env:
27+
DB_CONNECTION: testing
28+
DB_DATABASE: database/database.sqlite
29+
run: vendor/bin/phpunit
30+
31+
tests82:
32+
33+
runs-on: ubuntu-latest
34+
35+
steps:
36+
- uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
37+
with:
38+
php-version: '8.2'
39+
- uses: actions/checkout@v3
40+
- name: Install Dependencies
41+
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
42+
- name: Create Database
43+
run: |
44+
mkdir -p database
45+
touch database/database.sqlite
46+
- name: Execute tests (Unit and Feature tests) via PHPUnit
47+
env:
48+
DB_CONNECTION: testing
49+
DB_DATABASE: database/database.sqlite
50+
run: vendor/bin/phpunit

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
vendor
2+
.phpunit.result.cache
3+
.vscode
4+
storage/*
5+
!storage/.gitkeep
6+
.env
7+
Makefile
8+
.gitmodules
9+
composer.lock
10+
.idea
11+
.phpunit.cache

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 karoldabro
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,127 @@
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

Comments
 (0)