Skip to content

Commit

Permalink
array items fake to be required to ensure null-safe checks to not enf…
Browse files Browse the repository at this point in the history
…orce 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.
  • Loading branch information
wol-soft committed Nov 18, 2024
1 parent 3ed4c4b commit 9cfb3ca
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Model/Property/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public function getDefaultValue(): ?string
*/
public function isRequired(): bool
{
return $this->isPropertyRequired;
return $this->isPropertyRequired || str_starts_with($this->name, 'item of array ');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Validator/ArrayItemValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(
// an item of the array behaves like a nested property to add item-level validation
$this->nestedProperty = (new PropertyFactory(new PropertyProcessorFactory()))
->create(
new PropertyMetaDataCollection([$nestedPropertyName]),
new PropertyMetaDataCollection(),
$schemaProcessor,
$schema,
$nestedPropertyName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function generateValidators(PropertyInterface $property, JsonSchema $p
$this->addDependencyValidator($property, $dependencies);
}

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

Expand Down
9 changes: 2 additions & 7 deletions src/PropertyProcessor/Property/ArrayProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,21 +234,16 @@ private function addContainsValidation(PropertyInterface $property, JsonSchema $
return;
}

$name = "item of array {$property->getName()}";
// an item of the array behaves like a nested property to add item-level validation
$nestedProperty = (new PropertyFactory(new PropertyProcessorFactory()))
->create(
new PropertyMetaDataCollection([$name]),
new PropertyMetaDataCollection(),
$this->schemaProcessor,
$this->schema,
$name,
"item of array {$property->getName()}",
$propertySchema->withJson($propertySchema->getJson()[self::JSON_FIELD_CONTAINS]),
);

$nestedProperty->filterValidators(static fn(Validator $validator): bool =>
!is_a($validator->getValidator(), RequiredPropertyValidator::class)
);

$property->addValidator(
new PropertyTemplateValidator(
$property,
Expand Down

0 comments on commit 9cfb3ca

Please sign in to comment.