Skip to content

Commit

Permalink
use discardAdditionalProperties: true so we actually test the right t…
Browse files Browse the repository at this point in the history
…hing
  • Loading branch information
ianhoffman committed Mar 9, 2023
1 parent d1209ae commit 8b894cd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 41 deletions.
16 changes: 4 additions & 12 deletions tests/ObjectSchemaValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -698,23 +698,15 @@ public function testEmptyClosedShape(): void {
]);
$validator->validate();
expect($validator->isValid())->toBeTrue();
expect($validator->getValidatedInput())->toEqual(shape());

// Additional properties are discarded
$validator = new ObjectSchemaValidator(dict[
'empty_closed_shape' => dict['foo' => 'bar']
]);
$validator->validate();
expect($validator->isValid())->toBeFalse();

$errors = $validator->getErrors();
expect(C\count($errors))->toEqual(1);

$error = C\firstx($errors);
expect($error['code'])->toEqual(FieldErrorCode::FAILED_CONSTRAINT);
expect($error['message'])->toEqual('invalid additional property: foo');

$constraint = Shapes::at($error, 'constraint');
expect($constraint['type'])->toEqual(FieldErrorConstraint::ADDITIONAL_PROPERTIES);
expect($constraint['got'] ?? null)->toEqual('foo');
expect($validator->isValid())->toBeTrue();
expect($validator->getValidatedInput())->toEqual(shape());
}

}
34 changes: 6 additions & 28 deletions tests/examples/codegen/ObjectSchemaValidator.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<<5d8158e850af0f6d550037bc4003da67>>
* @generated SignedSource<<39430f90b530b5635008632715853853>>
*/
namespace Slack\Hack\JsonSchema\Tests\Generated;
use namespace Slack\Hack\JsonSchema;
Expand Down Expand Up @@ -1662,33 +1662,11 @@ public static function check(
mixed $input,
string $pointer,
): TObjectSchemaValidatorPropertiesEmptyClosedShape {
$typed = Constraints\ObjectConstraint::check($input, $pointer, self::$coerce);

$errors = vec[];
$output = shape();

/*HHAST_IGNORE_ERROR[UnusedVariable] Some loops generated with this statement do not use their $value*/
foreach ($typed as $key => $value) {
if (\HH\Lib\C\contains_key(self::$properties, $key)) {
continue;
}

$errors[] = shape(
'code' => JsonSchema\FieldErrorCode::FAILED_CONSTRAINT,
'message' => "invalid additional property: {$key}",
'constraint' => shape(
'type' => JsonSchema\FieldErrorConstraint::ADDITIONAL_PROPERTIES,
'got' => $key,
),
);
}

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

/* HH_IGNORE_ERROR[4163] */
return $output;
// Hack to prevent us from having to change the params names when we are not
// using them.
$_ = $input;
$_ = $pointer;
return shape();
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/examples/object-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
"empty_closed_shape": {
"type": "object",
"additionalProperties": false,
"discardAdditionalProperties": false,
"discardAdditionalProperties": true,
"properties": {}
}
}
Expand Down

0 comments on commit 8b894cd

Please sign in to comment.