['required', 'string', 'max:20'], 'amount' => ['required', 'integer', 'min:1'], 'execute_date' => ['required', 'string', 'regex:/^\d{8}$/'], 'contract_id' => ['required', 'string', 'max:20'], 'card_seq' => ['nullable', 'integer'], ]; } /** * バリデーション失敗時のJSONエラーレスポンス */ protected function failedValidation(Validator $validator): void { throw new HttpResponseException(response()->json([ 'error' => [ 'code' => 'INVALID_REQUEST', 'message' => $validator->errors()->first(), ] ], 400)); } /** * 追加バリデーション(execute_dateの実在日チェック) */ public function withValidator($validator): void { $validator->after(function ($validator) { $executeDate = $this->input('execute_date'); if (!$executeDate) { return; } try { Carbon::createFromFormat('Ymd', $executeDate, 'Asia/Tokyo'); $errors = Carbon::getLastErrors() ?: ['warning_count' => 0, 'error_count' => 0]; if (($errors['warning_count'] ?? 0) > 0 || ($errors['error_count'] ?? 0) > 0) { $validator->errors()->add('execute_date', 'execute_dateの日付が不正です。'); } } catch (\Throwable) { $validator->errors()->add('execute_date', 'execute_dateの日付が不正です。'); } }); } }