diff --git a/CHANGELOG.md b/CHANGELOG.md index eb0b7062..c977af64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to `mcp/sdk` will be documented in this file. +0.11.0 +------ + +### Server + +* Fix const validation errors always reporting the expected value as null. + 0.10.0 ------ diff --git a/src/Server/Capability/Discovery/SchemaValidator.php b/src/Server/Capability/Discovery/SchemaValidator.php index 9350fc83..f317731d 100644 --- a/src/Server/Capability/Discovery/SchemaValidator.php +++ b/src/Server/Capability/Discovery/SchemaValidator.php @@ -240,7 +240,7 @@ private function formatValidationError(ValidationError $error): string } break; case 'const': - $expected = json_encode($args['expected'] ?? 'null', \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE); + $expected = json_encode($args['const'] ?? null, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE); $message = "Value must be equal to the constant value: {$expected}."; break; case 'minLength': diff --git a/tests/Unit/Server/Capability/Discovery/SchemaValidatorTest.php b/tests/Unit/Server/Capability/Discovery/SchemaValidatorTest.php index d62a9311..49039559 100644 --- a/tests/Unit/Server/Capability/Discovery/SchemaValidatorTest.php +++ b/tests/Unit/Server/Capability/Discovery/SchemaValidatorTest.php @@ -113,6 +113,28 @@ public function testEnumConstraintViolation(): void $this->assertStringContainsString('must be one of the allowed values: "A", "B"', $errors[0]['message']); } + public function testConstConstraintViolationIncludesExpectedValue(): void + { + $schema = ['type' => 'string', 'const' => 'PairMatching']; + $data = 'Quiz'; + + $errors = $this->validator->validateAgainstJsonSchema($data, $schema); + $this->assertCount(1, $errors); + $this->assertEquals('const', $errors[0]['keyword']); + $this->assertSame('Value must be equal to the constant value: "PairMatching".', $errors[0]['message']); + } + + public function testConstConstraintViolationWithNullConstant(): void + { + $schema = ['const' => null]; + $data = 'not-null'; + + $errors = $this->validator->validateAgainstJsonSchema($data, $schema); + $this->assertCount(1, $errors); + $this->assertEquals('const', $errors[0]['keyword']); + $this->assertSame('Value must be equal to the constant value: null.', $errors[0]['message']); + } + public function testMinimumConstraintViolation(): void { $schema = ['type' => 'integer', 'minimum' => 10];