Skip to content

Commit 351c96b

Browse files
committed
Fixed can't generate correct error message for custom callable type
1 parent b88fdf9 commit 351c96b

2 files changed

Lines changed: 48 additions & 20 deletions

File tree

src/Validator.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,27 @@ protected function getBuiltInTypeValidators()
3838

3939
$validators = [];
4040
foreach ($types as $type => $func) {
41-
$validators[$type] = function ($value) use ($func, $type) {
42-
if ($func($value)) {
43-
return true;
44-
}
41+
$validators[$type] = $this->createValidator($type, $func);
42+
}
4543

46-
$givenType = $this->getType($value);
44+
return $validators;
45+
}
46+
47+
protected function createValidator($type, callable $callable)
48+
{
49+
return function ($value) use ($callable, $type) {
50+
if ($callable($value)) {
51+
return true;
52+
}
4753

48-
$path = $this->getNormalizedPath();
54+
$givenType = $this->getType($value);
4955

50-
$this->addError($path, "The path of '$path' requires to be a $type, $givenType is given");
56+
$path = $this->getNormalizedPath();
5157

52-
return false;
53-
};
54-
}
58+
$this->addError($path, "The path of '$path' requires to be a $type, $givenType is given");
5559

56-
return $validators;
60+
return false;
61+
};
5762
}
5863

5964
protected function isNumber($data)
@@ -96,7 +101,7 @@ public function addType(string $type, $definition)
96101
throw new \InvalidArgumentException("The type: $type is already exists");
97102
}
98103

99-
$this->types[$type] = $this->buildTypeValidator($definition);
104+
$this->types[$type] = $this->buildTypeValidator($type, $definition);
100105
}
101106

102107
protected $errors = [];
@@ -139,10 +144,10 @@ public function matches($data, $type)
139144
return $this->matchInternal($data, $type);
140145
}
141146

142-
protected function buildTypeValidator($definition)
147+
protected function buildTypeValidator($type, $definition)
143148
{
144149
if (is_callable($definition)) {
145-
$validator = $definition;
150+
$validator = $this->createValidator($type, $definition);
146151
} else if ($this->isObject($definition)) {
147152
$validator = function ($data) use ($definition) {
148153
return $this->matchObject($data, $definition);

tests/JsonTypeTest.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ class JsonTypeTest extends PHPUnit_Framework_TestCase
1414
{
1515
protected function createValidator()
1616
{
17-
return new Validator();
17+
$validator = new Validator();
18+
19+
$validator->addType('timestamp', $this->timestampValidator());
20+
21+
return $validator;
1822
}
1923

2024
public function basicTypes()
@@ -90,19 +94,23 @@ public function testMatchArrayOfCustomType()
9094
$this->assertTrue($json->matches([$this->userData()], ['user']));
9195
}
9296

93-
public function testAddCustomTypeThroughCallable()
97+
protected function timestampValidator()
9498
{
95-
$json = $this->createValidator();
96-
97-
$json->addType('timestamp', function ($value) {
99+
return function ($value) {
98100
if ((!is_string($value) && !is_numeric($value)) || strtotime($value) === false) {
99101
return false;
100102
}
101103

102104
$date = date_parse($value);
103105

104106
return checkdate($date['month'], $date['day'], $date['year']);
105-
});
107+
};
108+
}
109+
110+
public function testAddCustomTypeThroughCallable()
111+
{
112+
$json = $this->createValidator();
113+
106114

107115
$this->assertTrue($json->matches('2017-01-01 00:00:00', 'timestamp'));
108116
$this->assertFalse($json->matches('2017-01-91 00:00:00', 'timestamp'));
@@ -166,6 +174,21 @@ public function typeErrorMessages()
166174
'$.foo[0]',
167175
"The path of '$.foo[0]' requires to be a string, integer is given",
168176
],
177+
178+
// Custom Types
179+
[
180+
'foo',
181+
'timestamp',
182+
'$',
183+
"The path of '$' requires to be a timestamp, string is given",
184+
],
185+
[
186+
['foo' => 'bar'],
187+
['foo' => 'timestamp'],
188+
'$.foo',
189+
"The path of '$.foo' requires to be a timestamp, string is given",
190+
],
191+
169192
];
170193
}
171194

0 commit comments

Comments
 (0)