Class: Riffer::Params
- Inherits:
-
Object
- Object
- Riffer::Params
- Defined in:
- lib/riffer/params.rb
Overview
Riffer::Params provides a DSL for defining parameters.
Used within a Tool’s params block to define required and optional parameters, and by StructuredOutput to define response schemas.
params do
required :city, String, description: "The city name"
optional :units, String, default: "celsius", enum: ["celsius", "fahrenheit"]
end
Instance Attribute Summary collapse
-
#parameters ⇒ Object
readonly
: Array.
Instance Method Summary collapse
-
#initialize ⇒ Params
constructor
– : () -> void.
-
#optional(name, type, description: nil, enum: nil, default: nil, of: nil, &block) ⇒ Object
Defines an optional parameter.
-
#required(name, type, description: nil, enum: nil, of: nil, &block) ⇒ Object
Defines a required parameter.
-
#to_json_schema(strict: false) ⇒ Object
Converts all parameters to JSON Schema format.
-
#validate(arguments) ⇒ Object
Validates arguments against parameter definitions.
Constructor Details
#initialize ⇒ Params
– : () -> void
19 20 21 |
# File 'lib/riffer/params.rb', line 19 def initialize @parameters = [] end |
Instance Attribute Details
#parameters ⇒ Object (readonly)
: Array
15 16 17 |
# File 'lib/riffer/params.rb', line 15 def parameters @parameters end |
Instance Method Details
#optional(name, type, description: nil, enum: nil, default: nil, of: nil, &block) ⇒ Object
Defines an optional parameter.
– : (Symbol, Class, ?description: String?, ?enum: Array?, ?default: untyped, ?of: Class?) ?{ () -> void } -> void
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/riffer/params.rb', line 44 def optional(name, type, description: nil, enum: nil, default: nil, of: nil, &block) nested = build_nested(type, of, &block) @parameters << Riffer::Param.new( name: name, type: type, required: false, description: description, enum: enum, default: default, item_type: of, nested_params: nested ) end |
#required(name, type, description: nil, enum: nil, of: nil, &block) ⇒ Object
Defines a required parameter.
– : (Symbol, Class, ?description: String?, ?enum: Array?, ?of: Class?) ?{ () -> void } -> void
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/riffer/params.rb', line 27 def required(name, type, description: nil, enum: nil, of: nil, &block) nested = build_nested(type, of, &block) @parameters << Riffer::Param.new( name: name, type: type, required: true, description: description, enum: enum, item_type: of, nested_params: nested ) end |
#to_json_schema(strict: false) ⇒ Object
Converts all parameters to JSON Schema format.
When strict is true, every property appears in required and optional properties are made nullable instead. This satisfies providers that enforce strict structured output schemas.
– : (?strict: bool) -> Hash[Symbol, untyped]
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/riffer/params.rb', line 109 def to_json_schema(strict: false) properties = {} required_params = [] @parameters.each do |param| properties[param.name.to_s] = param.to_json_schema(strict: strict) required_params << param.name.to_s if strict || param.required end { type: "object", properties: properties, required: required_params, additionalProperties: false } end |
#validate(arguments) ⇒ Object
Validates arguments against parameter definitions.
Raises Riffer::ValidationError if validation fails.
– : (Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/riffer/params.rb', line 64 def validate(arguments) validated = {} errors = [] @parameters.each do |param| value = arguments[param.name] if value.nil? && param.required errors << "#{param.name} is required" next end if value.nil? validated[param.name] = param.default next end unless param.valid_type?(value) errors << "#{param.name} must be a #{param.type_name}" next end if param.enum && !param.enum.include?(value) errors << "#{param.name} must be one of: #{param.enum.join(", ")}" next end value = validate_nested(param, value, errors) validated[param.name] = value end raise Riffer::ValidationError, errors.join("; ") if errors.any? validated end |