Skip to content

Commit 1320479

Browse files
committed
Relationship validator callback patches, fixes #10
1 parent 0a2d7c8 commit 1320479

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

src/Validator/Relationships/HasManyValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ protected function validate($value)
257257
$this->validateIdentifier($identifier, $key);
258258
}
259259

260-
if ($this->hasCallback()) {
260+
if ($this->getErrors()->isEmpty() && $this->hasCallback()) {
261261
$this->validateCallback($data);
262262
}
263263
}

src/Validator/Relationships/HasOneValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ protected function validate($value)
265265
}
266266

267267
// check the callback, if one exists.
268-
if ($this->hasCallback() && false == call_user_func($this->getCallback(), $data)) {
268+
if ($this->getErrors()->isEmpty() && $this->hasCallback() && false == call_user_func($this->getCallback(), $data)) {
269269
$this->error(static::ERROR_NOT_FOUND, '/' . Relationship::DATA);
270270
}
271271
}

test/Validator/Relationships/HasManyValidatorTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,18 @@ public function testCallbackInvalidIndexes()
195195
$this->assertEquals('/data/1', $error->source()->getPointer());
196196
}
197197

198+
/**
199+
* @depends testCallback
200+
*/
201+
public function testCallbackNotInvokedIfCollectionIsInvalid()
202+
{
203+
$this->validator->setTypes(static::TYPE_A);
204+
205+
$this->validator->setCallback(function () {
206+
$this->fail('Not expecting callback to be invoked.');
207+
});
208+
209+
$this->assertFalse($this->validator->isValid($this->input));
210+
}
211+
198212
}

test/Validator/Relationships/BelongsToValidatorTest.php renamed to test/Validator/Relationships/HasOneValidatorTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use CloudCreativity\JsonApi\Object\ResourceIdentifier\ResourceIdentifier;
2323
use CloudCreativity\JsonApi\Validator\ValidatorTestCase;
2424

25-
class BelongsToValidatorTest extends ValidatorTestCase
25+
class HasOneValidatorTest extends ValidatorTestCase
2626
{
2727

2828
const TYPE = 'foo';
@@ -177,4 +177,19 @@ public function testCallbackInvalid()
177177
$this->assertEquals(404, $error->getStatus());
178178
$this->assertEquals('/data', $error->source()->getPointer());
179179
}
180+
181+
/**
182+
* @depends testCallback
183+
*/
184+
public function testCallbackNotInvokedForInvalidIdentifier()
185+
{
186+
$callback = function () {
187+
$this->fail('Not expecting callback to be invoked.');
188+
};
189+
190+
$this->valid->{Relationship::DATA}->{ResourceIdentifier::TYPE} = static::INVALID_TYPE;
191+
192+
$this->validator->setCallback($callback);
193+
$this->assertFalse($this->validator->isValid($this->valid));
194+
}
180195
}

0 commit comments

Comments
 (0)