Skip to content

Commit 9cfb3ca

Browse files
committed
array items fake to be required to ensure null-safe checks to not enforce each place implementing checks to handle array items specifically. Don't pollute the PropertyMetaDataCollection (which is now used to generate a cache key for properties) with this information as the items aren't really required.
1 parent 3ed4c4b commit 9cfb3ca

File tree

4 files changed

+5
-10
lines changed

4 files changed

+5
-10
lines changed

src/Model/Property/Property.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public function getDefaultValue(): ?string
288288
*/
289289
public function isRequired(): bool
290290
{
291-
return $this->isPropertyRequired;
291+
return $this->isPropertyRequired || str_starts_with($this->name, 'item of array ');
292292
}
293293

294294
/**

src/Model/Validator/ArrayItemValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(
4343
// an item of the array behaves like a nested property to add item-level validation
4444
$this->nestedProperty = (new PropertyFactory(new PropertyProcessorFactory()))
4545
->create(
46-
new PropertyMetaDataCollection([$nestedPropertyName]),
46+
new PropertyMetaDataCollection(),
4747
$schemaProcessor,
4848
$schema,
4949
$nestedPropertyName,

src/PropertyProcessor/Property/AbstractPropertyProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function generateValidators(PropertyInterface $property, JsonSchema $p
4848
$this->addDependencyValidator($property, $dependencies);
4949
}
5050

51-
if ($property->isRequired()) {
51+
if ($property->isRequired() && !str_starts_with($property->getName(), 'item of array ')) {
5252
$property->addValidator(new RequiredPropertyValidator($property), 1);
5353
}
5454

src/PropertyProcessor/Property/ArrayProcessor.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,21 +234,16 @@ private function addContainsValidation(PropertyInterface $property, JsonSchema $
234234
return;
235235
}
236236

237-
$name = "item of array {$property->getName()}";
238237
// an item of the array behaves like a nested property to add item-level validation
239238
$nestedProperty = (new PropertyFactory(new PropertyProcessorFactory()))
240239
->create(
241-
new PropertyMetaDataCollection([$name]),
240+
new PropertyMetaDataCollection(),
242241
$this->schemaProcessor,
243242
$this->schema,
244-
$name,
243+
"item of array {$property->getName()}",
245244
$propertySchema->withJson($propertySchema->getJson()[self::JSON_FIELD_CONTAINS]),
246245
);
247246

248-
$nestedProperty->filterValidators(static fn(Validator $validator): bool =>
249-
!is_a($validator->getValidator(), RequiredPropertyValidator::class)
250-
);
251-
252247
$property->addValidator(
253248
new PropertyTemplateValidator(
254249
$property,

0 commit comments

Comments
 (0)