77 lines
2.0 KiB
PHP
77 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
class AIChatRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'message' => 'required|string|min:1|max:2000',
|
|
'conversation_id' => 'nullable|integer|exists:ai_conversations,id',
|
|
'context' => 'nullable|array',
|
|
'context.module' => 'nullable|string|max:50',
|
|
'context.purpose' => 'nullable|string|max:50',
|
|
'context.data' => 'nullable|array',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom messages for validator errors.
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'message.required' => '消息内容不能为空',
|
|
'message.min' => '消息内容至少1个字符',
|
|
'message.max' => '消息内容最多2000个字符',
|
|
'conversation_id.exists' => '对话不存在',
|
|
'context.array' => '上下文必须是数组格式',
|
|
'context.module.max' => '模块名称最多50个字符',
|
|
'context.purpose.max' => '用途最多50个字符',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*/
|
|
protected function prepareForValidation()
|
|
{
|
|
// 确保context是数组
|
|
if ($this->has('context') && !is_array($this->context)) {
|
|
$this->merge([
|
|
'context' => [],
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get validated data with defaults.
|
|
*/
|
|
public function validatedData(): array
|
|
{
|
|
$validated = parent::validated();
|
|
|
|
// 设置默认值
|
|
if (!isset($validated['context'])) {
|
|
$validated['context'] = [];
|
|
}
|
|
|
|
return $validated;
|
|
}
|
|
} |