Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle type not being a string value like we expect #34

Merged
merged 2 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Codegen/Constraints/UntypedBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,10 @@ private function generateOptimizedAnyOfChecks(TOptimizedAnyOfTypes $any_of_types
)
->ensureEmptyLine()
->addMultilineCall('Constraints\ObjectRequiredConstraint::check', vec['$typed', 'keyset[$key]', '$pointer'])
->addAssignment('$type_name', '($typed[$key] ?? null) as nonnull', HackBuilderValues::literal())
->addAssignment('$field_pointer', 'JsonSchema\get_pointer($pointer, $key)', HackBuilderValues::literal())
->addAssignment(
'$type_name',
'Constraints\StringConstraint::check($type_name, $field_pointer, false)',
'Constraints\StringConstraint::check($typed[$key] ?? null, $field_pointer, false)',
HackBuilderValues::literal(),
)
->ensureEmptyLine()
Expand Down
19 changes: 19 additions & 0 deletions tests/UntypedSchemaValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,23 @@ public function testAnyOfOptimizedEnumInvalid(): void {
expect($validator->isValid())->toBeFalse();
}

public function testAnyOfOptimizedEnumInvalidType(): void {
$input = dict[
'any_of_optimized_enum' => dict[
'type' => null,
'integer' => 3,
],
];

$validator = new UntypedSchemaValidator($input);
$validator->validate();
expect($validator->isValid())->toBeFalse();
$errors = $validator->getErrors();
expect(C\count($errors))->toBeSame(1);
$error = $errors[0];
expect($error['code'])->toBeSame('invalid_type');
expect($error['message'])->toBeSame('must provide a string');
expect($error['pointer'] ?? null)->toBeSame('/any_of_optimized_enum/type');
}

}
5 changes: 2 additions & 3 deletions tests/examples/codegen/CustomCodegenConfigValidator.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<<359265246c578753e092aaac23c09040>>
* @generated SignedSource<<b091dd7814125f747984f31d8fafd43c>>
*/
namespace Slack\Hack\JsonSchema\Tests\Generated;
use namespace Slack\Hack\JsonSchema;
Expand Down Expand Up @@ -649,10 +649,9 @@ public static function check(
$typed = Constraints\ObjectConstraint::check($input, $pointer, false);

Constraints\ObjectRequiredConstraint::check($typed, keyset[$key], $pointer);
$type_name = ($typed[$key] ?? null) as nonnull;
$field_pointer = JsonSchema\get_pointer($pointer, $key);
$type_name =
Constraints\StringConstraint::check($type_name, $field_pointer, false);
Constraints\StringConstraint::check($typed[$key] ?? null, $field_pointer, false);

$types = dict[
'phone' =>
Expand Down
5 changes: 2 additions & 3 deletions tests/examples/codegen/PersonSchemaValidator.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<<386e40d994e41438a6cf7c899b5da3c1>>
* @generated SignedSource<<2ab2503832dc94e7486ad2834e8e40a5>>
*/
namespace Slack\Hack\JsonSchema\Tests\Generated;
use namespace Slack\Hack\JsonSchema;
Expand Down Expand Up @@ -649,10 +649,9 @@ public static function check(
$typed = Constraints\ObjectConstraint::check($input, $pointer, false);

Constraints\ObjectRequiredConstraint::check($typed, keyset[$key], $pointer);
$type_name = ($typed[$key] ?? null) as nonnull;
$field_pointer = JsonSchema\get_pointer($pointer, $key);
$type_name =
Constraints\StringConstraint::check($type_name, $field_pointer, false);
Constraints\StringConstraint::check($typed[$key] ?? null, $field_pointer, false);

$types = dict[
'phone' =>
Expand Down
5 changes: 2 additions & 3 deletions tests/examples/codegen/UntypedSchemaValidator.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<<3c498202f74829743c3f3d148c18ac0b>>
* @generated SignedSource<<0f9dda249bf4bf5215072b9e288a0fa5>>
*/
namespace Slack\Hack\JsonSchema\Tests\Generated;
use namespace Slack\Hack\JsonSchema;
Expand Down Expand Up @@ -965,10 +965,9 @@ public static function check(
$typed = Constraints\ObjectConstraint::check($input, $pointer, false);

Constraints\ObjectRequiredConstraint::check($typed, keyset[$key], $pointer);
$type_name = ($typed[$key] ?? null) as nonnull;
$field_pointer = JsonSchema\get_pointer($pointer, $key);
$type_name =
Constraints\StringConstraint::check($type_name, $field_pointer, false);
Constraints\StringConstraint::check($typed[$key] ?? null, $field_pointer, false);

$types = dict[
'first' =>
Expand Down