Skip to content

Commit b854caa

Browse files
committed
add support for minItems when applying defaults
1 parent 9eae2ae commit b854caa

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/JsonSchema/Constraints/UndefinedConstraint.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,15 @@ protected function applyDefaultValues(&$value, $schema, $path)
252252
$this->appliedDefaults[] = $currentProperty;
253253
}
254254
}
255-
} elseif (
256-
isset($schema->items) &&
257-
LooseTypeCheck::isArray($schema->items) &&
258-
LooseTypeCheck::isArray($value)
259-
) {
255+
} elseif (isset($schema->items) && LooseTypeCheck::isArray($value)) {
256+
$items = array();
257+
if (LooseTypeCheck::isArray($schema->items)) {
258+
$items = $schema->items;
259+
} elseif (isset($schema->minItems) && count($value) < $schema->minItems) {
260+
$items = array_fill(count($value), $schema->minItems - 1, $schema->items);
261+
}
260262
// $value is an array, and items are defined - treat as plain array
261-
foreach ($schema->items as $currentItem => $itemDefinition) {
263+
foreach ($items as $currentItem => $itemDefinition) {
262264
if (
263265
!array_key_exists($currentItem, $value)
264266
&& property_exists($itemDefinition, 'default')

tests/Constraints/DefaultPropertiesTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ public function getValidTests()
159159
'{"items":{"properties":{"propertyOne":{"default":"valueOne"}}}}',
160160
'[]'
161161
),
162+
array(// #23 if items is a schema with a default value and minItems is present, fill the array
163+
'["a"]',
164+
'{"items":{"default":"b"}, "minItems": 3}',
165+
'["a","b","b"]'
166+
),
162167
);
163168
}
164169

0 commit comments

Comments
 (0)