Skip to content

Commit

Permalink
fix(laravel): validate enum schema within filter
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Sep 16, 2024
1 parent a49bde1 commit 755e342
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ private function addSchemaValidation(Parameter $parameter): Parameter
$assertions[] = 'multiple_of:'.$schema['multipleOf'];
}

// if (isset($schema['enum'])) {
// $assertions[] = [Rule::enum($schema['enum'])];
// }
if (isset($schema['enum'])) {
$assertions[] = Rule::in($schema['enum']);
}

if (isset($schema['type']) && 'array' === $schema['type']) {
$assertions[] = 'array';
Expand Down
14 changes: 9 additions & 5 deletions src/Laravel/State/ParameterValidatorProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,17 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
$value = null;
}

foreach ((array) $constraints as $k => $c) {
if (!\is_string($k)) {
$k = $key;
}
if (str_contains($key, ':property')) {
$k = str_replace('[:property]', '', $key);
$allConstraints[$k] = [];

$allConstraints[$k] = $c;
foreach ($parameter->getExtraProperties()['_properties'] ?? [] as $property) {
$allConstraints[$k][$property] = $constraints;
}
continue;
}

$allConstraints[$key] = $constraints;
}

$validator = Validator::make($request->query->all(), $allConstraints);
Expand Down
7 changes: 7 additions & 0 deletions src/Laravel/Tests/EloquentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,11 @@ public function testRangeGreaterThanEqualFilter(): void
$this->assertSame($response->json()['member'][1]['@id'], $bookAfter['@id']);
$this->assertSame($response->json()['totalItems'], 2);
}

public function testWrongOrderFilter(): void
{
$res = $this->get('/api/authors?order[name]=something', ['Accept' => ['application/ld+json']]);
$this->assertEquals($res->getStatusCode(), 422);
}

}
14 changes: 4 additions & 10 deletions src/Metadata/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class Parameter
* @param array<string, mixed> $extraProperties
* @param ParameterProviderInterface|callable|string|null $provider
* @param FilterInterface|string|null $filter
* @param Constraint|array<string, string>|string|Constraint[]|null $constraints
* @param mixed $constraints an array of Symfony constraints, or an array of Laravel rules
*/
public function __construct(
protected ?string $key = null,
Expand All @@ -42,7 +42,7 @@ public function __construct(
protected ?bool $required = null,
protected ?int $priority = null,
protected ?false $hydra = null,
protected Constraint|array|string|null $constraints = null,
protected mixed $constraints = null,
protected string|\Stringable|null $security = null,
protected ?string $securityMessage = null,
protected ?array $extraProperties = [],
Expand Down Expand Up @@ -106,10 +106,7 @@ public function getHydra(): ?bool
return $this->hydra;
}

/**
* @return Constraint|string|array<string, string>|Constraint[]|null
*/
public function getConstraints(): Constraint|string|array|null
public function getConstraints(): mixed
{
return $this->constraints;
}
Expand Down Expand Up @@ -239,10 +236,7 @@ public function withHydra(false $hydra): static
return $self;
}

/**
* @param string|array<string, string>|Constraint[]|Constraint $constraints
*/
public function withConstraints(string|array|Constraint $constraints): static
public function withConstraints(mixed $constraints): static
{
$self = clone $this;
$self->constraints = $constraints;
Expand Down

0 comments on commit 755e342

Please sign in to comment.