Skip to content

Commit

Permalink
Change validation object and set default values
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrands02 committed Nov 21, 2024
1 parent a3aa660 commit 76bba42
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/Service/ValidationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,31 @@ public function validatePublication(array $publication): array
$validator = new Validator();
$validator->setMaxErrors(100);

if(empty($publicationType->getProperties()) === true) {
if (empty($publicationType->getProperties()) === true) {
return $publication;
}


// Check for default values, and only set the property if the property is empty
foreach ($publicationType->getProperties() as $property) {
if (isset($property['default']) === true && empty($property['default']) === false && (isset($publication['data'][$property['title']]) === false || empty($publication['data'][$property['title']]) === true)) {
$publication['data'][$property['title']] = $property['default'];
}
}

$result = $validator->validate(data: (object) json_decode(json_encode($publication['data'])), schema: $publicationType->getSchema($this->urlGenerator));

$publication['validation'] = [];
$publication['validation'] = [
'errors' => [],
'valid' => true
];

if ($result->hasError()) {
$errors = (new ErrorFormatter())->format($result->error());
$publication['validation'] = $errors;
foreach ($errors as $error) {
$publication['validation']['errors'][] = $error[0];
}
$publication['validation']['valid'] = false;
}

return $publication;
Expand Down

0 comments on commit 76bba42

Please sign in to comment.