Skip to content

Commit 41bc8e5

Browse files
authored
Merge pull request #59 from betsyalegria/revert-rm-arrayconstraint
Revert changes that removed ArrayConstraint coercion
2 parents 79622b6 + 61cd586 commit 41bc8e5

6 files changed

Lines changed: 21 additions & 17 deletions

File tree

.github/workflows/build-and-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
# Run tests on all OS's and HHVM versions, even if one fails
99
fail-fast: false
1010
matrix:
11-
os: [ubuntu]
11+
os: [ ubuntu ]
1212
hhvm:
1313
- "4.128"
1414
runs-on: ${{matrix.os}}-latest

src/Constraints/ArrayConstraint.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
namespace Slack\Hack\JsonSchema\Constraints;
44

55
use namespace HH\Lib\Str;
6-
use namespace Facebook\{TypeAssert, TypeSpec};
6+
use type Facebook\TypeAssert\{TypeCoercionException};
7+
use namespace Facebook\TypeSpec;
78
use namespace Slack\Hack\JsonSchema;
89

910
class ArrayConstraint {
@@ -25,8 +26,9 @@ public static function check(mixed $input, string $pointer, bool $coerce): vec<m
2526

2627
$spec = TypeSpec\vec(TypeSpec\mixed());
2728
try {
28-
return $spec->assertType($input);
29-
} catch (TypeAssert\IncorrectTypeException $e) {
29+
# To allow for either PHP or hack arrays, we coerce to a vec here.
30+
return $spec->coerceType($input);
31+
} catch (TypeCoercionException $e) {
3032
$error = shape(
3133
'code' => JsonSchema\FieldErrorCode::INVALID_TYPE,
3234
'message' => 'must provide an array',

tests/ArraySchemaValidatorTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class ArraySchemaValidatorTest extends BaseCodegenTestCase {
1818
}
1919

2020
public function testArrayOfStringsInvalidRoot(): void {
21-
$validator = new ArraySchemaValidator(vec['test', 'list', 'of', 'strings']);
21+
$validator = new ArraySchemaValidator(varray['test', 'list', 'of', 'strings']);
2222
$validator->validate();
2323

2424
expect($validator->isValid())->toBeFalse();
@@ -36,26 +36,28 @@ public function testArrayOfStringsValid(): void {
3636
}
3737

3838
public function testArrayOfStringsLegacyArrays(): void {
39-
$validator = new ArraySchemaValidator(darray['array_of_strings' => vec['test', 'list', 'of', 'strings']]);
39+
$validator = new ArraySchemaValidator(darray['array_of_strings' => varray['test', 'list', 'of', 'strings']]);
4040
$validator->validate();
4141

4242
expect($validator->isValid())->toBeTrue();
4343
$validated = $validator->getValidatedInput();
4444
expect(C\count($validated['array_of_strings'] ?? vec[]))->toBeSame(4);
4545
}
4646

47-
public function testDictOfStringsInvalid(): void {
47+
public function testArrayOfStringsLegacyAndHackArrays(): void {
4848
$validator = new ArraySchemaValidator(dict[
49-
'array_of_strings' => dict['test' => 'test'],
49+
'array_of_strings' => varray['test', 'list', 'of', 'strings'],
5050
]);
5151
$validator->validate();
5252

53-
expect($validator->isValid())->toBeFalse();
53+
expect($validator->isValid())->toBeTrue();
54+
$validated = $validator->getValidatedInput();
55+
expect(C\count($validated['array_of_strings'] ?? vec[]))->toBeSame(4);
5456
}
5557

5658
public function testUntypedArrayValid(): void {
5759
$validator = new ArraySchemaValidator(dict[
58-
'untyped_array' => vec['test', 'values'],
60+
'untyped_array' => varray['test', 'values'],
5961
]);
6062
$validator->validate();
6163

tests/DiscardAddititionalPropertiesValidatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function testAdditionalProperitesRef(): void {
6161
$input = dict[
6262
'additional_properties_ref' => dict[
6363
'something' => dict[
64-
'something-else' => vec['array', 'of', 'strings'],
64+
'something-else' => varray['array', 'of', 'strings'],
6565
],
6666
],
6767
];
@@ -73,7 +73,7 @@ public function testAdditionalProperitesRef(): void {
7373
$input = dict[
7474
'additional_properties_ref' => dict[
7575
'something' => dict[
76-
'something-else' => vec[34, 'of', 'strings'],
76+
'something-else' => varray[34, 'of', 'strings'],
7777
],
7878
],
7979
];

tests/ObjectSchemaValidatorTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ public function testNoAdditionalProperties(): void {
486486
public function testAdditionalProperitesArray(): void {
487487
$input = dict[
488488
'additional_properties_array' => dict[
489-
'something' => vec['array', 'of', 'strings'],
489+
'something' => varray['array', 'of', 'strings'],
490490
],
491491
];
492492

@@ -496,7 +496,7 @@ public function testAdditionalProperitesArray(): void {
496496

497497
$input = dict[
498498
'additional_properties_array' => dict[
499-
'something' => vec[34, 'of', 'strings'],
499+
'something' => varray[34, 'of', 'strings'],
500500
],
501501
];
502502

@@ -509,7 +509,7 @@ public function testAdditionalProperitesRef(): void {
509509
$input = dict[
510510
'additional_properties_ref' => dict[
511511
'something' => dict[
512-
'something-else' => vec['array', 'of', 'strings'],
512+
'something-else' => varray['array', 'of', 'strings'],
513513
],
514514
],
515515
];
@@ -521,7 +521,7 @@ public function testAdditionalProperitesRef(): void {
521521
$input = dict[
522522
'additional_properties_ref' => dict[
523523
'something' => dict[
524-
'something-else' => vec[34, 'of', 'strings'],
524+
'something-else' => varray[34, 'of', 'strings'],
525525
],
526526
],
527527
];

tests/RefSchemaValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function testUniqueRefs(): void {
6464
),
6565
shape(
6666
'input' => darray[
67-
'single-item-array-ref' => vec[
67+
'single-item-array-ref' => varray[
6868
darray['string' => 'test', 'integer' => 5],
6969
darray['string' => 'test2', 'integer' => 10],
7070
],

0 commit comments

Comments
 (0)