ruby_llm-schema 0.4.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
metadata CHANGED
@@ -1,46 +1,52 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_llm-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Friis
8
+ - Carmine Paolino
8
9
  bindir: bin
9
10
  cert_chain: []
10
11
  date: 1980-01-02 00:00:00.000000000 Z
11
- dependencies: []
12
- description: A compact Ruby DSL for building standards-oriented JSON Schema documents
13
- from Ruby.
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: schematist
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description: Forwards RubyLLM::Schema to Schematist::Schema. Depend on schematist
28
+ instead.
14
29
  email:
15
30
  - d@friis.me
31
+ - carmine@paolino.me
16
32
  executables: []
17
33
  extensions: []
18
34
  extra_rdoc_files: []
19
35
  files:
20
- - LICENSE
21
36
  - README.md
22
37
  - lib/ruby_llm/schema.rb
23
- - lib/ruby_llm/schema/dsl.rb
24
- - lib/ruby_llm/schema/dsl/complex_types.rb
25
- - lib/ruby_llm/schema/dsl/conditionals.rb
26
- - lib/ruby_llm/schema/dsl/primitive_types.rb
27
- - lib/ruby_llm/schema/dsl/schema_builders.rb
28
- - lib/ruby_llm/schema/dsl/utilities.rb
29
- - lib/ruby_llm/schema/errors.rb
30
- - lib/ruby_llm/schema/helpers.rb
31
- - lib/ruby_llm/schema/json_output.rb
32
- - lib/ruby_llm/schema/validator.rb
33
- - lib/ruby_llm/schema/version.rb
34
- - lib/tasks/release.rake
35
- homepage: https://github.com/crmne/ruby_llm-schema#readme
38
+ homepage: https://github.com/crmne/schematist#migrating-from-ruby_llm-schema
36
39
  licenses:
37
40
  - MIT
38
41
  metadata:
39
- homepage_uri: https://github.com/crmne/ruby_llm-schema#readme
40
- source_code_uri: https://github.com/crmne/ruby_llm-schema
41
- changelog_uri: https://github.com/crmne/ruby_llm-schema/releases
42
- bug_tracker_uri: https://github.com/crmne/ruby_llm-schema/issues
42
+ homepage_uri: https://github.com/crmne/schematist#migrating-from-ruby_llm-schema
43
+ source_code_uri: https://github.com/crmne/schematist
44
+ changelog_uri: https://github.com/crmne/schematist/releases
45
+ bug_tracker_uri: https://github.com/crmne/schematist/issues
43
46
  rubygems_mfa_required: 'true'
47
+ post_install_message: |
48
+ ruby_llm-schema is now schematist. This release only forwards to it.
49
+ Switch your Gemfile to `gem 'schematist'` and RubyLLM::Schema to Schematist::Schema.
44
50
  rdoc_options: []
45
51
  require_paths:
46
52
  - lib
@@ -55,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
61
  - !ruby/object:Gem::Version
56
62
  version: '0'
57
63
  requirements: []
58
- rubygems_version: 4.0.10
64
+ rubygems_version: 4.0.16
59
65
  specification_version: 4
60
- summary: A simple Ruby DSL for creating JSON schemas.
66
+ summary: Deprecated. ruby_llm-schema is now schematist.
61
67
  test_files: []
data/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2025 Daniel Friis
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RubyLLM
4
- class Schema
5
- module DSL
6
- module ComplexTypes
7
- def object(name, description: nil, required: true, requires: nil, **options, &block)
8
- add_property(name, object_schema(description: description, **options, &block), required: required, requires: requires)
9
- end
10
-
11
- def array(name, description: nil, required: true, requires: nil, **options, &block)
12
- add_property(name, array_schema(description: description, **options, &block), required: required, requires: requires)
13
- end
14
-
15
- def any_of(name, description: nil, required: true, requires: nil, **options, &block)
16
- add_property(name, any_of_schema(description: description, **options, &block), required: required, requires: requires)
17
- end
18
-
19
- def one_of(name, description: nil, required: true, requires: nil, **options, &block)
20
- add_property(name, one_of_schema(description: description, **options, &block), required: required, requires: requires)
21
- end
22
-
23
- def optional(name, description: nil, &block)
24
- any_of(name, description: description) do
25
- instance_eval(&block)
26
- null
27
- end
28
- end
29
- end
30
- end
31
- end
32
- end
@@ -1,169 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RubyLLM
4
- class Schema
5
- module DSL
6
- module Conditionals
7
- def conditions
8
- @conditions ||= []
9
- end
10
-
11
- def dependencies
12
- @dependencies ||= {}
13
- end
14
-
15
- def dependent(property, &block)
16
- builder = ConditionalBuilder.new
17
- builder.instance_eval(&block)
18
-
19
- dependencies[property.to_s] = builder
20
- end
21
-
22
- def given(**properties, &block)
23
- raise ArgumentError, "given requires at least one property condition" if properties.empty?
24
-
25
- if_schema = {
26
- properties: properties.transform_keys(&:to_s).transform_values { |v| coerce_condition(v) },
27
- required: properties.keys.map(&:to_s)
28
- }
29
-
30
- then_builder = ConditionalBuilder.new
31
- else_builder = ConditionalBuilder.new
32
-
33
- context = ConditionalContext.new(then_builder, else_builder)
34
- context.instance_eval(&block)
35
-
36
- condition = {if: if_schema, then: then_builder.to_schema}
37
- condition[:else] = else_builder.to_schema unless else_builder.empty?
38
-
39
- conditions << condition
40
- end
41
-
42
- private
43
-
44
- def merge_conditions(schema, schema_class)
45
- if schema_class.respond_to?(:conditions) && schema_class.conditions.any?
46
- if schema_class.conditions.length == 1
47
- schema.merge!(schema_class.conditions.first)
48
- else
49
- schema[:allOf] = schema_class.conditions
50
- end
51
- end
52
-
53
- if schema_class.respond_to?(:dependencies) && schema_class.dependencies.any?
54
- dependent_required = {}
55
- dependent_schemas = {}
56
-
57
- schema_class.dependencies.each do |property, builder|
58
- if builder.validations_empty?
59
- dependent_required[property] = builder.required_fields
60
- else
61
- dependent_schemas[property] = builder.to_schema
62
- end
63
- end
64
-
65
- schema[:dependentRequired] = dependent_required if dependent_required.any?
66
- schema[:dependentSchemas] = dependent_schemas if dependent_schemas.any?
67
- end
68
-
69
- schema
70
- end
71
-
72
- def coerce_condition(value)
73
- case value
74
- when Array then {enum: value}
75
- when Regexp then {pattern: value.source}
76
- when Hash then value
77
- else {const: value}
78
- end
79
- end
80
- end
81
-
82
- class ConditionalContext
83
- def initialize(then_builder, else_builder)
84
- @then_builder = then_builder
85
- @else_builder = else_builder
86
- end
87
-
88
- def requires(*fields)
89
- @then_builder.requires(*fields)
90
- end
91
-
92
- def validates(field, **options)
93
- @then_builder.validates(field, **options)
94
- end
95
-
96
- def otherwise(&block)
97
- @else_builder.instance_eval(&block)
98
- end
99
- end
100
-
101
- class ConditionalBuilder
102
- def requires(*fields)
103
- required.concat(fields.map(&:to_s))
104
- end
105
-
106
- VALIDATES_KEY_MAP = {
107
- type: :type,
108
- const: :const,
109
- enum: :enum,
110
- not_value: :not,
111
- min_length: :minLength,
112
- max_length: :maxLength,
113
- pattern: :pattern,
114
- minimum: :minimum,
115
- maximum: :maximum
116
- }.freeze
117
-
118
- def validates(field, **options)
119
- constraints = {}
120
-
121
- options.each do |key, value|
122
- schema_key = VALIDATES_KEY_MAP[key]
123
- raise ArgumentError, "unknown validates option: #{key.inspect}" unless schema_key
124
-
125
- case key
126
- when :type then constraints[:type] = value.to_s
127
- when :not_value then constraints[:not] = {const: value}
128
- when :pattern then constraints[:pattern] = value.is_a?(Regexp) ? value.source : value
129
- else constraints[schema_key] = value
130
- end
131
- end
132
-
133
- validations[field.to_s] = constraints
134
- end
135
-
136
- def to_schema
137
- schema = {}
138
-
139
- schema[:required] = required if required.any?
140
- schema[:properties] = validations if validations.any?
141
-
142
- schema
143
- end
144
-
145
- def empty?
146
- required.empty? && validations.empty?
147
- end
148
-
149
- def required_fields
150
- required.dup
151
- end
152
-
153
- def validations_empty?
154
- validations.empty?
155
- end
156
-
157
- private
158
-
159
- def required
160
- @required ||= []
161
- end
162
-
163
- def validations
164
- @validations ||= {}
165
- end
166
- end
167
- end
168
- end
169
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RubyLLM
4
- class Schema
5
- module DSL
6
- module PrimitiveTypes
7
- def string(name, description: nil, required: true, requires: nil, **options)
8
- add_property(name, string_schema(description: description, **options), required: required, requires: requires)
9
- end
10
-
11
- def number(name, description: nil, required: true, requires: nil, **options)
12
- add_property(name, number_schema(description: description, **options), required: required, requires: requires)
13
- end
14
-
15
- def integer(name, description: nil, required: true, requires: nil, **options)
16
- add_property(name, integer_schema(description: description, **options), required: required, requires: requires)
17
- end
18
-
19
- def boolean(name, description: nil, required: true, requires: nil, **options)
20
- add_property(name, boolean_schema(description: description, **options), required: required, requires: requires)
21
- end
22
-
23
- def null(name, description: nil, required: true, requires: nil, **options)
24
- add_property(name, null_schema(description: description, **options), required: required, requires: requires)
25
- end
26
- end
27
- end
28
- end
29
- end
@@ -1,194 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RubyLLM
4
- class Schema
5
- module DSL
6
- module SchemaBuilders
7
- def string_schema(description: nil, enum: nil, min_length: nil, max_length: nil, pattern: nil, format: nil)
8
- {
9
- type: "string",
10
- enum: enum,
11
- description: description,
12
- minLength: min_length,
13
- maxLength: max_length,
14
- pattern: pattern,
15
- format: format
16
- }.compact
17
- end
18
-
19
- def number_schema(description: nil, minimum: nil, maximum: nil, multiple_of: nil)
20
- {
21
- type: "number",
22
- description: description,
23
- minimum: minimum,
24
- maximum: maximum,
25
- multipleOf: multiple_of
26
- }.compact
27
- end
28
-
29
- def integer_schema(description: nil, minimum: nil, maximum: nil, multiple_of: nil)
30
- {
31
- type: "integer",
32
- description: description,
33
- minimum: minimum,
34
- maximum: maximum,
35
- multipleOf: multiple_of
36
- }.compact
37
- end
38
-
39
- def boolean_schema(description: nil)
40
- {type: "boolean", description: description}.compact
41
- end
42
-
43
- def null_schema(description: nil)
44
- {type: "null", description: description}.compact
45
- end
46
-
47
- def object_schema(description: nil, of: nil, reference: nil, &block)
48
- if reference
49
- warn "[DEPRECATION] The `reference` option will be deprecated. Please use `of` instead."
50
- of = reference
51
- end
52
-
53
- if of
54
- determine_object_reference(of, description)
55
- else
56
- sub_schema = Class.new(Schema)
57
- result = sub_schema.class_eval(&block)
58
-
59
- # If the block returned a reference and no properties were added, use the reference
60
- if result.is_a?(Hash) && result["$ref"] && sub_schema.properties.empty?
61
- result.merge(description ? {description: description} : {})
62
- # If the block returned a Schema class or instance, convert it to inline schema
63
- elsif schema_class?(result) && sub_schema.properties.empty?
64
- schema_class_to_inline_schema(result).merge(description ? {description: description} : {})
65
- # Block didn't return reference or schema, so we build an inline object schema
66
- else
67
- schema = {
68
- type: "object",
69
- properties: sub_schema.properties,
70
- required: sub_schema.required_properties,
71
- additionalProperties: sub_schema.additional_properties,
72
- description: description
73
- }.compact
74
-
75
- merge_conditions(schema, sub_schema)
76
- end
77
- end
78
- end
79
-
80
- def array_schema(description: nil, of: nil, min_items: nil, max_items: nil, &block)
81
- items = determine_array_items(of, &block)
82
-
83
- {
84
- type: "array",
85
- description: description,
86
- items: items,
87
- minItems: min_items,
88
- maxItems: max_items
89
- }.compact
90
- end
91
-
92
- def any_of_schema(description: nil, &block)
93
- schemas = collect_schemas_from_block(&block)
94
-
95
- {
96
- description: description,
97
- anyOf: schemas
98
- }.compact
99
- end
100
-
101
- def one_of_schema(description: nil, &block)
102
- schemas = collect_schemas_from_block(&block)
103
-
104
- {
105
- description: description,
106
- oneOf: schemas
107
- }.compact
108
- end
109
-
110
- private
111
-
112
- def determine_array_items(of, &)
113
- return collect_schemas_from_block(&).first if block_given?
114
- return send("#{of}_schema") if primitive_type?(of)
115
- return reference(of) if of.is_a?(Symbol)
116
- return schema_class_to_inline_schema(of) if schema_class?(of)
117
-
118
- raise InvalidArrayTypeError, "Invalid array type: #{of.inspect}. Must be a primitive type (:string, :number, etc.), a symbol reference, a Schema class, or a Schema instance."
119
- end
120
-
121
- def determine_object_reference(of, description = nil)
122
- result = case of
123
- when Symbol
124
- reference(of)
125
- when Class
126
- raise InvalidObjectTypeError, "Invalid object type: #{of.inspect}. Class must inherit from RubyLLM::Schema." unless schema_class?(of)
127
-
128
- schema_class_to_inline_schema(of)
129
-
130
- else
131
- raise InvalidObjectTypeError, "Invalid object type: #{of.inspect}. Must be a symbol reference, a Schema class, or a Schema instance." unless schema_class?(of)
132
-
133
- schema_class_to_inline_schema(of)
134
-
135
- end
136
-
137
- description ? result.merge(description: description) : result
138
- end
139
-
140
- def collect_schemas_from_block(&block)
141
- schemas = []
142
- schema_builder = self
143
-
144
- context = Object.new
145
-
146
- # Dynamically create methods for all schema builders
147
- schema_builder.methods.grep(/_schema$/).each do |schema_method|
148
- type_name = schema_method.to_s.sub(/_schema$/, "")
149
-
150
- context.define_singleton_method(type_name) do |_name = nil, **options, &blk|
151
- schemas << schema_builder.send(schema_method, **options, &blk)
152
- end
153
- end
154
-
155
- # Allow Schema classes to be accessed in the context
156
- context.define_singleton_method(:const_missing) do |name|
157
- const_get(name) if const_defined?(name)
158
- end
159
-
160
- context.instance_eval(&block)
161
- schemas
162
- end
163
-
164
- def schema_class_to_inline_schema(schema_class_or_instance)
165
- # Handle both Schema classes and Schema instances
166
- schema_class = if schema_class_or_instance.is_a?(Class)
167
- schema_class_or_instance
168
- else
169
- schema_class_or_instance.class
170
- end
171
-
172
- # Directly convert schema class to inline object schema
173
- {
174
- type: "object",
175
- properties: schema_class.properties,
176
- required: schema_class.required_properties,
177
- additionalProperties: schema_class.additional_properties
178
- }.tap do |schema|
179
- # For instances, prefer instance description over class description
180
- description = if schema_class_or_instance.is_a?(Class)
181
- schema_class.description
182
- else
183
- schema_class_or_instance.instance_variable_get(:@description) || schema_class.description
184
- end
185
-
186
- schema[:description] = description if description
187
-
188
- merge_conditions(schema, schema_class)
189
- end
190
- end
191
- end
192
- end
193
- end
194
- end
@@ -1,63 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RubyLLM
4
- class Schema
5
- module DSL
6
- module Utilities
7
- # Schema definition and reference methods
8
- def define(name, &)
9
- sub_schema = Class.new(Schema)
10
- sub_schema.class_eval(&)
11
-
12
- schema = {
13
- type: "object",
14
- properties: sub_schema.properties,
15
- required: sub_schema.required_properties,
16
- additionalProperties: sub_schema.additional_properties
17
- }
18
-
19
- merge_conditions(schema, sub_schema)
20
-
21
- definitions[name] = schema
22
- end
23
-
24
- def reference(schema_name)
25
- if schema_name == :root
26
- {"$ref" => "#"}
27
- else
28
- {"$ref" => "#/$defs/#{schema_name}"}
29
- end
30
- end
31
-
32
- private
33
-
34
- def add_property(name, definition, required:, requires: nil)
35
- property_name = name.to_sym
36
-
37
- properties[property_name] = definition
38
- if required
39
- required_properties << property_name unless required_properties.include?(property_name)
40
- else
41
- required_properties.delete(property_name)
42
- end
43
-
44
- if requires
45
- builder = ConditionalBuilder.new
46
- builder.requires(*Array(requires))
47
- dependencies[name.to_s] = builder
48
- end
49
-
50
- nil
51
- end
52
-
53
- def primitive_type?(type)
54
- type.is_a?(Symbol) && PRIMITIVE_TYPES.include?(type)
55
- end
56
-
57
- def schema_class?(type)
58
- (type.is_a?(Class) && type < Schema) || type.is_a?(Schema)
59
- end
60
- end
61
- end
62
- end
63
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "dsl/schema_builders"
4
- require_relative "dsl/primitive_types"
5
- require_relative "dsl/complex_types"
6
- require_relative "dsl/conditionals"
7
- require_relative "dsl/utilities"
8
-
9
- module RubyLLM
10
- class Schema
11
- module DSL
12
- include SchemaBuilders
13
- include PrimitiveTypes
14
- include ComplexTypes
15
- include Conditionals
16
- include Utilities
17
- end
18
- end
19
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RubyLLM
4
- class Schema
5
- # Base error class for all schema-related errors
6
- class Error < StandardError; end
7
-
8
- # Raised when an invalid schema type is specified
9
- class InvalidSchemaTypeError < Error
10
- def initialize(type)
11
- super("Unknown schema type: #{type}")
12
- end
13
- end
14
-
15
- # Raised when an invalid array type is specified
16
- class InvalidArrayTypeError < Error; end
17
-
18
- # Raised when an invalid object type is specified
19
- class InvalidObjectTypeError < Error; end
20
-
21
- # Raised when schema definition is invalid
22
- class InvalidSchemaError < Error; end
23
-
24
- # Raised when schema validation fails
25
- class ValidationError < Error; end
26
-
27
- # Raised when maximum limits are exceeded
28
- class LimitExceededError < Error; end
29
- end
30
- end
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RubyLLM
4
- module Helpers
5
- def schema(name = nil, description: nil, &block)
6
- schema_class = Schema.create(&block)
7
- schema_class.new(name, description: description)
8
- end
9
- end
10
- end
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RubyLLM
4
- class Schema
5
- module JsonOutput
6
- def to_json_schema
7
- validate! # Validate schema before generating JSON
8
-
9
- schema_hash = {
10
- type: "object",
11
- properties: self.class.properties,
12
- required: self.class.required_properties,
13
- additionalProperties: self.class.additional_properties
14
- }
15
-
16
- schema_hash[:strict] = self.class.strict unless self.class.strict.nil?
17
-
18
- # Only include $defs if there are definitions
19
- schema_hash["$defs"] = self.class.definitions unless self.class.definitions.empty?
20
-
21
- self.class.send(:merge_conditions, schema_hash, self.class)
22
-
23
- {
24
- name: @name,
25
- description: @description || self.class.description,
26
- schema: schema_hash
27
- }
28
- end
29
-
30
- def to_json(*_args)
31
- validate! # Validate schema before generating JSON string
32
- JSON.pretty_generate(to_json_schema)
33
- end
34
- end
35
- end
36
- end