Validate and normalize tool parameters against a schema:
- Check all
requiredparams are present - Validate types match (str, int, float, bool)
- Apply
defaultvalues for missing optional params - Return normalized params with defaults applied
Type Conversion:
- Try to convert values to correct type
- '123' → 123 for int type
- 'true' → True for bool type
Error Format:
{'param': 'name', 'error': 'description'}
Examples
Example 1:
Input:
validate_tool_input({'name': 'test'}, {'name': {'type': 'str', 'required': True}})Output:
{'valid': True, 'errors': [], 'normalized_params': {'name': 'test'}}Explanation: Required param present, correct type
Example 2:
Input:
validate_tool_input({}, {'count': {'type': 'int', 'required': False, 'default': 5}})Output:
{'valid': True, 'errors': [], 'normalized_params': {'count': 5}}Explanation: Missing optional param, default applied
Starter Code
def validate_tool_input(params, schema):
"""
Validate tool parameters against a JSON-like schema.
Schema format:
{
'param_name': {'type': 'str|int|float|bool', 'required': True/False, 'default': val},
...
}
Returns:
dict with 'valid' (bool), 'errors' (list), 'normalized_params' (dict)
"""
# Your implementation here
passPython3
ReadyLines: 1Characters: 0
Ready