Class: RubyLLM::Schema::DSL::ConditionalBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/schema/dsl/conditionals.rb

Constant Summary collapse

VALIDATES_KEY_MAP =
{
  type: :type,
  const: :const,
  enum: :enum,
  not_value: :not,
  min_length: :minLength,
  max_length: :maxLength,
  pattern: :pattern,
  minimum: :minimum,
  maximum: :maximum
}.freeze

Instance Method Summary collapse

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/ruby_llm/schema/dsl/conditionals.rb', line 145

def empty?
  required.empty? && validations.empty?
end

#required_fieldsObject



149
150
151
# File 'lib/ruby_llm/schema/dsl/conditionals.rb', line 149

def required_fields
  required.dup
end

#requires(*fields) ⇒ Object



102
103
104
# File 'lib/ruby_llm/schema/dsl/conditionals.rb', line 102

def requires(*fields)
  required.concat(fields.map(&:to_s))
end

#to_schemaObject



136
137
138
139
140
141
142
143
# File 'lib/ruby_llm/schema/dsl/conditionals.rb', line 136

def to_schema
  schema = {}

  schema[:required] = required if required.any?
  schema[:properties] = validations if validations.any?

  schema
end

#validates(field, **options) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ruby_llm/schema/dsl/conditionals.rb', line 118

def validates(field, **options)
  constraints = {}

  options.each do |key, value|
    schema_key = VALIDATES_KEY_MAP[key]
    raise ArgumentError, "unknown validates option: #{key.inspect}" unless schema_key

    case key
    when :type then constraints[:type] = value.to_s
    when :not_value then constraints[:not] = {const: value}
    when :pattern then constraints[:pattern] = value.is_a?(Regexp) ? value.source : value
    else constraints[schema_key] = value
    end
  end

  validations[field.to_s] = constraints
end

#validations_empty?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/ruby_llm/schema/dsl/conditionals.rb', line 153

def validations_empty?
  validations.empty?
end