|
10 | 10 | namespace JsonSchema\Constraints;
|
11 | 11 |
|
12 | 12 | use JsonSchema\Exception\InvalidArgumentException;
|
| 13 | +use UnexpectedValueException as StandardUnexpectedValueException; |
13 | 14 |
|
14 | 15 | /**
|
15 | 16 | * The Type Constraints, validates an element against a given type
|
|
19 | 20 | */
|
20 | 21 | class Type extends Constraint
|
21 | 22 | {
|
| 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 | + |
22 | 38 | /**
|
23 | 39 | * {@inheritDoc}
|
24 | 40 | */
|
@@ -56,7 +72,15 @@ public function check($value = null, $schema = null, $path = null, $i = null)
|
56 | 72 | }
|
57 | 73 |
|
58 | 74 | 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"); |
60 | 84 | }
|
61 | 85 | }
|
62 | 86 |
|
|
0 commit comments