ExMCP.Content.SchemaValidator (ex_mcp v0.10.0)

View Source

Schema validation for MCP content.

This module handles all schema-based validation, including JSON Schema validation and custom schema rules. Extracted from the original Content.Validation module to follow Single Responsibility Principle.

Summary

Functions

Validates content format and structure.

Validates content size against maximum limits.

Validates MIME types against allowed types.

Validates that required fields are present and non-empty.

Validates content against a JSON Schema.

Types

validation_error()

@type validation_error() :: %{
  rule: atom(),
  message: String.t(),
  field: String.t() | nil,
  value: any(),
  severity: :error | :warning | :info
}

validation_result()

@type validation_result() :: :ok | {:error, [validation_error()]}

Functions

validate_format(content)

@spec validate_format(ExMCP.Content.Protocol.content()) :: validation_result()

Validates content format and structure.

validate_max_size(arg1, max_size)

@spec validate_max_size(ExMCP.Content.Protocol.content(), pos_integer()) ::
  validation_result()

Validates content size against maximum limits.

validate_mime_types(arg1, allowed_types)

@spec validate_mime_types(ExMCP.Content.Protocol.content(), [String.t()]) ::
  validation_result()

Validates MIME types against allowed types.

validate_required_fields(arg1)

@spec validate_required_fields(ExMCP.Content.Protocol.content()) ::
  validation_result()

Validates that required fields are present and non-empty.

validate_schema(content, schema)

@spec validate_schema(ExMCP.Content.Protocol.content(), map()) :: validation_result()

Validates content against a JSON Schema.

Examples

schema = %{
  type: "object",
  properties: %{
    name: %{type: "string"},
    age: %{type: "integer", minimum: 0}
  },
  required: ["name"]
}

case SchemaValidator.validate_schema(content, schema) do
  :ok -> :valid
  {:error, errors} -> handle_errors(errors)
end