class Riffer::Params
rbs_inline: enabled
Attributes
Public Class Methods
Source
# File lib/riffer/params.rb, line 17 def self.from_json_schema(schema) params = new properties = schema[:properties] || {} required = (schema[:required] || []).map(&:to_s) properties.each do |name, property_schema| params.parameters << Riffer::Params::Param.from_json_schema( name.to_sym, property_schema, required: required.include?(name.to_s), ) end params end
The inverse of +to_json_schema(strict: false)+. Raises Riffer::ArgumentError on features outside the Params-expressible subset of JSON Schema.
Public Instance Methods
Source
# File lib/riffer/params.rb, line 48 def optional(name, type, description: nil, enum: nil, default: nil, of: nil, &) nested = build_nested(type, of, &) @parameters << Riffer::Params::Param.new( name: name, type: type, required: false, description: description, enum: enum, default: default, item_type: of, nested_params: nested, ) end
Source
# File lib/riffer/params.rb, line 33 def required(name, type, description: nil, enum: nil, of: nil, &) nested = build_nested(type, of, &) @parameters << Riffer::Params::Param.new( name: name, type: type, required: true, description: description, enum: enum, item_type: of, nested_params: nested, ) end
Source
# File lib/riffer/params.rb, line 103 def to_json_schema(strict: false) properties = {} #: Hash[String, untyped] required_params = [] #: Array[String] @parameters.each do |param| properties[param.name.to_s] = param.to_json_schema(strict: strict) # Providers enforcing strict structured output require every property in # +required+; strict mode makes optional ones nullable instead. required_params << param.name.to_s if strict || param.required end { type: "object", properties: properties, required: required_params, additionalProperties: false, } end
Source
# File lib/riffer/params.rb, line 64 def validate(arguments) validated = {} #: Hash[Symbol, untyped] errors = [] #: Array[String] @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] = coerce_value(param.type, value) end raise Riffer::ValidationError, errors.join("; ") if errors.any? validated end