Skip to content

Commit 7d946d3

Browse files
author
Michael Hahn
authored
Merge pull request #39 from ajmacd/revert-35-ajm-form-encoded-non-string-arrays
Revert "Support form-encoded uncoerced non-string arrays."
2 parents 95415cc + 965472a commit 7d946d3

File tree

4 files changed

+3
-103
lines changed

4 files changed

+3
-103
lines changed

src/Constraints/ArrayConstraint.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Slack\Hack\JsonSchema\Constraints;
44

5+
use namespace HH\Lib\Str;
56
use type Facebook\TypeAssert\{TypeCoercionException};
67
use namespace Facebook\TypeSpec;
78
use namespace Slack\Hack\JsonSchema;
@@ -21,9 +22,7 @@ public static function check(
2122
// check to see if we can create an array from a comma delimited string.
2223
$valid_json = $coerced is nonnull && $coerced is Traversable<_>;
2324
if (!$valid_json) {
24-
// Wrap in brackets and try JSON decoding again. This'll result in
25-
// correctly typed output for non-string form encoded arrays.
26-
$coerced = JsonSchema\json_decode_hack("[{$input}]");
25+
$coerced = Str\split($input, ',');
2726
}
2827

2928
$input = $coerced;

tests/ArraySchemaValidatorTest.php

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -129,43 +129,4 @@ public function testCoerceArrayURLEncodedStringSingleValue(): void {
129129
expect($validated)->toBeSame(shape('coerce_array' => $input));
130130
}
131131

132-
public function testCoerceArrayOfUncoercedIntegersValidString(): void {
133-
$input = vec[1, 2, 3, 4];
134-
135-
$validator = new ArraySchemaValidator(dict[
136-
'coerce_array_of_uncoerced_integers' => \json_encode($input),
137-
]);
138-
$validator->validate();
139-
140-
expect($validator->isValid())->toBeTrue();
141-
$validated = $validator->getValidatedInput();
142-
143-
expect($validated)->toBeSame(shape('coerce_array_of_uncoerced_integers' => $input));
144-
}
145-
146-
public function testCoerceArrayOfUncoercedIntegersInvalidString(): void {
147-
$input = vec['1', '2', '3', '4'];
148-
149-
$validator = new ArraySchemaValidator(dict[
150-
'coerce_array_of_uncoerced_integers' => \json_encode($input),
151-
]);
152-
$validator->validate();
153-
154-
expect($validator->isValid())->toBeFalse();
155-
}
156-
157-
public function testCoerceArrayOfUncoercedIntegersURLEncodedString(): void {
158-
$input = vec[1, 2, 3, 4];
159-
160-
$validator = new ArraySchemaValidator(dict[
161-
'coerce_array_of_uncoerced_integers' => Str\join($input, ','),
162-
]);
163-
$validator->validate();
164-
165-
expect($validator->isValid())->toBeTrue();
166-
$validated = $validator->getValidatedInput();
167-
168-
expect($validated)->toBeSame(shape('coerce_array_of_uncoerced_integers' => $input));
169-
}
170-
171132
}

tests/examples/array-schema.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@
1717
"type": "number",
1818
"coerce": true
1919
}
20-
},
21-
"coerce_array_of_uncoerced_integers": {
22-
"type": "array",
23-
"coerce": true,
24-
"items": {
25-
"type": "integer"
26-
}
2720
}
2821
}
2922
}

tests/examples/codegen/ArraySchemaValidator.php

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* To re-generate this file run `make test`
66
*
77
*
8-
* @generated SignedSource<<df6e79084f583f4c3b613739bf647027>>
8+
* @generated SignedSource<<14484ee6f8f738f12aee00444bbc398d>>
99
*/
1010
namespace Slack\Hack\JsonSchema\Tests\Generated;
1111
use namespace Slack\Hack\JsonSchema;
@@ -15,7 +15,6 @@
1515
?'array_of_strings' => vec<string>,
1616
?'untyped_array' => vec<mixed>,
1717
?'coerce_array' => vec<num>,
18-
?'coerce_array_of_uncoerced_integers' => vec<int>,
1918
...
2019
);
2120

@@ -110,47 +109,6 @@ public static function check(mixed $input, string $pointer): vec<num> {
110109
}
111110
}
112111

113-
final class ArraySchemaValidatorPropertiesCoerceArrayOfUncoercedIntegersItems {
114-
115-
private static bool $coerce = false;
116-
117-
public static function check(mixed $input, string $pointer): int {
118-
$typed =
119-
Constraints\IntegerConstraint::check($input, $pointer, self::$coerce);
120-
121-
return $typed;
122-
}
123-
}
124-
125-
final class ArraySchemaValidatorPropertiesCoerceArrayOfUncoercedIntegers {
126-
127-
private static bool $coerce = true;
128-
129-
public static function check(mixed $input, string $pointer): vec<int> {
130-
$typed = Constraints\ArrayConstraint::check($input, $pointer, self::$coerce);
131-
132-
$output = vec[];
133-
$errors = vec[];
134-
135-
foreach ($typed as $index => $value) {
136-
try {
137-
$output[] = ArraySchemaValidatorPropertiesCoerceArrayOfUncoercedIntegersItems::check(
138-
$value,
139-
JsonSchema\get_pointer($pointer, (string) $index),
140-
);
141-
} catch (JsonSchema\InvalidFieldException $e) {
142-
$errors = \HH\Lib\Vec\concat($errors, $e->errors);
143-
}
144-
}
145-
146-
if (\HH\Lib\C\count($errors)) {
147-
throw new JsonSchema\InvalidFieldException($pointer, $errors);
148-
}
149-
150-
return $output;
151-
}
152-
}
153-
154112
final class ArraySchemaValidator
155113
extends JsonSchema\BaseValidator<TArraySchemaValidator> {
156114

@@ -204,17 +162,6 @@ public static function check(
204162
}
205163
}
206164

207-
if (\HH\Lib\C\contains_key($typed, 'coerce_array_of_uncoerced_integers')) {
208-
try {
209-
$output['coerce_array_of_uncoerced_integers'] = ArraySchemaValidatorPropertiesCoerceArrayOfUncoercedIntegers::check(
210-
$typed['coerce_array_of_uncoerced_integers'],
211-
JsonSchema\get_pointer($pointer, 'coerce_array_of_uncoerced_integers'),
212-
);
213-
} catch (JsonSchema\InvalidFieldException $e) {
214-
$errors = \HH\Lib\Vec\concat($errors, $e->errors);
215-
}
216-
}
217-
218165
if (\HH\Lib\C\count($errors)) {
219166
throw new JsonSchema\InvalidFieldException($pointer, $errors);
220167
}

0 commit comments

Comments
 (0)