Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1666] RFC: add support for optional custom error message for Configuration Validation Errors #2

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions Definition/BaseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ public function getInfo()
return $this->getAttribute('info');
}

/**
* Sets an error message.
*
* @param string $errorMessage
*/
public function setErrorMessage($errorMessage)
{
$this->setAttribute('errorMessage', $errorMessage);
}

/**
* Returns error message.
*
* @return string The error message
*/
public function getErrorMessage()
{
return $this->getAttribute('errorMessage');
}

/**
* Sets the example configuration for this node.
*
Expand Down
11 changes: 11 additions & 0 deletions Definition/Builder/NodeDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ public function info($info)
return $this->attribute('info', $info);
}

/**
* Sets error message.
*
* @param string $errorMessage The error message text
* @return NodeDefinition
*/
public function errorMessage($errorMessage)
{
return $this->attribute('errorMessage', $errorMessage);
}

/**
* Sets example configuration.
*
Expand Down
10 changes: 8 additions & 2 deletions Definition/ScalarNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ class ScalarNode extends VariableNode
protected function validateType($value)
{
if (!is_scalar($value) && null !== $value) {
$ex = new InvalidTypeException(sprintf(
$errorMessage = $this->getErrorMessage();
$finalMessage = sprintf(
'Invalid type for path "%s". Expected scalar, but got %s.',
$this->getPath(),
gettype($value)
));
);
if ($errorMessage != null) {
$finalMessage .= sprintf("\nError message: %s.", $errorMessage);
}

$ex = new InvalidTypeException($finalMessage);
$ex->setPath($this->getPath());

throw $ex;
Expand Down
10 changes: 8 additions & 2 deletions Definition/VariableNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,17 @@ protected function validateType($value)
protected function finalizeValue($value)
{
if (!$this->allowEmptyValue && empty($value)) {
$ex = new InvalidConfigurationException(sprintf(
$errorMessage = $this->getErrorMessage();
$finalMessage = sprintf(
'The path "%s" cannot contain an empty value, but got %s.',
$this->getPath(),
json_encode($value)
));
);
if ($errorMessage != null) {
$finalMessage .= sprintf("\nError message: %s.", $errorMessage);
}

$ex = new InvalidConfigurationException($finalMessage);
$ex->setPath($this->getPath());

throw $ex;
Expand Down
17 changes: 17 additions & 0 deletions Tests/Definition/Builder/TreeBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,21 @@ public function testDefinitionExampleGetsTransferedToNode()
$this->assertTrue(is_array($tree->getExample()));
$this->assertEquals('example', $children['child']->getExample());
}

public function testDefinitionErrorMessageGetsTransferedToNode()
{
$builder = new TreeBuilder();

$builder->root('test')->errorMessage('You are missing the root node')
->children()
->node('child', 'variable')->errorMessage('child error message')->defaultValue('default')
->end()
->end();

$tree = $builder->buildTree();
$children = $tree->getChildren();

$this->assertEquals('You are missing the root node', $tree->getErrorMessage());
$this->assertEquals('child error message', $children['child']->getErrorMessage());
}
}
38 changes: 38 additions & 0 deletions Tests/Definition/ScalarNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,42 @@ public function getInvalidValues()
array(new \stdClass()),
);
}

public function testNormalizeThrowsExceptionWithoutErrorMessage()
{
$value = array(array('foo' => 'bar'));
$node = new ScalarNode('test');

$expectedMessage = sprintf(
'Invalid type for path "%s". Expected scalar, but got %s.',
$node->getPath(),
gettype($value)
);

$this->setExpectedException(
'Symfony\Component\Config\Definition\Exception\InvalidTypeException', $expectedMessage
);

$node->normalize($value);
}

public function testNormalizeThrowsExceptionWithErrorMessage()
{
$value = array(array('foo' => 'bar'));
$node = new ScalarNode('test');
$node->setErrorMessage('This is a custom error message');

$expectedMessage = sprintf(
'Invalid type for path "%s". Expected scalar, but got %s.'.
"\nError message: ".$node->getErrorMessage(),
$node->getPath(),
gettype($value)
);

$this->setExpectedException(
'Symfony\Component\Config\Definition\Exception\InvalidTypeException', $expectedMessage
);

$node->normalize($value);
}
}