Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use validation rule, expect 422 for validation failure #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions resources/js/vat-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ document.addEventListener('vue:loaded', function () {
return
}

console.log(result)

event.target.setCustomValidity(result ? '' : window.config.vat_validation.translations.failed)
event.target.reportValidity()
});
Expand Down Expand Up @@ -65,12 +63,19 @@ const validate = useMemoize(useThrottleFn(
let options = {
headers: {
Authorization: `Bearer ${token.value || mask.value}`,
Accept: 'application/json',
},
}

return await window
.rapidezAPI('post', 'vat-validate', data, options)
.catch(() => {
.catch((error) => {
if (FetchError.prototype.isPrototypeOf(error)) {
if (error.response.status === 422) {
return false
}
}

window.Notify(window.config.translations.errors.wrong, 'error')
return 'error'
})
Expand Down
21 changes: 6 additions & 15 deletions src/Http/Controllers/VatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

namespace Rapidez\VatValidation\Http\Controllers;

use Ibericode\Vat\Validator;
use Ibericode\Vat\Vies\ViesException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Rapidez\VatValidation\Rules\VatValid;

class VatController
{
/** @return array<string, mixed> */
public function __invoke(Request $request): JsonResponse
public function __invoke(Request $request): mixed
{
$request->validate([
'id' => 'string|required',
Expand All @@ -26,16 +23,10 @@ public function __invoke(Request $request): JsonResponse
}
}

try {
// Try validating the number
$validator = new Validator;
$result = Cache::remember('vat_' . $request->id, config('rapidez.vatvalidation.cache_duration'), function () use ($request, $validator) {
return $validator->validateVatNumber($request->id);
});
$request->validate([
'id' => new VatValid,
]);

return response()->json($result);
} catch (ViesException $exception) {
abort(503, $exception->getMessage());
}
return true;
}
}
31 changes: 31 additions & 0 deletions src/Rules/VatValid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rapidez\VatValidation\Rules;

use Closure;
use Ibericode\Vat\Validator;
use Ibericode\Vat\Vies\ViesException;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Cache;

class VatValid implements ValidationRule
{
/**
* Run the validation rule.
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
try {
$validator = new Validator;
$result = Cache::remember('vat_' . $value, config('rapidez.vatvalidation.cache_duration'), function () use ($value, $validator) {
return $validator->validateVatNumber($value);
});

if (!$result) {
$fail('Vat validation failed.');
}
} catch (ViesException $exception) {
$fail($exception->getMessage());
}
}
}