Skip to content

Commit

Permalink
Fix error if custom validator doesn't provides a message
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Mar 27, 2019
1 parent d133fba commit ac13596
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Develop
- Fix error if custom validator doesn't provides a message

## [4.2.1] - 2019-01-13
- Fix issue with ResourceLocator

Expand Down
6 changes: 5 additions & 1 deletion src/ServerSideValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,14 @@ private function ruleWithMessage($rule, $messageSet)
// Weird way to adapt with Valitron's funky interface
$params = array_merge([$rule], array_slice(func_get_args(), 2));
call_user_func_array([$this, 'rule'], $params);

// Set message. Use Valitron's default message if not specified in the schema.
if (!$messageSet) {
$messageSet = "'".$params[1]."' ".vsprintf(static::$_ruleMessages[$rule], array_slice(func_get_args(), 3));
$message = (isset(static::$_ruleMessages[$rule])) ? static::$_ruleMessages[$rule] : null;
$message = vsprintf($message, array_slice(func_get_args(), 3));
$messageSet = "'{$params[1]}' $message";
}

$this->message($messageSet);
}

Expand Down
36 changes: 32 additions & 4 deletions tests/ServerSideValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function testValidateArray()
{
// Arrange
$schema = new RequestSchemaRepository([
'array' => [
'screech' => [
'validators' => [
'array' => [
'message' => 'Array must be an array.',
Expand All @@ -91,18 +91,18 @@ public function testValidateArray()
$validator = new ServerSideValidator($schema, $this->translator);

$result = $validator->validate([
'array' => ['foo', 'bar'],
'screech' => ['foo', 'bar'],
]);

// Check that the correct Valitron rule was generated
$this->assertTrue($validator->hasRule('array', 'array'));
$this->assertTrue($validator->hasRule('array', 'screech'));

// Check passing validation
$this->assertTrue($result);

// Check failing validation
$this->assertFalse($validator->validate([
'array' => 'screeeech',
'screech' => 'screeeech',
]));
}

Expand Down Expand Up @@ -864,4 +864,32 @@ public function testDomainRulesServerOnly()
// Check passing validation
$this->assertFalse($result);
}

/**
* @depends testValidateUsername
*/
public function testValidateWithNoValidatorMessage()
{
// Arrange
$schema = new RequestSchemaRepository([
'user_name' => [
'validators' => [
'username' => [],
],
],
]);

// Act
$validator = new ServerSideValidator($schema, $this->translator);

$result = $validator->validate([
'user_name' => 'alex.weissman',
]);

// Check that the correct Valitron rule was generated
$this->assertTrue($validator->hasRule('username', 'user_name'));

// Check passing validation
$this->assertTrue($result);
}
}

0 comments on commit ac13596

Please sign in to comment.