| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 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: | |
| 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: | |
| 42: | $messages = new ErrorFormatter()->formatFlat($error); |
| 43: | |
| 44: | return $messages; |
| 45: | } |
| 46: | } |
| 47: | |