1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
12: */
13:
14: namespace Nexus\Mcp\Server\Validation;
15:
16: use Opis\JsonSchema\Errors\ErrorFormatter;
17: use Opis\JsonSchema\Helper;
18: use Opis\JsonSchema\Validator;
19:
20: /**
21: * Default `SchemaValidatorInterface` backed by opis/json-schema.
22: */
23: final readonly class OpisSchemaValidator implements SchemaValidatorInterface
24: {
25: private Validator $validator;
26:
27: public function __construct()
28: {
29: $this->validator = new Validator();
30: }
31:
32: #[\Override]
33: public function validate(mixed $data, array $schema): array
34: {
35: $error = $this->validator->validate(Helper::toJSON($data), (object) Helper::toJSON($schema))->error();
36:
37: if (null === $error) {
38: return [];
39: }
40:
41: /** @var list<string> $messages */
42: $messages = new ErrorFormatter()->formatFlat($error);
43:
44: return $messages;
45: }
46: }
47: