Skip to content

Commit ce28423

Browse files
committed
Add support for GroupSequence
1 parent ec4e0cb commit ce28423

File tree

6 files changed

+307
-1
lines changed

6 files changed

+307
-1
lines changed

features/main/validation.feature

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Feature: Using validations groups
2+
As a client software developer
3+
I need to be able to use validation groups
4+
5+
@createSchema
6+
@dropSchema
7+
Scenario: Create a resource
8+
When I add "Content-Type" header equal to "application/ld+json"
9+
And I send a "POST" request to "/dummy_validation" with body:
10+
"""
11+
{
12+
"code": "My Dummy"
13+
}
14+
"""
15+
Then the response status code should be 201
16+
And the response should be in JSON
17+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
18+
19+
@createSchema
20+
@dropSchema
21+
Scenario: Create a resource with validation
22+
When I add "Content-Type" header equal to "application/ld+json"
23+
And I send a "POST" request to "/dummy_validation/validation_groups" with body:
24+
"""
25+
{
26+
"code": "My Dummy"
27+
}
28+
"""
29+
Then print last JSON response
30+
Then the response status code should be 400
31+
And the response should be in JSON
32+
And the JSON should be equal to:
33+
"""
34+
{
35+
"@context": "\/contexts\/ConstraintViolationList",
36+
"@type": "ConstraintViolationList",
37+
"hydra:title": "An error occurred",
38+
"hydra:description": "name: This value should not be null.",
39+
"violations": [
40+
{
41+
"propertyPath": "name",
42+
"message": "This value should not be null."
43+
}
44+
]
45+
}
46+
"""
47+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
48+
49+
@createSchema
50+
@dropSchema
51+
Scenario: Create a resource with validation group sequence
52+
When I add "Content-Type" header equal to "application/ld+json"
53+
And I send a "POST" request to "/dummy_validation/validation_sequence" with body:
54+
"""
55+
{
56+
"code": "My Dummy"
57+
}
58+
"""
59+
Then print last JSON response
60+
Then the response status code should be 400
61+
And the response should be in JSON
62+
And the JSON should be equal to:
63+
"""
64+
{
65+
"@context": "\/contexts\/ConstraintViolationList",
66+
"@type": "ConstraintViolationList",
67+
"hydra:title": "An error occurred",
68+
"hydra:description": "title: This value should not be null.",
69+
"violations": [
70+
{
71+
"propertyPath": "title",
72+
"message": "This value should not be null."
73+
}
74+
]
75+
}
76+
"""
77+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"

src/Bridge/Symfony/Validator/Validator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException;
1717
use ApiPlatform\Core\Validator\ValidatorInterface;
1818
use Psr\Container\ContainerInterface;
19+
use Symfony\Component\Validator\Constraints\GroupSequence;
1920
use Symfony\Component\Validator\Validator\ValidatorInterface as SymfonyValidatorInterface;
2021

2122
/**
@@ -52,7 +53,9 @@ public function validate($data, array $context = [])
5253
$validationGroups = $validationGroups($data);
5354
}
5455

55-
$validationGroups = (array) $validationGroups;
56+
if (!$validationGroups instanceof GroupSequence) {
57+
$validationGroups = (array) $validationGroups;
58+
}
5659
}
5760

5861
$violations = $this->validator->validate($data, null, $validationGroups);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Controller;
15+
16+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyValidation;
17+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
18+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
19+
20+
class DummyValidationController
21+
{
22+
/**
23+
* @Route(
24+
* name="post_validation_groups",
25+
* path="/dummy_validation/validation_groups",
26+
* defaults={"_api_resource_class"=DummyValidation::class, "_api_collection_operation_name"="post_validation_groups"}
27+
* )
28+
* @Method("POST")
29+
*
30+
* @param $data
31+
*
32+
* @return mixed
33+
*/
34+
public function postValidationGroups($data)
35+
{
36+
return $data;
37+
}
38+
39+
/**
40+
* @Route(
41+
* name="post_validation_sequence",
42+
* path="/dummy_validation/validation_sequence",
43+
* defaults={"_api_resource_class"=DummyValidation::class, "_api_collection_operation_name"="post_validation_sequence"}
44+
* )
45+
* @Method("POST")
46+
*
47+
* @param $data
48+
*
49+
* @return mixed
50+
*/
51+
public function postValidationSequence($data)
52+
{
53+
return $data;
54+
}
55+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Core\Annotation\ApiResource;
17+
use Doctrine\ORM\Mapping as ORM;
18+
use Symfony\Component\Validator\Constraints as Assert;
19+
20+
/**
21+
* @ORM\Entity
22+
* @ApiResource(
23+
* collectionOperations={
24+
* "get"={"method"="GET"},
25+
* "post"={"path"="dummy_validation.{_format}", "method"="POST"},
26+
* "post_validation_groups"={"route_name"="post_validation_groups", "validation_groups"={"a"}},
27+
* "post_validation_sequence"={"route_name"="post_validation_sequence", "validation_groups"="app.dummy_validation.group_generator"}
28+
* }
29+
* )
30+
*/
31+
class DummyValidation
32+
{
33+
/**
34+
* @var int The id
35+
*
36+
* @ORM\Column(type="integer")
37+
* @ORM\Id
38+
* @ORM\GeneratedValue(strategy="AUTO")
39+
*/
40+
private $id;
41+
42+
/**
43+
* @var string|null The dummy name
44+
*
45+
* @ORM\Column(nullable=true)
46+
* @Assert\NotNull(groups={"a"})
47+
*/
48+
private $name;
49+
50+
/**
51+
* @var string|null The dummy title
52+
*
53+
* @ORM\Column(nullable=true)
54+
* @Assert\NotNull(groups={"b"})
55+
*/
56+
private $title;
57+
58+
/**
59+
* @var string The dummy code
60+
* @ORM\Column
61+
*/
62+
private $code;
63+
64+
/**
65+
* @return int
66+
*/
67+
public function getId()
68+
{
69+
return $this->id;
70+
}
71+
72+
/**
73+
* @param int $id
74+
*
75+
* @return DummyValidation
76+
*/
77+
public function setId(int $id)
78+
{
79+
$this->id = $id;
80+
81+
return $this;
82+
}
83+
84+
/**
85+
* @return null|string
86+
*/
87+
public function getName()
88+
{
89+
return $this->name;
90+
}
91+
92+
/**
93+
* @param null|string $name
94+
*
95+
* @return DummyValidation
96+
*/
97+
public function setName($name)
98+
{
99+
$this->name = $name;
100+
101+
return $this;
102+
}
103+
104+
/**
105+
* @return null|string
106+
*/
107+
public function getTitle()
108+
{
109+
return $this->title;
110+
}
111+
112+
/**
113+
* @param null|string $title
114+
*
115+
* @return DummyValidation
116+
*/
117+
public function setTitle($title)
118+
{
119+
$this->title = $title;
120+
121+
return $this;
122+
}
123+
124+
/**
125+
* @return string
126+
*/
127+
public function getCode()
128+
{
129+
return $this->code;
130+
}
131+
132+
/**
133+
* @param string $code
134+
*
135+
* @return DummyValidation
136+
*/
137+
public function setCode($code)
138+
{
139+
$this->code = $code;
140+
141+
return $this;
142+
}
143+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Validator;
15+
16+
use Symfony\Component\Validator\Constraints\GroupSequence;
17+
18+
class DummyValidationGroupsGenerator
19+
{
20+
public function __invoke()
21+
{
22+
return new GroupSequence(['b', 'a']);
23+
}
24+
}

tests/Fixtures/app/config/config_test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,7 @@ services:
227227
test.annotation_reader:
228228
alias: annotation_reader
229229
public: true
230+
231+
app.dummy_validation.group_generator:
232+
class: ApiPlatform\Core\Tests\Fixtures\TestBundle\Validator\DummyValidationGroupsGenerator
233+
public: true

0 commit comments

Comments
 (0)