Skip to content

Commit fdea4c4

Browse files
authored
Merge pull request #94 from Krunch/master
Fix tolerateStrings to validate types properly
2 parents 0a5dd03 + 1e3e6dd commit fdea4c4

File tree

3 files changed

+176
-2
lines changed

3 files changed

+176
-2
lines changed

src/Constraint/Type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static function readString($types, &$data)
4040
break;
4141
case self::BOOLEAN:
4242
$newData = filter_var($data, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
43-
$ok = is_bool($data);
43+
$ok = is_bool($newData);
4444
break;
4545
case self::NULL:
4646
break;

src/Schema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public function out($data, Context $options = null)
204204
* @throws InvalidValue
205205
* @throws \Exception
206206
*/
207-
private function processType($data, Context $options, $path = '#')
207+
private function processType(&$data, Context $options, $path = '#')
208208
{
209209
if ($options->tolerateStrings && is_string($data)) {
210210
$valid = Type::readString($this->type, $data);
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
3+
namespace Swaggest\JsonSchema\Tests\PHPUnit;
4+
5+
use Swaggest\JsonSchema\Context;
6+
use Swaggest\JsonSchema\InvalidValue;
7+
use Swaggest\JsonSchema\Schema;
8+
9+
class TolerateStringsTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function testWithTolerateStrings()
12+
{
13+
$json_schema = json_decode(
14+
json_encode(
15+
[
16+
'items' => [
17+
['type' => 'string'],
18+
['type' => 'integer'],
19+
['type' => 'number'],
20+
['type' => 'boolean'],
21+
[
22+
'type' => 'integer',
23+
'enum' => [1, 2, 3]
24+
]
25+
]
26+
]
27+
)
28+
);
29+
30+
$data = [
31+
'text',
32+
'10',
33+
'123.45',
34+
'true',
35+
'2'
36+
];
37+
38+
$options = new Context();
39+
$options->tolerateStrings = true;
40+
41+
$schema = Schema::import($json_schema);
42+
43+
$this->assertSame($data, $schema->in($data, $options));
44+
}
45+
46+
public function testWithTolerateStringsBadNumber()
47+
{
48+
$json_schema = json_decode(
49+
json_encode(
50+
[
51+
'items' => [
52+
['type' => 'string'],
53+
['type' => 'integer'],
54+
['type' => 'number'],
55+
['type' => 'boolean']
56+
]
57+
]
58+
)
59+
);
60+
61+
$data = [
62+
'text',
63+
'10',
64+
'bad',
65+
'true'
66+
];
67+
68+
$options = new Context();
69+
$options->tolerateStrings = true;
70+
71+
$schema = Schema::import($json_schema);
72+
73+
$this->setExpectedException(get_class(new InvalidValue()), 'Number expected, "bad" received at #->items:2');
74+
$schema->in($data, $options);
75+
}
76+
77+
public function testWithTolerateStringsBadInteger()
78+
{
79+
$json_schema = json_decode(
80+
json_encode(
81+
[
82+
'items' => [
83+
['type' => 'string'],
84+
['type' => 'integer'],
85+
['type' => 'number'],
86+
['type' => 'boolean']
87+
]
88+
]
89+
)
90+
);
91+
92+
$data = [
93+
'text',
94+
'bad',
95+
'123.45',
96+
'true'
97+
];
98+
99+
$options = new Context();
100+
$options->tolerateStrings = true;
101+
102+
$schema = Schema::import($json_schema);
103+
104+
$this->setExpectedException(get_class(new InvalidValue()), 'Integer expected, "bad" received at #->items:1');
105+
$schema->in($data, $options);
106+
}
107+
108+
public function testWithTolerateStringsBadBoolean()
109+
{
110+
$json_schema = json_decode(
111+
json_encode(
112+
[
113+
'items' => [
114+
['type' => 'string'],
115+
['type' => 'integer'],
116+
['type' => 'number'],
117+
['type' => 'boolean']
118+
]
119+
]
120+
)
121+
);
122+
123+
$data = [
124+
'text',
125+
'10',
126+
'123.45',
127+
'bad'
128+
];
129+
130+
$options = new Context();
131+
$options->tolerateStrings = true;
132+
133+
$schema = Schema::import($json_schema);
134+
135+
$this->setExpectedException(get_class(new InvalidValue()), 'Boolean expected, "bad" received at #->items:3');
136+
$schema->in($data, $options);
137+
}
138+
139+
public function testWithTolerateStringsBadEnum()
140+
{
141+
$json_schema = json_decode(
142+
json_encode(
143+
[
144+
'items' => [
145+
['type' => 'string'],
146+
['type' => 'integer'],
147+
['type' => 'number'],
148+
['type' => 'boolean'],
149+
[
150+
'type' => 'integer',
151+
'enum' => [1, 2, 3]
152+
]
153+
]
154+
]
155+
)
156+
);
157+
158+
$data = [
159+
'text',
160+
'10',
161+
'123.45',
162+
'true',
163+
'5'
164+
];
165+
166+
$options = new Context();
167+
$options->tolerateStrings = true;
168+
169+
$schema = Schema::import($json_schema);
170+
171+
$this->setExpectedException(get_class(new InvalidValue()), 'Enum failed, enum: [1,2,3], data: 5 at #->items:4');
172+
$schema->in($data, $options);
173+
}
174+
}

0 commit comments

Comments
 (0)