Class: RubyLLM::Schema::DSL::ConditionalBuilder
- Inherits:
-
Object
- Object
- RubyLLM::Schema::DSL::ConditionalBuilder
- 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
- #empty? ⇒ Boolean
- #required_fields ⇒ Object
- #requires(*fields) ⇒ Object
- #to_schema ⇒ Object
- #validates(field, **options) ⇒ Object
- #validations_empty? ⇒ Boolean
Instance Method Details
#empty? ⇒ Boolean
145 146 147 |
# File 'lib/ruby_llm/schema/dsl/conditionals.rb', line 145 def empty? required.empty? && validations.empty? end |
#required_fields ⇒ Object
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_schema ⇒ Object
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, **) constraints = {} .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
153 154 155 |
# File 'lib/ruby_llm/schema/dsl/conditionals.rb', line 153 def validations_empty? validations.empty? end |