Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UpdateFeatureRequest
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 rules
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 messages
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Http\Requests\v2\Feature;
4
5use App\Http\Models\Feature;
6use App\Http\Requests\v2\Parameter\Concerns\AuthorizesVengresoAdmin;
7use Illuminate\Foundation\Http\FormRequest;
8use Illuminate\Validation\Rule;
9
10/**
11 * Request for updating an existing feature.
12 *
13 * @property string|null $key Unique identifier key (e.g., "character_limit", "bold")
14 * @property string|null $name Human-readable name (e.g., "Character Limit")
15 * @property string|null $description Description of the feature
16 * @property string|null $value_type Type of value: boolean, integer, string, array, object
17 * @property string|null $category Feature category: formatting, limits, storage, ai, general
18 * @property mixed $default_value Default value when not overridden by plan
19 * @property bool|null $is_active Whether the feature is active
20 * @property int|null $display_order Order for display purposes
21 */
22class UpdateFeatureRequest extends FormRequest
23{
24    use AuthorizesVengresoAdmin;
25
26    /**
27     * Get the validation rules that apply to the request.
28     *
29     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
30     */
31    public function rules(): array
32    {
33        $featureId = $this->route('feature')?->_id ?? $this->route('feature');
34
35        return [
36            'key' => [
37                'sometimes',
38                'string',
39                'max:100',
40                'regex:/^[a-z][a-z0-9_]*$/',
41                Rule::unique('features', 'key')->ignore($featureId, '_id'),
42            ],
43            'name' => 'sometimes|string|max:255',
44            'description' => 'nullable|string|max:1000',
45            'value_type' => ['sometimes', 'string', Rule::in(Feature::VALUE_TYPES)],
46            'category' => ['sometimes', 'string', Rule::in(Feature::CATEGORIES)],
47            'default_value' => 'sometimes',
48            'is_active' => 'sometimes|boolean',
49            'display_order' => 'sometimes|integer|min:0',
50        ];
51    }
52
53    /**
54     * Get custom messages for validator errors.
55     *
56     * @return array<string, string>
57     */
58    public function messages(): array
59    {
60        return [
61            'key.regex' => 'The key must start with a letter and contain only lowercase letters, numbers, and underscores.',
62            'key.unique' => 'A feature with this key already exists.',
63            'value_type.in' => 'The value type must be one of: '.implode(', ', Feature::VALUE_TYPES),
64            'category.in' => 'The category must be one of: '.implode(', ', Feature::CATEGORIES),
65        ];
66    }
67}