MCP PHP SDK

SchemaValidator

Validates data against JSON Schema definitions using opis/json-schema.

Tags
author

Kyrian Obikwelu koshnawaza@gmail.com

Table of Contents

Properties

$jsonSchemaValidator  : Validator|null
$logger  : LoggerInterface

Methods

__construct()  : mixed
validateAgainstJsonSchema()  : array<int, array{pointer: string, keyword: string, message: string}>
Validates data against a JSON schema.
collectSubErrors()  : void
Recursively collects leaf validation errors.
convertDataForValidator()  : mixed
Recursively converts associative arrays to stdClass objects for validator compatibility.
formatJsonPointerPath()  : string
Formats the path array into a JSON Pointer string.
formatValidationError()  : string
Formats an Opis SchemaValidationError into a user-friendly message.
getJsonSchemaValidator()  : Validator
Get or create the JSON Schema validator instance.

Properties

$jsonSchemaValidator

private Validator|null $jsonSchemaValidator = null

Methods

__construct()

public __construct([LoggerInterface $logger = new NullLogger() ]) : mixed
Parameters
$logger : LoggerInterface = new NullLogger()

validateAgainstJsonSchema()

Validates data against a JSON schema.

public validateAgainstJsonSchema(mixed $data, array<string, mixed>|object $schema) : array<int, array{pointer: string, keyword: string, message: string}>
Parameters
$data : mixed

the data to validate (should generally be decoded JSON)

$schema : array<string, mixed>|object

the JSON Schema definition (as PHP array or object)

Return values
array<int, array{pointer: string, keyword: string, message: string}>

array of validation errors, empty if valid

collectSubErrors()

Recursively collects leaf validation errors.

private collectSubErrors(ValidationError $error, array<string|int, Error&$collectedErrors) : void
Parameters
$error : ValidationError
$collectedErrors : array<string|int, Error>

convertDataForValidator()

Recursively converts associative arrays to stdClass objects for validator compatibility.

private convertDataForValidator(mixed $data) : mixed
Parameters
$data : mixed

formatJsonPointerPath()

Formats the path array into a JSON Pointer string.

private formatJsonPointerPath(array<string|int, string>|array<string|int, int>|null $pathComponents) : string
Parameters
$pathComponents : array<string|int, string>|array<string|int, int>|null
Return values
string

formatValidationError()

Formats an Opis SchemaValidationError into a user-friendly message.

private formatValidationError(ValidationError $error) : string
Parameters
$error : ValidationError
Return values
string

getJsonSchemaValidator()

Get or create the JSON Schema validator instance.

private getJsonSchemaValidator() : Validator
Return values
Validator

        
On this page

Search results