├── .github
└── FUNDING.yml
├── .gitignore
├── README.md
├── composer.json
├── composer.lock
├── phpunit.xml
├── src
├── Concerns
│ └── ValidatesInertiaInput.php
├── Exceptions
│ └── ValidatedException.php
└── ValidationParser.php
└── tests
└── TestCase.php
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: [juhlinus]
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | /vendor/
3 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ✔️ Kakunin
2 | ## 💰 Is this useful to you?
3 | **Consider [sponsoring me on github](https://github.com/sponsors/juhlinus)! 🙏**
4 |
5 | ## 💾 Installation
6 | ```
7 | composer require juhlinus/kakunin
8 | ```
9 |
10 | ## 🤔 Usage
11 | Kakunin relies on [Custom Form Requests](https://laravel.com/docs/7.x/validation#form-request-validation).
12 |
13 | Add the `ValidatesInertiaInput` trait to your newly generated form request like so:
14 |
15 | ```php
16 |
56 |
57 |
58 |
59 |
60 |
61 |
73 | ```
74 |
75 | Note that I'm passing a `validate` parameter. If this isn't passed then Kakunin will not validate your request.
76 |
77 | That's it! Happy validating!
78 |
79 | ## 📝 Configuration
80 |
81 | If you wish to change the `validate` to something else, then you can add `KAKUNIN_VALIDATION_KEY` to your `.env` file. Lastly, add the following to your `config/services.php` file:
82 |
83 | ```php
84 | 'kakunin' => [
85 | 'validation_key' => env('KAKUNIN_VALIDATION_KEY'),
86 | ],
87 | ```
88 |
89 | ## ⛩ That's a stupid name for a package
90 |
91 | Kakunin(確認) is the Japanese verb "to validate".
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "juhlinus/kakunin",
3 | "type": "library",
4 | "license": "MIT",
5 | "autoload": {
6 | "psr-4": {
7 | "Kakunin\\": "src"
8 | }
9 | },
10 | "autoload-dev": {
11 | "psr-4": {
12 | "Kakunin\\Tests\\": "tests/"
13 | }
14 | },
15 | "require-dev": {
16 | "orchestra/testbench": "^5.1"
17 | },
18 | "authors": [
19 | {
20 | "name": "Linus Juhlin",
21 | "email": "linus.juhlin@protonmail.com"
22 | }
23 | ],
24 | "require": {
25 | "php": "^7.4|^8.0"
26 | },
27 | "suggest": {
28 | "symfony/http-foundation": "Required for requests and responses.",
29 | "illuminate/support": "Required for redirecting after input has been validated."
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/phpunit.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 | tests
13 |
14 |
15 |
16 |
17 | src
18 |
19 |
20 |
--------------------------------------------------------------------------------
/src/Concerns/ValidatesInertiaInput.php:
--------------------------------------------------------------------------------
1 | getValidatorInstance());
25 |
26 | if ($this->request->get($validationParser->getValidationKey(), false)) {
27 | throw new ValidatedException();
28 | }
29 | }
30 |
31 | /**
32 | * Handle a failed validation attempt.
33 | *
34 | * @param \Illuminate\Validation\Validator $validator
35 | * @return void
36 | *
37 | * @throws \Illuminate\Validation\ValidationException
38 | */
39 | protected function failedValidation(Validator $validator): void
40 | {
41 | $validationParser = new ValidationParser($validator);
42 |
43 | if ($validationParser->shouldNotValidate()) {
44 | parent::failedValidation($validator);
45 | }
46 |
47 | $messages = $validationParser->parseValidationMessages();
48 |
49 | $exception = new ValidationException($validator);
50 |
51 | throw $exception->withMessages($messages);
52 | }
53 | }
--------------------------------------------------------------------------------
/src/Exceptions/ValidatedException.php:
--------------------------------------------------------------------------------
1 | validator = $validator;
21 | $this->validate_key = config(
22 | 'services.kakunin.validation_key',
23 | 'validate'
24 | );
25 | }
26 |
27 | /**
28 | * Returns whether or not we should validate the request
29 | *
30 | * @return boolean
31 | */
32 | public function shouldNotValidate(): bool
33 | {
34 | $this->validated_keys = array_keys(
35 | array_filter(
36 | $this->validator->getData()
37 | )
38 | );
39 |
40 | if (in_array($this->validate_key, $this->validated_keys)) {
41 | return false;
42 | }
43 |
44 | return true;
45 | }
46 |
47 | /**
48 | * Get validation key
49 | *
50 | * @return string
51 | */
52 | public function getValidationKey(): string
53 | {
54 | return $this->validate_key;
55 | }
56 |
57 | /**
58 | * Filters out the validation messages we don't need
59 | *
60 | * @return array
61 | */
62 | public function parseValidationMessages(): array
63 | {
64 | $parentValidatorMessages = $this->validator->messages()->messages();
65 |
66 | $messages = array_filter($parentValidatorMessages, function ($messageKey) {
67 | return in_array($messageKey, $this->validated_keys);
68 | }, ARRAY_FILTER_USE_KEY);
69 |
70 | return $messages;
71 | }
72 | }
--------------------------------------------------------------------------------
/tests/TestCase.php:
--------------------------------------------------------------------------------
1 |