class Riffer::Params::Param
Constants
- CLASS_TYPES
- JSON_TYPE_MAPPINGS
- PRIMITIVE_TYPES
- TYPES
Attributes
Public Class Methods
Source
# File lib/riffer/params/param.rb, line 88 def self.from_json_schema(name, schema, required:) variants = resolve_variants(name, schema[:anyOf]) if schema.key?(:anyOf) type = variants ? :any_of : json_type(schema[:type]) nested = resolve_nested_params(type, schema) item_type, dimensions, items = resolve_typed_items(schema) if type == :array && !nested new( name: name, type: type, required: required, description: schema[:description], enum: schema[:enum] || items&.dig(:enum), format: schema[:format] || items&.dig(:format), default: schema[:default], item_type: item_type, dimensions: dimensions || 1, nested_params: nested, variants: variants, ) end
Source
# File lib/riffer/params/param.rb, line 183 def initialize(name:, type:, required:, description: nil, enum: nil, format: nil, default: nil, item_type: nil, dimensions: 1, nested_params: nil, variants: nil) raise Riffer::ArgumentError, "#{name}: an :any_of param requires variants" if type == :any_of && variants.nil? @name = name.to_sym @type = type @required = required @description = description @enum = enum # Frozen and deduplicated, so copies never share a mutable string. @format = format.is_a?(String) ? -format : format @default = default @item_type = item_type @dimensions = dimensions @nested_params = nested_params @variants = variants # Checked here rather than in Params so from_json_schema enforces them too. validate_format! if format validate_variant_order! if variants end
Source
# File lib/riffer/params/param.rb, line 49 def self.resolve_type(type) return type if TYPES.include?(type) symbol = CLASS_TYPES[type] #: Symbol? return unless symbol Riffer::Deprecation.warn( id: :param_class_types, message: "#{type.name} as a param type is deprecated and will be removed in a future version, " \ "not before 1 December 2026; use #{symbol.inspect}.", ) symbol end
Source
# File lib/riffer/params/param.rb, line 65 def self.resolve_type!(type, subject, allowed) resolved = resolve_type(type) return resolved if resolved && allowed.include?(resolved) raise Riffer::ArgumentError, "#{subject} must be one of #{allowed.map(&:inspect).join(', ')}, got #{type.inspect}" end
Source
# File lib/riffer/params/param.rb, line 74 def self.value_of_type?(type, value) case type when :string then value.is_a?(String) when :integer then value.is_a?(Integer) when :number then value.is_a?(Numeric) when :boolean then [true, false].include?(value) when :array then value.is_a?(Array) when :object then value.is_a?(Hash) else false end end
Public Instance Methods
Source
# File lib/riffer/params/param.rb, line 229 def to_json_schema(strict: false) validate_strict_shape! if strict # Nullable so strict providers can distinguish absent from present. nullable = strict && !required if type == :any_of && variants schema = { anyOf: variants.map { |variant| variant.to_json_schema(strict: strict) } } #: Hash[Symbol, untyped] schema[:anyOf] << { type: "null" } if nullable schema[:description] = description if description schema[:default] = default unless strict || default.nil? return schema end # A typed array's enum and format constrain its items, not the array. value_enum = enum unless item_type value_format = format unless item_type # Providers like Anthropic reject a nullable type union combined with enum. if nullable && value_enum schema = { anyOf: [{ type: type_name, enum: value_enum }, { type: "null" }] } #: Hash[Symbol, untyped] schema[:description] = description if description return schema end type = type_name type = [type, "null"] if nullable schema = { type: type } #: Hash[Symbol, untyped] schema[:description] = description if description schema[:enum] = value_enum if value_enum schema[:format] = value_format if value_format # Strict providers reject the +default+ keyword; emit it only in # non-strict mode, where it makes the schema a lossless round-trip # source for +from_json_schema+. schema[:default] = default unless strict || default.nil? if self.type == :array && nested_params schema[:items] = nested_params.to_json_schema(strict: strict) elsif self.type == :array && item_type items = { type: item_type.name, enum: enum, format: format }.compact #: Hash[Symbol, untyped] (dimensions - 1).times { items = { type: "array", items: items } } schema[:items] = items elsif self.type == :object && nested_params # The nested schema carries its own type: "object", which would clobber a nullable union. schema.merge!(nested_params.to_json_schema(strict: strict).except(:type)) end schema end
Source
# File lib/riffer/params/param.rb, line 218 def type_name if type == :any_of names = Array(variants).map { |variant| variant.is_a?(Riffer::Params) ? "object" : variant.type_name } return names.uniq.join(" or ") end type.name end
Source
# File lib/riffer/params/param.rb, line 206 def valid_type?(value) return true if value.nil? && !required if type == :any_of Array(variants).any? { |variant| variant.is_a?(Riffer::Params) ? value.is_a?(Hash) : variant.valid_type?(value) } else self.class.value_of_type?(type, value) end end