Skip to content

Commit aaf4ea6

Browse files
committed
added recursion to validation, made structural validation a public method
1 parent bb5f8ee commit aaf4ea6

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

app/V1Module/presenters/base/BasePresenter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,20 +217,24 @@ private function processParams(ReflectionMethod $reflection)
217217
return;
218218
}
219219

220+
///TODO: handle nested MetaFormat creation
220221
$fieldNames = FormatCache::getFormatFieldNames($format);
221222
$formatInstance = MetaFormatHelper::createFormatInstance($format);
222223
foreach ($fieldNames as $field) {
223224
///TODO: check if required
224225
$value = $this->getPostField($field, false);
225-
$this->logger->log(var_export([$field, $value], true), ILogger::DEBUG);
226226
if (!$formatInstance->checkedAssign($field, $value)) {
227227
///TODO: it would be nice to give a more detailed error message here
228228
throw new InvalidArgumentException($field);
229229
}
230230
}
231231

232+
// validate structural constraints
233+
if (!$formatInstance->validateStructure()) {
234+
throw new BadRequestException("All request fields are valid but additional structural constraints failed.");
235+
}
236+
232237
$this->requestFormatInstance = $formatInstance;
233-
$this->logger->log(var_export($formatInstance, true), ILogger::DEBUG);
234238

235239
// $this->logger->log(var_export($annotations, true), ILogger::DEBUG);
236240
// $this->logger->log(var_export($requiredFields, true), ILogger::DEBUG);

app/helpers/MetaFormats/MetaFormat.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function checkedAssign(string $fieldName, mixed $value)
5050
public function validate()
5151
{
5252
// check whether all higher level contracts hold
53-
if (!$this->validateSelf()) {
53+
if (!$this->validateStructure()) {
5454
return false;
5555
}
5656

@@ -60,19 +60,23 @@ public function validate()
6060
if (!$this->checkIfAssignable($fieldName, $this->$fieldName)) {
6161
return false;
6262
}
63+
64+
// check nested formats recursively
65+
if ($this->$fieldName instanceof MetaFormat && !$this->$fieldName->validate()) {
66+
return false;
67+
}
6368
}
6469

6570
return true;
6671
}
6772

6873
/**
69-
* Validates this format. Automatically called by the validate method on all fields.
70-
* Primitive formats should always override this, composite formats might want to override
71-
* this in case more complex contracts need to be enforced.
74+
* Validates this format. Automatically called by the validate method on all suitable fields.
75+
* Formats might want to override this in case more complex contracts need to be enforced.
7276
* This method should not check the format of nested types.
7377
* @return bool Returns whether the format is valid.
7478
*/
75-
protected function validateSelf()
79+
public function validateStructure()
7680
{
7781
// there are no constraints by default
7882
return true;

0 commit comments

Comments
 (0)