Skip to content

Commit

Permalink
Merge pull request #39 from ajmacd/revert-35-ajm-form-encoded-non-str…
Browse files Browse the repository at this point in the history
…ing-arrays

Revert "Support form-encoded uncoerced non-string arrays."
  • Loading branch information
Michael Hahn authored Sep 3, 2020
2 parents 95415cc + 965472a commit 7d946d3
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 103 deletions.
5 changes: 2 additions & 3 deletions src/Constraints/ArrayConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Slack\Hack\JsonSchema\Constraints;

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

$input = $coerced;
Expand Down
39 changes: 0 additions & 39 deletions tests/ArraySchemaValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,43 +129,4 @@ public function testCoerceArrayURLEncodedStringSingleValue(): void {
expect($validated)->toBeSame(shape('coerce_array' => $input));
}

public function testCoerceArrayOfUncoercedIntegersValidString(): void {
$input = vec[1, 2, 3, 4];

$validator = new ArraySchemaValidator(dict[
'coerce_array_of_uncoerced_integers' => \json_encode($input),
]);
$validator->validate();

expect($validator->isValid())->toBeTrue();
$validated = $validator->getValidatedInput();

expect($validated)->toBeSame(shape('coerce_array_of_uncoerced_integers' => $input));
}

public function testCoerceArrayOfUncoercedIntegersInvalidString(): void {
$input = vec['1', '2', '3', '4'];

$validator = new ArraySchemaValidator(dict[
'coerce_array_of_uncoerced_integers' => \json_encode($input),
]);
$validator->validate();

expect($validator->isValid())->toBeFalse();
}

public function testCoerceArrayOfUncoercedIntegersURLEncodedString(): void {
$input = vec[1, 2, 3, 4];

$validator = new ArraySchemaValidator(dict[
'coerce_array_of_uncoerced_integers' => Str\join($input, ','),
]);
$validator->validate();

expect($validator->isValid())->toBeTrue();
$validated = $validator->getValidatedInput();

expect($validated)->toBeSame(shape('coerce_array_of_uncoerced_integers' => $input));
}

}
7 changes: 0 additions & 7 deletions tests/examples/array-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@
"type": "number",
"coerce": true
}
},
"coerce_array_of_uncoerced_integers": {
"type": "array",
"coerce": true,
"items": {
"type": "integer"
}
}
}
}
55 changes: 1 addition & 54 deletions tests/examples/codegen/ArraySchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* To re-generate this file run `make test`
*
*
* @generated SignedSource<<df6e79084f583f4c3b613739bf647027>>
* @generated SignedSource<<14484ee6f8f738f12aee00444bbc398d>>
*/
namespace Slack\Hack\JsonSchema\Tests\Generated;
use namespace Slack\Hack\JsonSchema;
Expand All @@ -15,7 +15,6 @@
?'array_of_strings' => vec<string>,
?'untyped_array' => vec<mixed>,
?'coerce_array' => vec<num>,
?'coerce_array_of_uncoerced_integers' => vec<int>,
...
);

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

final class ArraySchemaValidatorPropertiesCoerceArrayOfUncoercedIntegersItems {

private static bool $coerce = false;

public static function check(mixed $input, string $pointer): int {
$typed =
Constraints\IntegerConstraint::check($input, $pointer, self::$coerce);

return $typed;
}
}

final class ArraySchemaValidatorPropertiesCoerceArrayOfUncoercedIntegers {

private static bool $coerce = true;

public static function check(mixed $input, string $pointer): vec<int> {
$typed = Constraints\ArrayConstraint::check($input, $pointer, self::$coerce);

$output = vec[];
$errors = vec[];

foreach ($typed as $index => $value) {
try {
$output[] = ArraySchemaValidatorPropertiesCoerceArrayOfUncoercedIntegersItems::check(
$value,
JsonSchema\get_pointer($pointer, (string) $index),
);
} catch (JsonSchema\InvalidFieldException $e) {
$errors = \HH\Lib\Vec\concat($errors, $e->errors);
}
}

if (\HH\Lib\C\count($errors)) {
throw new JsonSchema\InvalidFieldException($pointer, $errors);
}

return $output;
}
}

final class ArraySchemaValidator
extends JsonSchema\BaseValidator<TArraySchemaValidator> {

Expand Down Expand Up @@ -204,17 +162,6 @@ public static function check(
}
}

if (\HH\Lib\C\contains_key($typed, 'coerce_array_of_uncoerced_integers')) {
try {
$output['coerce_array_of_uncoerced_integers'] = ArraySchemaValidatorPropertiesCoerceArrayOfUncoercedIntegers::check(
$typed['coerce_array_of_uncoerced_integers'],
JsonSchema\get_pointer($pointer, 'coerce_array_of_uncoerced_integers'),
);
} catch (JsonSchema\InvalidFieldException $e) {
$errors = \HH\Lib\Vec\concat($errors, $e->errors);
}
}

if (\HH\Lib\C\count($errors)) {
throw new JsonSchema\InvalidFieldException($pointer, $errors);
}
Expand Down

0 comments on commit 7d946d3

Please sign in to comment.