Skip to content

Commit

Permalink
Merge pull request #2 from EcoOnline/ESR/EPC-11157-uuid-500
Browse files Browse the repository at this point in the history
EPC-11157: validate `Guid` values (and return a proper HTTP 400)
  • Loading branch information
yvo-niedrich authored Aug 7, 2024
2 parents 43b73e5 + f64d6de commit 940dc98
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Primitive.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public function __construct($value = null)
* @return Primitive
*/
abstract public function set($value);

public function allows($value): bool
{
return true;
}

/**
* Get the internal representation of the value
Expand Down
7 changes: 6 additions & 1 deletion src/PrimitiveType.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,9 @@ public function getOpenAPISchema(): array
{
return $this->instance()->getOpenAPISchema();
}
}

public function allowsValue($value): bool
{
return $this->instance()->allows($value);
}
}
13 changes: 12 additions & 1 deletion src/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,11 @@ public function isSearchable(): bool
*/
public function assertAllowsValue($value)
{
if (!$this->isNullable() && $value === null) {
if ($value === null) {
if ($this->isNullable()) {
return;
}

throw new BadRequestException(
'property_not_nullable',
sprintf("The property '%s' cannot be set to null", $this->getName())
Expand All @@ -383,6 +387,13 @@ public function assertAllowsValue($value)
sprintf("The value property '%s' exceeds the maximum length", $this->getName())
);
}

if (!$this->type->allowsValue($value)) {
throw new BadRequestException(
'property_value_invalid',
sprintf("The value property '%s' is not valid", $this->getName()),
);
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,9 @@ public function is(string $class): bool
{
return is_a($this->factory, $class, true);
}

public function allowsValue($value): bool
{
return true;
}
}
5 changes: 5 additions & 0 deletions src/Type/Guid.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ public function getOpenAPISchema(?Property $property = null): array
'pattern' => '^'.Lexer::guid.'$',
]);
}

public function allows($value): bool
{
return Lexer::patternCheck(Lexer::guid, (string) $value);
}
}

0 comments on commit 940dc98

Please sign in to comment.