Skip to content

Commit

Permalink
Grav 1.7 edition: Fixed validation: strict not working in blueprints [
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Feb 18, 2020
1 parent 1d6a474 commit 028bbf0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

1. [](#new)
* Added `Session::regenerateId()` method to properly prevent session fixation issues
1. [](#bugfix)
* Fixed `validation: strict` not working in blueprints [#1273](https://github.com/getgrav/grav/issues/1273)

# v1.7.0-rc.6
## 02/11/2020
Expand Down
11 changes: 6 additions & 5 deletions system/src/Grav/Common/Data/BlueprintSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public function getType($name)
public function validate(array $data)
{
try {
$messages = $this->validateArray($data, $this->nested, $this->items['']['form'] ?? []);
$validation = $this->items['']['form']['validation'] ?? 'loose';
$messages = $this->validateArray($data, $this->nested, $validation === 'strict');
} catch (\RuntimeException $e) {
throw (new ValidationException($e->getMessage(), $e->getCode(), $e))->setMessages();
}
Expand Down Expand Up @@ -131,11 +132,11 @@ protected function flattenArray(array $data, array $rules, string $prefix)
/**
* @param array $data
* @param array $rules
* @param array $parent
* @param bool $strict
* @return array
* @throws \RuntimeException
*/
protected function validateArray(array $data, array $rules, array $parent)
protected function validateArray(array $data, array $rules, bool $strict)
{
$messages = $this->checkRequired($data, $rules);

Expand All @@ -153,8 +154,8 @@ protected function validateArray(array $data, array $rules, array $parent)
$messages += Validation::validate($child, $rule);
} elseif (\is_array($child) && \is_array($val)) {
// Array has been defined in blueprints.
$messages += $this->validateArray($child, $val, $rule ?? []);
} elseif (isset($parent['validation']) && $parent['validation'] === 'strict') {
$messages += $this->validateArray($child, $val, $strict);
} elseif ($strict) {
// Undefined/extra item.
throw new \RuntimeException(sprintf('%s is not defined in blueprints', $key));
}
Expand Down
1 change: 0 additions & 1 deletion tests/unit/Grav/Common/Data/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public function testValidateStrictExtra()
$blueprint = $this->loadBlueprint('strict');

$blueprint->validate(['test' => 'string', 'wrong' => 'field']);
die();
}

/**
Expand Down
16 changes: 11 additions & 5 deletions tests/unit/data/blueprints/strict.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ form:
validation: strict

fields:
test:
type: text
label: Test
validate:
required: true
tabs:
type: tabs
fields:
tab:
type: tab
fields:
test:
type: text
label: Test
validate:
required: true

0 comments on commit 028bbf0

Please sign in to comment.