Skip to content

Commit cc46ee5

Browse files
VibeSWEclaude
andcommitted
Enforce JSON Schema type unions in tool argument validation
`validate_type` ignored array-valued `type` (for example `["string", "null"]`), so the dashboard operation schemas' nullable and union fields were never type-checked. Accept a value when it matches any listed type, and name every allowed type in the error. Verified the dashboard agent E2E script's create, update, rename, placement, invalid-layout and protected-field payloads still pass schema validation, so service-side 422/400 responses are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0097de1 commit cc46ee5

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

‎docs/tool-api.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ rerun that SQL unchanged, for example in a dashboard tile.
217217

218218
## Argument schema validation
219219

220-
`inputSchema` is enforced server-side for `type`, `required`,
220+
`inputSchema` is enforced server-side for `type` (a single type or a list of types), `required`,
221221
`additionalProperties`, `enum`, `const`, `minLength`/`maxLength`,
222222
`minimum`/`maximum`, `minItems`/`maxItems`, `items`, `anyOf` and `oneOf`.
223223
`oneOf` passes only when exactly one variant matches. Combinators are checked

‎src/tool_catalog.rs‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,14 @@ fn validate_enum(schema: &Value, value: &Value, path: &str) -> Result<(), String
194194
}
195195

196196
fn validate_type(schema: &Value, value: &Value, path: &str) -> Result<(), String> {
197-
let Some(expected_type) = schema.get("type").and_then(Value::as_str) else {
198-
return Ok(());
197+
let expected_types = match schema.get("type") {
198+
Some(Value::String(expected_type)) => vec![expected_type.as_str()],
199+
Some(Value::Array(expected_types)) => {
200+
expected_types.iter().filter_map(Value::as_str).collect()
201+
}
202+
_ => return Ok(()),
199203
};
200-
let type_matches = match expected_type {
204+
let type_matches = |expected_type: &str| match expected_type {
201205
"object" => value.is_object(),
202206
"array" => value.is_array(),
203207
"string" => value.is_string(),
@@ -207,10 +211,10 @@ fn validate_type(schema: &Value, value: &Value, path: &str) -> Result<(), String
207211
"null" => value.is_null(),
208212
_ => true,
209213
};
210-
if type_matches {
214+
if expected_types.is_empty() || expected_types.iter().any(|kind| type_matches(kind)) {
211215
Ok(())
212216
} else {
213-
Err(format!("{path} must be a {expected_type}"))
217+
Err(format!("{path} must be a {}", expected_types.join(" or ")))
214218
}
215219
}
216220

@@ -1266,6 +1270,23 @@ mod tests {
12661270
);
12671271
}
12681272

1273+
#[actix_web::test]
1274+
async fn schema_type_unions_accept_any_listed_type() {
1275+
let schema = serde_json::json!({ "type": ["string", "null"] });
1276+
for value in [serde_json::json!("x"), serde_json::json!(null)] {
1277+
assert!(validate_tool_arguments(&schema, &value).is_ok(), "{value}");
1278+
}
1279+
let error = validate_tool_arguments(&schema, &serde_json::json!(1)).unwrap_err();
1280+
assert_eq!(error, "arguments must be a string or null");
1281+
assert!(
1282+
validate_tool_arguments(
1283+
&serde_json::json!({ "type": "integer" }),
1284+
&serde_json::json!(1.5)
1285+
)
1286+
.is_err()
1287+
);
1288+
}
1289+
12691290
#[actix_web::test]
12701291
async fn tool_results_use_mcp_call_tool_result_shape() {
12711292
let result = serde_json::to_value(ToolCallResult::success(serde_json::json!({

0 commit comments

Comments
 (0)