Skip to content

Commit fe2cfcf

Browse files
committed
Various PR cleanup stuff
* Fix whitespace * Turn $early into $extraFlags * Change "string" to "ABC" in string test * Update README.md description of CHECK_MODE_EARLY_COERCE
1 parent 628b32a commit fe2cfcf

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ Please note that using `CHECK_MODE_COERCE_TYPES` or `CHECK_MODE_APPLY_DEFAULTS`
195195
original data.
196196

197197
`CHECK_MODE_EARLY_COERCE` has no effect unless used in combination with `CHECK_MODE_COERCE_TYPES`. If
198-
enabled, type coercion will occur as soon as possible, even if the value may already be of another valid
199-
type.
198+
enabled, the validator will use (and coerce) the first compatible type it encounters, even if the
199+
schema defines another type that matches directly and does not require coercion.
200200

201201
## Running the tests
202202

tests/Constraints/CoerciveTest.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function dataCoerceCases()
3131
// toType
3232
'string' => array(
3333
// fromType fromValue toValue valid Test Number
34-
array('string', '"string"', 'string', true), // #0
34+
array('string', '"ABC"', 'ABC', true), // #0
3535
array('integer', '45', '45', true), // #1
3636
array('boolean', 'true', 'true', true), // #2
3737
array('boolean', 'false', 'false', true), // #3
@@ -55,7 +55,7 @@ public function dataCoerceCases()
5555
array('boolean', 'true', true, true), // #17
5656
array('NULL', 'null', false, true), // #18
5757
array('array', '["true"]', true, true), // #19
58-
array('object', '{"a":"b"}', null, false), // #20
58+
array('object', '{"a":"b"}', null, false), // #20
5959
array('string', '""', null, false), // #21
6060
array('string', '"ABC"', null, false), // #22
6161
array('integer', '2', null, false), // #23
@@ -140,7 +140,7 @@ public function dataCoerceCases()
140140
$tests[] = array(
141141
'{"properties":{"propertyOne":{"type":["object", "number", "string"]}}}',
142142
'{"propertyOne":"42"}',
143-
'string', 'integer', 42, true, true
143+
'string', 'integer', 42, true, Constraint::CHECK_MODE_EARLY_COERCE
144144
);
145145

146146
// #45 check multiple types (none valid)
@@ -154,11 +154,8 @@ public function dataCoerceCases()
154154
}
155155

156156
/** @dataProvider dataCoerceCases **/
157-
public function testCoerceCases($schema, $data, $startType, $endType, $endValue, $valid, $early = false, $assoc = false)
157+
public function testCoerceCases($schema, $data, $startType, $endType, $endValue, $valid, $extraFlags = 0, $assoc = false)
158158
{
159-
if ($early) {
160-
$this->factory->addConfig(Constraint::CHECK_MODE_EARLY_COERCE);
161-
}
162159
$validator = new Validator($this->factory);
163160

164161
$schema = json_decode($schema);
@@ -171,7 +168,7 @@ public function testCoerceCases($schema, $data, $startType, $endType, $endValue,
171168
}
172169
$this->assertEquals($startType, $type, "Incorrect type '$type': expected '$startType'");
173170

174-
$validator->validate($data, $schema);
171+
$validator->validate($data, $schema, $this->factory->getConfig() | $extraFlags);
175172

176173
// check validity
177174
if ($valid) {
@@ -199,8 +196,6 @@ public function testCoerceCases($schema, $data, $startType, $endType, $endValue,
199196
$this->assertFalse($validator->isValid(), 'Validation succeeded, but should have failed');
200197
$this->assertEquals(1, count($validator->getErrors()));
201198
}
202-
203-
$this->factory->removeConfig(Constraint::CHECK_MODE_EARLY_COERCE);
204199
}
205200

206201
/** @dataProvider dataCoerceCases **/

0 commit comments

Comments
 (0)