Nous.Tool.Validator (nous v0.13.3)
View SourceValidates tool arguments against JSON schema.
Provides validation of arguments before tool execution to catch errors early and provide helpful feedback to the LLM.
Example
schema = %{
"type" => "object",
"properties" => %{
"query" => %{"type" => "string"},
"limit" => %{"type" => "integer"}
},
"required" => ["query"]
}
args = %{"query" => "elixir", "limit" => 10}
{:ok, args} = Validator.validate(args, schema)
# Missing required field
{:error, {:missing_required, ["query"]}} = Validator.validate(%{}, schema)
# Wrong type
{:error, {:type_mismatch, [{"limit", "integer", "string"}]}} =
Validator.validate(%{"query" => "x", "limit" => "not a number"}, schema)Integration with ToolExecutor
When tool.validate_args is true, the ToolExecutor will validate
arguments before calling the tool function.
Summary
Functions
Format validation errors into a human-readable string.
Check if a value matches a JSON schema type.
Get the JSON schema type name for a value.
Validate arguments against a JSON schema.
Validate arguments and raise on error.
Validate that all required fields are present.
Validate argument types against schema properties.
Types
Functions
@spec format_error(validation_error()) :: String.t()
Format validation errors into a human-readable string.
Example
error = {:missing_required, ["query", "limit"]}
Validator.format_error(error)
# => "Missing required fields: query, limit"
Check if a value matches a JSON schema type.
Supports: string, integer, number, boolean, array, object, null
Example
Validator.matches_type?("hello", "string") # => true
Validator.matches_type?(42, "integer") # => true
Validator.matches_type?(3.14, "number") # => true
Validator.matches_type?([1, 2], "array") # => true
Validator.matches_type?(%{}, "object") # => true
Get the JSON schema type name for a value.
Example
Validator.typeof("hello") # => "string"
Validator.typeof(42) # => "integer"
Validator.typeof(3.14) # => "number"
Validator.typeof([]) # => "array"
Validator.typeof(%{}) # => "object"
Validator.typeof(nil) # => "null"
@spec validate(map(), map()) :: {:ok, map()} | {:error, validation_error()}
Validate arguments against a JSON schema.
Parameters
args- The arguments map to validateschema- JSON schema with "properties" and "required" fields
Returns
{:ok, args}- Arguments are valid{:error, reason}- Validation failed
Example
schema = %{
"type" => "object",
"properties" => %{
"name" => %{"type" => "string"}
},
"required" => ["name"]
}
Validator.validate(%{"name" => "Alice"}, schema)
# => {:ok, %{"name" => "Alice"}}
Validate arguments and raise on error.
Useful for tests or when you want to fail fast.
Example
Validator.validate!(args, schema) # Raises on invalid
Validate that all required fields are present.
Example
Validator.validate_required(%{"a" => 1}, ["a", "b"])
# => {:error, {:missing_required, ["b"]}}
Validate argument types against schema properties.
Example
properties = %{
"count" => %{"type" => "integer"}
}
Validator.validate_types(%{"count" => "not a number"}, properties)
# => {:error, {:type_mismatch, [{"count", "integer", "string"}]}}