use Validator::validate()

This commit is contained in:
shibafu 2020-08-09 10:59:44 +09:00
parent 134983d13d
commit 73c64f0f27

View File

@ -12,6 +12,7 @@ use Carbon\Carbon;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
class WebhookController extends Controller class WebhookController extends Controller
{ {
@ -26,9 +27,7 @@ class WebhookController extends Controller
], 404); ], 404);
} }
$inputs = $request->all(); $validator = Validator::make($request->all(), [
$validator = Validator::make($inputs, [
'checked_in_at' => 'nullable|date|after_or_equal:2000-01-01 00:00:00|before_or_equal:2099-12-31 23:59:59', 'checked_in_at' => 'nullable|date|after_or_equal:2000-01-01 00:00:00|before_or_equal:2099-12-31 23:59:59',
'note' => 'nullable|string|max:500', 'note' => 'nullable|string|max:500',
'link' => 'nullable|url|max:2000', 'link' => 'nullable|url|max:2000',
@ -40,7 +39,9 @@ class WebhookController extends Controller
'tags.*.not_regex' => 'The :attribute cannot contain spaces, tabs and newlines.' 'tags.*.not_regex' => 'The :attribute cannot contain spaces, tabs and newlines.'
]); ]);
if ($validator->fails()) { try {
$inputs = $validator->validate();
} catch (ValidationException $e) {
return response()->json([ return response()->json([
'status' => 422, 'status' => 422,
'error' => [ 'error' => [