Skip to content

Commit ab7a413

Browse files
Merge pull request #75 from Relaticle/feat/validation-capabilities
feat: replace generic validation rules with field-type-owned validation capabilities
2 parents a0a6a24 + 9ae827c commit ab7a413

File tree

78 files changed

+4815
-2166
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+4815
-2166
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
applyTo: "src/**/*.php"
3+
---
4+
5+
# Filament v5 Namespace Conventions
6+
7+
This package targets Filament v5. These namespaces are correct:
8+
9+
- `Filament\Schemas\Components\Component` -- base class for layout components (Fieldset, Grid, Section, Tabs)
10+
- `Filament\Schemas\Components\Utilities\Get` / `Set` -- schema utilities
11+
- `Filament\Forms\Components\Component` -- base class for form field components
12+
- `Filament\Actions\` -- all actions (not `Filament\Tables\Actions\`)
13+
- `Filament\Support\Icons\Heroicon` -- icon enum
14+
15+
Do NOT flag `Filament\Schemas\Components\Component` as incorrect.
16+
17+
# Package Architecture
18+
19+
- Custom field types live in `src/FieldTypeSystem/Definitions/`
20+
- Validation capabilities live in `src/Validation/Capabilities/`
21+
- Each capability implements `Relaticle\CustomFields\Contracts\ValidationCapability`
22+
- `DateConstraintValue` is a Spatie Laravel Data class -- use `::from()` for hydration, not manual construction
23+
24+
# Data Patterns
25+
26+
- DTOs use `Spatie\LaravelData\Data` with `#[MapName(SnakeCaseMapper::class)]`
27+
- Feature flags via `FeatureManager::isEnabled(CustomFieldsFeature::*)`
28+
- Field type configuration via `FieldSchema` fluent builder

database/factories/CustomFieldFactory.php

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,11 @@ public function definition(): array
5454
/**
5555
* Configure the field with specific validation rules.
5656
*
57-
* @param array<string|array{name: string, parameters: array}> $rules
57+
* @param array<string, mixed> $rules
5858
*/
5959
public function withValidation(array $rules): self
6060
{
61-
return $this->state(function (array $attributes) use ($rules) {
62-
$validationRules = collect($rules)->map(function ($rule) {
63-
if (is_string($rule)) {
64-
return ['name' => $rule, 'parameters' => []];
65-
}
66-
67-
return $rule;
68-
})->toArray();
69-
70-
return ['validation_rules' => $validationRules];
71-
});
61+
return $this->state(fn () => ['validation_rules' => $rules]);
7262
}
7363

7464
/**
@@ -180,35 +170,9 @@ public function systemDefined(): self
180170
*/
181171
public function ofType(string $type): self
182172
{
183-
$defaultValidation = match ($type) {
184-
'text' => [
185-
['name' => 'string', 'parameters' => []],
186-
['name' => 'max', 'parameters' => [255]],
187-
],
188-
'number' => [
189-
['name' => 'numeric', 'parameters' => []],
190-
],
191-
'link' => [
192-
['name' => 'url', 'parameters' => []],
193-
],
194-
'date' => [
195-
['name' => 'date', 'parameters' => []],
196-
],
197-
'checkbox', 'toggle' => [
198-
['name' => 'boolean', 'parameters' => []],
199-
],
200-
'select', 'radio' => [
201-
['name' => 'in', 'parameters' => ['option1', 'option2', 'option3']],
202-
],
203-
'multi_select', 'checkbox_list', 'tags_input' => [
204-
['name' => 'array', 'parameters' => []],
205-
],
206-
default => [],
207-
};
208-
209173
return $this->state([
210174
'type' => $type,
211-
'validation_rules' => $defaultValidation,
175+
'validation_rules' => [],
212176
]);
213177
}
214178

@@ -219,7 +183,7 @@ public function required(): self
219183
{
220184
return $this->state(function (array $attributes) {
221185
$validationRules = $attributes['validation_rules'] ?? [];
222-
array_unshift($validationRules, ['name' => 'required', 'parameters' => []]);
186+
$validationRules['required'] = true;
223187

224188
return ['validation_rules' => $validationRules];
225189
});
@@ -234,11 +198,11 @@ public function withLength(?int $min = null, ?int $max = null): self
234198
$validationRules = $attributes['validation_rules'] ?? [];
235199

236200
if ($min !== null) {
237-
$validationRules[] = ['name' => 'min', 'parameters' => [$min]];
201+
$validationRules['min_length'] = $min;
238202
}
239203

240204
if ($max !== null) {
241-
$validationRules[] = ['name' => 'max', 'parameters' => [$max]];
205+
$validationRules['max_length'] = $max;
242206
}
243207

244208
return ['validation_rules' => $validationRules];

phpstan.neon

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ parameters:
1414
ignoreErrors:
1515
# Ignore unused trait warnings for library traits meant to be consumed by package users
1616
- identifier: trait.unused
17-
# Filament type stubs declare view() as expecting view-string|null
18-
- identifier: argument.type
19-
path: src/Filament/Integration/Components/Infolists/*
20-
- identifier: argument.type
21-
path: src/Filament/Integration/Components/Tables/Columns/*
17+
# Filament type stubs declare view() as expecting view-string|null (CI-only, version-dependent)
18+
-
19+
identifier: argument.type
20+
path: src/Filament/Integration/Components/Infolists/*
21+
reportUnmatched: false
22+
-
23+
identifier: argument.type
24+
path: src/Filament/Integration/Components/Tables/Columns/*
25+
reportUnmatched: false
2226
parallel:
2327
maximumNumberOfProcesses: 3

resources/lang/en/custom-fields.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
'unique_per_entity_type_help' => 'Each value can only be assigned to one record of this entity type.',
7272
'validation' => [
7373
'label' => 'Validation',
74+
'required' => 'Required',
7475
'rules' => 'Validation Rules',
7576
'rule' => 'Rule',
7677
'description' => 'Description',

src/Console/Commands/CleanupOrphanedValuesCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Database\Eloquent\Relations\Relation;
1010
use Illuminate\Support\Facades\DB;
11-
use Relaticle\CustomFields\Models\CustomFieldValue;
11+
use Relaticle\CustomFields\CustomFields;
1212

1313
final class CleanupOrphanedValuesCommand extends Command
1414
{
@@ -30,7 +30,7 @@ public function handle(): int
3030
$this->newLine();
3131
}
3232

33-
$table = (new CustomFieldValue)->getTable();
33+
$table = CustomFields::newValueModel()->getTable();
3434
$entityTypes = DB::table($table)
3535
->select('entity_type')
3636
->distinct()

0 commit comments

Comments
 (0)