Skip to content

Commit 46a0311

Browse files
authored
Merge pull request #535 from shmax/issue-533
return original value when no cast
2 parents 0ccb470 + a754bd3 commit 46a0311

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed

src/JsonSchema/Constraints/TypeConstraint.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ protected function toString($value)
337337
if ($this->getTypeCheck()->isArray($value) && count($value) === 1) {
338338
return $this->toString(reset($value));
339339
}
340+
341+
return $value;
340342
}
341343

342344
/**

tests/Constraints/CoerciveTest.php

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,55 +39,49 @@ public function dataCoerceCases()
3939
array('array', '[45]', '45', true), // #5
4040
array('object', '{"a":"b"}', null, false), // #6
4141
array('array', '[{"a":"b"}]', null, false), // #7
42+
array('array', '[1,2]', array(1, 2), false), // #8
4243
),
4344
'integer' => array(
44-
array('string', '"45"', 45, true), // #8
45-
array('integer', '45', 45, true), // #9
46-
array('boolean', 'true', 1, true), // #10
47-
array('boolean', 'false', 0, true), // #11
48-
array('NULL', 'null', 0, true), // #12
49-
array('array', '["-45"]', -45, true), // #13
50-
array('object', '{"a":"b"}', null, false), // #14
51-
array('array', '["ABC"]', null, false), // #15
45+
array('string', '"45"', 45, true), // #9
46+
array('integer', '45', 45, true), // #10
47+
array('boolean', 'true', 1, true), // #11
48+
array('boolean', 'false', 0, true), // #12
49+
array('NULL', 'null', 0, true), // #13
50+
array('array', '["-45"]', -45, true), // #14
51+
array('object', '{"a":"b"}', null, false), // #15
52+
array('array', '["ABC"]', null, false), // #16
5253
),
5354
'boolean' => array(
54-
array('string', '"true"', true, true), // #16
55-
array('integer', '1', true, true), // #17
56-
array('boolean', 'true', true, true), // #18
57-
array('NULL', 'null', false, true), // #19
58-
array('array', '["true"]', true, true), // #20
59-
array('object', '{"a":"b"}', null, false), // #21
60-
array('string', '""', null, false), // #22
61-
array('string', '"ABC"', null, false), // #23
62-
array('integer', '2', null, false), // #24
55+
array('string', '"true"', true, true), // #17
56+
array('integer', '1', true, true), // #18
57+
array('boolean', 'true', true, true), // #19
58+
array('NULL', 'null', false, true), // #20
59+
array('array', '["true"]', true, true), // #21
60+
array('object', '{"a":"b"}', null, false), // #22
61+
array('string', '""', null, false), // #23
62+
array('string', '"ABC"', null, false), // #24
63+
array('integer', '2', null, false), // #25
6364
),
6465
'NULL' => array(
65-
array('string', '""', null, true), // #25
66-
array('integer', '0', null, true), // #26
67-
array('boolean', 'false', null, true), // #27
68-
array('NULL', 'null', null, true), // #28
69-
array('array', '[0]', null, true), // #29
70-
array('object', '{"a":"b"}', null, false), // #30
71-
array('string', '"null"', null, false), // #31
72-
array('integer', '-1', null, false), // #32
66+
array('string', '""', null, true), // #26
67+
array('integer', '0', null, true), // #27
68+
array('boolean', 'false', null, true), // #28
69+
array('NULL', 'null', null, true), // #29
70+
array('array', '[0]', null, true), // #30
71+
array('object', '{"a":"b"}', null, false), // #31
72+
array('string', '"null"', null, false), // #32
73+
array('integer', '-1', null, false), // #33
7374
),
7475
'array' => array(
75-
array('string', '"ABC"', array('ABC'), true), // #33
76-
array('integer', '45', array(45), true), // #34
77-
array('boolean', 'true', array(true), true), // #35
78-
array('NULL', 'null', array(null), true), // #36
79-
array('array', '["ABC"]', array('ABC'), true), // #37
80-
array('object', '{"a":"b"}', null, false), // #38
76+
array('string', '"ABC"', array('ABC'), true), // #34
77+
array('integer', '45', array(45), true), // #35
78+
array('boolean', 'true', array(true), true), // #36
79+
array('NULL', 'null', array(null), true), // #37
80+
array('array', '["ABC"]', array('ABC'), true), // #38
81+
array('object', '{"a":"b"}', null, false), // #39
8182
),
8283
);
8384

84-
// #39 check post-coercion validation (to array)
85-
$tests[] = array(
86-
'{"properties":{"propertyOne":{"type":"array","items":[{"type":"number"}]}}}',
87-
'{"propertyOne":"ABC"}',
88-
'string', null, null, false
89-
);
90-
9185
// #40 check multiple types (first valid)
9286
$tests[] = array(
9387
'{"properties":{"propertyOne":{"type":["number", "string"]}}}',
@@ -172,6 +166,13 @@ public function dataCoerceCases()
172166
'string', 'boolean', false, true
173167
);
174168

169+
// #52 check post-coercion validation (to array)
170+
$tests[] = array(
171+
'{"properties":{"propertyOne":{"type":"array","items":[{"type":"number"}]}}}',
172+
'{"propertyOne":"ABC"}',
173+
'string', null, null, false
174+
);
175+
175176
foreach ($types as $toType => $testCases) {
176177
foreach ($testCases as $testCase) {
177178
$tests[] = array(

0 commit comments

Comments
 (0)