Skip to content

Commit 069fec2

Browse files
committed
Fixes #104: Constraint::check() uses wrong indefinite article in ...
...error message to express the type constraint Type::check() used a wrong indefinte article on occasion. This has been fixed by providing a map of types and their wording so that the correct indefinite article is used, always. - #104 - composer/composer#1616
1 parent 3bbbc37 commit 069fec2

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/JsonSchema/Constraints/Type.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace JsonSchema\Constraints;
1111

1212
use JsonSchema\Exception\InvalidArgumentException;
13+
use UnexpectedValueException as StandardUnexpectedValueException;
1314

1415
/**
1516
* The Type Constraints, validates an element against a given type
@@ -19,6 +20,21 @@
1920
*/
2021
class Type extends Constraint
2122
{
23+
/**
24+
* @var array|string[] type wordings for validation error messages
25+
*/
26+
static $wording = array(
27+
'integer' => 'an integer',
28+
'number' => 'a number',
29+
'boolean' => 'a boolean',
30+
'object' => 'an object',
31+
'array' => 'an array',
32+
'string' => 'a string',
33+
'null' => 'a null',
34+
'any' => NULL, // validation of 'any' is always true so is not needed in message wording
35+
0 => NULL, // validation of a false-y value is always true, so not needed as well
36+
);
37+
2238
/**
2339
* {@inheritDoc}
2440
*/
@@ -56,7 +72,15 @@ public function check($value = null, $schema = null, $path = null, $i = null)
5672
}
5773

5874
if ($isValid === false) {
59-
$this->addError($path, gettype($value) . " value found, but a " . $type . " is required");
75+
if (!isset(self::$wording[$type])) {
76+
throw new StandardUnexpectedValueException(
77+
sprintf(
78+
"No wording for %s available, expected wordings are: [%s]",
79+
var_export($type, true),
80+
implode(', ', array_filter(self::$wording)))
81+
);
82+
}
83+
$this->addError($path, gettype($value) . " value found, but " . self::$wording[$type] . " is required");
6084
}
6185
}
6286

0 commit comments

Comments
 (0)