Skip to content

Commit

Permalink
add support for minItems when applying defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
mathroc committed Mar 24, 2017
1 parent 9eae2ae commit ea6bf9f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/JsonSchema/Constraints/UndefinedConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,16 @@ protected function applyDefaultValues(&$value, $schema, $path)
}
} elseif (
isset($schema->items) &&
LooseTypeCheck::isArray($schema->items) &&
LooseTypeCheck::isArray($value)
) {
$items = [];
if (LooseTypeCheck::isArray($schema->items)) {
$items = $schema->items;
} elseif (isset($schema->minItems) && count($value) < $schema->minItems) {
$items = array_fill(count($value), $schema->minItems - 1, $schema->items);
}
// $value is an array, and items are defined - treat as plain array
foreach ($schema->items as $currentItem => $itemDefinition) {
foreach ($items as $currentItem => $itemDefinition) {
if (
!array_key_exists($currentItem, $value)
&& property_exists($itemDefinition, 'default')
Expand Down
5 changes: 5 additions & 0 deletions tests/Constraints/DefaultPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ public function getValidTests()
'{"items":{"properties":{"propertyOne":{"default":"valueOne"}}}}',
'[]'
),
array(// #23 if items is a schema with a default value and minItems is present, fill the array
'["a"]',
'{"items":{"default":"b"}, "minItems": 3}',
'["a","b","b"]'
),
);
}

Expand Down

0 comments on commit ea6bf9f

Please sign in to comment.