From 8b894cd55377c736f8d61a2aace46cc7b5bdc592 Mon Sep 17 00:00:00 2001 From: Ian Hoffman Date: Thu, 9 Mar 2023 10:09:29 -0500 Subject: [PATCH] use discardAdditionalProperties: true so we actually test the right thing --- tests/ObjectSchemaValidatorTest.php | 16 +++------ .../codegen/ObjectSchemaValidator.php | 34 ++++--------------- tests/examples/object-schema.json | 2 +- 3 files changed, 11 insertions(+), 41 deletions(-) diff --git a/tests/ObjectSchemaValidatorTest.php b/tests/ObjectSchemaValidatorTest.php index dcf4f29..fca7bd2 100644 --- a/tests/ObjectSchemaValidatorTest.php +++ b/tests/ObjectSchemaValidatorTest.php @@ -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()); } } diff --git a/tests/examples/codegen/ObjectSchemaValidator.php b/tests/examples/codegen/ObjectSchemaValidator.php index 870f24a..3ec1e0b 100644 --- a/tests/examples/codegen/ObjectSchemaValidator.php +++ b/tests/examples/codegen/ObjectSchemaValidator.php @@ -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; @@ -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(); } } diff --git a/tests/examples/object-schema.json b/tests/examples/object-schema.json index db8ab3d..d671083 100644 --- a/tests/examples/object-schema.json +++ b/tests/examples/object-schema.json @@ -180,7 +180,7 @@ "empty_closed_shape": { "type": "object", "additionalProperties": false, - "discardAdditionalProperties": false, + "discardAdditionalProperties": true, "properties": {} } }