rust_mcp_schema/generated_schema/2025_06_18/
validators.rs

1/// Validates that a deserialized string field matches a given constant value.
2///
3/// This function is intended for use with `#[serde(deserialize_with)]` to enforce
4/// that a field in a struct always has a fixed, expected string value during deserialization.
5///
6/// # Parameters
7/// - `struct_name`: The name of the struct where this validation is applied.
8/// - `field_name`: The name of the field being validated.
9/// - `expected`: The expected constant string value for the field.
10/// - `deserializer`: The Serde deserializer for the field.
11///
12/// # Returns
13/// - `Ok(String)` if the deserialized value matches the expected value.
14/// - `Err(D::Error)` if the value differs, with an error message indicating
15///   which struct and field failed validation.
16///
17pub fn const_str_validator<'de, D>(
18    struct_name: &'static str,
19    field_name: &'static str,
20    expected: &'static str,
21    deserializer: D,
22) -> Result<String, D::Error>
23where
24    D: serde::de::Deserializer<'de>,
25{
26    let value: String = serde::Deserialize::deserialize(deserializer)?;
27    if value == expected {
28        Ok(value)
29    } else {
30        Err(serde::de::Error::custom(format!(
31            "Expected field `{field_name}` in struct `{struct_name}` as const value '{expected}', but got '{value}'",
32        )))
33    }
34}
35
36/// Macro to generate a field-specific validator function for use with Serde.
37///
38/// This avoids repetitive boilerplate when you have multiple fields/structs
39/// requiring constant string validation.
40///
41/// # Syntax
42/// ```ignore
43/// validate!(fn_name, "StructName", "field_name", "expected_value");
44/// ```
45///
46/// - `fn_name`: The function name to generate.
47/// - `StructName`: The name of the struct (for error messages).
48/// - `field_name`: The name of the field (for error messages).
49/// - `expected_value`: The required constant string value.
50///
51macro_rules! validate {
52    ($func_name:ident,  $struct:expr, $field:expr, $expected:expr $(,)?) => {
53        pub(crate) fn $func_name<'de, D>(deserializer: D) -> Result<String, D::Error>
54        where
55            D: serde::de::Deserializer<'de>,
56        {
57            const_str_validator($struct, $field, $expected, deserializer)
58        }
59    };
60}
61
62//* Validator Functions *//
63validate!(audio_content_type_, "AudioContent", "type_", "audio");
64validate!(boolean_schema_type_, "BooleanSchema", "type_", "boolean");
65validate!(call_tool_request_method, "CallToolRequest", "method", "tools/call");
66validate!(
67    cancelled_notification_method,
68    "CancelledNotification",
69    "method",
70    "notifications/cancelled"
71);
72validate!(complete_request_method, "CompleteRequest", "method", "completion/complete");
73validate!(
74    create_message_request_method,
75    "CreateMessageRequest",
76    "method",
77    "sampling/createMessage"
78);
79validate!(elicit_request_method, "ElicitRequest", "method", "elicitation/create");
80validate!(elicit_requested_schema_type_, "ElicitRequestedSchema", "type_", "object");
81validate!(embedded_resource_type_, "EmbeddedResource", "type_", "resource");
82validate!(enum_schema_type_, "EnumSchema", "type_", "string");
83validate!(get_prompt_request_method, "GetPromptRequest", "method", "prompts/get");
84validate!(image_content_type_, "ImageContent", "type_", "image");
85validate!(initialize_request_method, "InitializeRequest", "method", "initialize");
86validate!(
87    initialized_notification_method,
88    "InitializedNotification",
89    "method",
90    "notifications/initialized"
91);
92validate!(jsonrpc_error_jsonrpc, "JsonrpcError", "jsonrpc", "2.0");
93validate!(jsonrpc_notification_jsonrpc, "JsonrpcNotification", "jsonrpc", "2.0");
94validate!(jsonrpc_request_jsonrpc, "JsonrpcRequest", "jsonrpc", "2.0");
95validate!(jsonrpc_response_jsonrpc, "JsonrpcResponse", "jsonrpc", "2.0");
96validate!(list_prompts_request_method, "ListPromptsRequest", "method", "prompts/list");
97validate!(
98    list_resource_templates_request_method,
99    "ListResourceTemplatesRequest",
100    "method",
101    "resources/templates/list"
102);
103validate!(
104    list_resources_request_method,
105    "ListResourcesRequest",
106    "method",
107    "resources/list"
108);
109validate!(list_roots_request_method, "ListRootsRequest", "method", "roots/list");
110validate!(list_tools_request_method, "ListToolsRequest", "method", "tools/list");
111validate!(
112    logging_message_notification_method,
113    "LoggingMessageNotification",
114    "method",
115    "notifications/message"
116);
117validate!(ping_request_method, "PingRequest", "method", "ping");
118validate!(
119    progress_notification_method,
120    "ProgressNotification",
121    "method",
122    "notifications/progress"
123);
124validate!(
125    prompt_list_changed_notification_method,
126    "PromptListChangedNotification",
127    "method",
128    "notifications/prompts/list_changed"
129);
130validate!(prompt_reference_type_, "PromptReference", "type_", "ref/prompt");
131validate!(
132    read_resource_request_method,
133    "ReadResourceRequest",
134    "method",
135    "resources/read"
136);
137validate!(resource_link_type_, "ResourceLink", "type_", "resource_link");
138validate!(
139    resource_list_changed_notification_method,
140    "ResourceListChangedNotification",
141    "method",
142    "notifications/resources/list_changed"
143);
144validate!(
145    resource_template_reference_type_,
146    "ResourceTemplateReference",
147    "type_",
148    "ref/resource"
149);
150validate!(
151    resource_updated_notification_method,
152    "ResourceUpdatedNotification",
153    "method",
154    "notifications/resources/updated"
155);
156validate!(
157    roots_list_changed_notification_method,
158    "RootsListChangedNotification",
159    "method",
160    "notifications/roots/list_changed"
161);
162validate!(set_level_request_method, "SetLevelRequest", "method", "logging/setLevel");
163validate!(string_schema_type_, "StringSchema", "type_", "string");
164validate!(subscribe_request_method, "SubscribeRequest", "method", "resources/subscribe");
165validate!(text_content_type_, "TextContent", "type_", "text");
166validate!(tool_input_schema_type_, "ToolInputSchema", "type_", "object");
167validate!(
168    tool_list_changed_notification_method,
169    "ToolListChangedNotification",
170    "method",
171    "notifications/tools/list_changed"
172);
173validate!(tool_output_schema_type_, "ToolOutputSchema", "type_", "object");
174validate!(
175    unsubscribe_request_method,
176    "UnsubscribeRequest",
177    "method",
178    "resources/unsubscribe"
179);