diff --git a/src/Model/Property/Property.php b/src/Model/Property/Property.php index d5be007..dd37c1d 100644 --- a/src/Model/Property/Property.php +++ b/src/Model/Property/Property.php @@ -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 '); } /** diff --git a/src/Model/Validator/ArrayItemValidator.php b/src/Model/Validator/ArrayItemValidator.php index 001b7d0..dfc5f0b 100644 --- a/src/Model/Validator/ArrayItemValidator.php +++ b/src/Model/Validator/ArrayItemValidator.php @@ -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, diff --git a/src/PropertyProcessor/Property/AbstractPropertyProcessor.php b/src/PropertyProcessor/Property/AbstractPropertyProcessor.php index 908e512..73f0a0a 100644 --- a/src/PropertyProcessor/Property/AbstractPropertyProcessor.php +++ b/src/PropertyProcessor/Property/AbstractPropertyProcessor.php @@ -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); } diff --git a/src/PropertyProcessor/Property/ArrayProcessor.php b/src/PropertyProcessor/Property/ArrayProcessor.php index 709352b..b83cc19 100644 --- a/src/PropertyProcessor/Property/ArrayProcessor.php +++ b/src/PropertyProcessor/Property/ArrayProcessor.php @@ -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,