Skip to content

Commit 11ca654

Browse files
committed
chore: Ignore serialization properties via context
1 parent 58f1617 commit 11ca654

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/Validator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,12 @@ protected function getAliasName(ReflectionProperty|ReflectionParameter $reflecti
245245
*/
246246
protected function isToValidate(ReflectionProperty|ReflectionParameter $reflection): bool
247247
{
248+
$useSerialization = $this->context->getOptional('internal.options.ignore.useSerialization', false);
248249
$allAttributes = $reflection->getAttributes(Options\Ignore::class);
249250
foreach ($allAttributes as $attribute) {
250251
$instance = $attribute->newInstance();
251252

252-
return ! $instance->ignoreValidation();
253+
return $useSerialization ? ! $instance->ignoreSerialization() : ! $instance->ignoreValidation();
253254
}
254255

255256
return true;

tests/Integration/OptionsTest.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Attributes\Options as Options;
1616
use Attributes\Options\Exceptions\InvalidOptionException;
1717
use Attributes\Options\Ignore;
18+
use Attributes\Validation\Context;
1819
use Attributes\Validation\Tests\Models as Models;
1920
use Attributes\Validation\Validator;
2021

@@ -130,8 +131,9 @@
130131
$validator = new Validator(strict: $isStrict);
131132
$rawData = [
132133
'value' => 'my value',
133-
'ignore' => 'ignored',
134-
'ignoreValidation' => 'ignored',
134+
'ignore' => 'both',
135+
'ignoreValidation' => 'validation',
136+
'ignoreSerialization' => 'serialization',
135137
];
136138
$model = $validator->validate($rawData, new class
137139
{
@@ -142,11 +144,47 @@
142144

143145
#[Ignore(serialization: false)]
144146
public string $ignoreValidation = 'original';
147+
148+
#[Ignore(validation: false)]
149+
public string $ignoreSerialization = 'original';
150+
});
151+
expect($model)
152+
->toHaveProperty('value', 'my value')
153+
->toHaveProperty('ignore', 'original')
154+
->toHaveProperty('ignoreValidation', 'original')
155+
->toHaveProperty('ignoreSerialization', 'serialization');
156+
})
157+
->with([true, false])
158+
->group('validator', 'options', 'ignore');
159+
160+
test('Ignore as a serializer', function (bool $isStrict) {
161+
$context = new Context;
162+
$context->set('internal.options.ignore.useSerialization', true);
163+
$validator = new Validator(strict: $isStrict, context: $context);
164+
$rawData = [
165+
'value' => 'my value',
166+
'ignore' => 'both',
167+
'ignoreValidation' => 'validation',
168+
'ignoreSerialization' => 'serialization',
169+
];
170+
$model = $validator->validate($rawData, new class
171+
{
172+
public string $value;
173+
174+
#[Ignore]
175+
public string $ignore = 'original';
176+
177+
#[Ignore(serialization: false)]
178+
public string $ignoreValidation = 'original';
179+
180+
#[Ignore(validation: false)]
181+
public string $ignoreSerialization = 'original';
145182
});
146183
expect($model)
147184
->toHaveProperty('value', 'my value')
148185
->toHaveProperty('ignore', 'original')
149-
->toHaveProperty('ignoreValidation', 'original');
186+
->toHaveProperty('ignoreValidation', 'validation')
187+
->toHaveProperty('ignoreSerialization', 'original');
150188
})
151189
->with([true, false])
152190
->group('validator', 'options', 'ignore');

0 commit comments

Comments
 (0)