Skip to content

Commit

Permalink
added recursion to validation, made structural validation a public me…
Browse files Browse the repository at this point in the history
…thod
  • Loading branch information
eceltov committed Dec 17, 2024
1 parent bb5f8ee commit aaf4ea6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
8 changes: 6 additions & 2 deletions app/V1Module/presenters/base/BasePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,24 @@ private function processParams(ReflectionMethod $reflection)
return;
}

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

// validate structural constraints
if (!$formatInstance->validateStructure()) {
throw new BadRequestException("All request fields are valid but additional structural constraints failed.");
}

$this->requestFormatInstance = $formatInstance;
$this->logger->log(var_export($formatInstance, true), ILogger::DEBUG);

// $this->logger->log(var_export($annotations, true), ILogger::DEBUG);
// $this->logger->log(var_export($requiredFields, true), ILogger::DEBUG);
Expand Down
14 changes: 9 additions & 5 deletions app/helpers/MetaFormats/MetaFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function checkedAssign(string $fieldName, mixed $value)
public function validate()
{
// check whether all higher level contracts hold
if (!$this->validateSelf()) {
if (!$this->validateStructure()) {
return false;
}

Expand All @@ -60,19 +60,23 @@ public function validate()
if (!$this->checkIfAssignable($fieldName, $this->$fieldName)) {
return false;
}

// check nested formats recursively
if ($this->$fieldName instanceof MetaFormat && !$this->$fieldName->validate()) {
return false;
}
}

return true;
}

/**
* Validates this format. Automatically called by the validate method on all fields.
* Primitive formats should always override this, composite formats might want to override
* this in case more complex contracts need to be enforced.
* Validates this format. Automatically called by the validate method on all suitable fields.
* Formats might want to override this in case more complex contracts need to be enforced.
* This method should not check the format of nested types.
* @return bool Returns whether the format is valid.
*/
protected function validateSelf()
public function validateStructure()
{
// there are no constraints by default
return true;
Expand Down

0 comments on commit aaf4ea6

Please sign in to comment.