Class: Raif::JsonSchemaBuilder
- Inherits:
-
Object
- Object
- Raif::JsonSchemaBuilder
- Defined in:
- lib/raif/json_schema_builder.rb
Instance Attribute Summary collapse
-
#items_schema ⇒ Object
readonly
Returns the value of attribute items_schema.
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#required_properties ⇒ Object
readonly
Returns the value of attribute required_properties.
Instance Method Summary collapse
- #array(name, options = {}, &block) ⇒ Object
- #boolean(name, options = {}) ⇒ Object
-
#build_with_instance(instance, &block) ⇒ JsonSchemaBuilder
Build schema with instance context for instance-dependent schemas The block receives the instance as a parameter and has access to the builder methods.
-
#initialize ⇒ JsonSchemaBuilder
constructor
A new instance of JsonSchemaBuilder.
- #integer(name, options = {}) ⇒ Object
-
#items(options = {}) ⇒ Object
Allow setting array items directly.
- #number(name, options = {}) ⇒ Object
- #object(name = nil, options = {}, &block) ⇒ Object
- #string(name, options = {}) ⇒ Object
- #to_schema ⇒ Object
Constructor Details
#initialize ⇒ JsonSchemaBuilder
Returns a new instance of JsonSchemaBuilder.
7 8 9 10 11 |
# File 'lib/raif/json_schema_builder.rb', line 7 def initialize @properties = {} @required_properties = [] @items_schema = nil end |
Instance Attribute Details
#items_schema ⇒ Object (readonly)
Returns the value of attribute items_schema.
5 6 7 |
# File 'lib/raif/json_schema_builder.rb', line 5 def items_schema @items_schema end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
5 6 7 |
# File 'lib/raif/json_schema_builder.rb', line 5 def properties @properties end |
#required_properties ⇒ Object (readonly)
Returns the value of attribute required_properties.
5 6 7 |
# File 'lib/raif/json_schema_builder.rb', line 5 def required_properties @required_properties end |
Instance Method Details
#array(name, options = {}, &block) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/raif/json_schema_builder.rb', line 65 def array(name, = {}, &block) items_schema = .delete(:items) || {} if block_given? nested_builder = self.class.new nested_builder.instance_eval(&block) # If items were directly set using the items method if nested_builder.items_schema.present? items_schema = nested_builder.items_schema # If there are properties defined, it's an object schema elsif nested_builder.properties.any? items_schema = { type: "object", properties: nested_builder.properties, additionalProperties: false } # We currently use strict mode, which means that all properties are required items_schema[:required] = nested_builder.required_properties end end [:items] = items_schema unless items_schema.empty? add_property(name, "array", ) end |
#boolean(name, options = {}) ⇒ Object
39 40 41 |
# File 'lib/raif/json_schema_builder.rb', line 39 def boolean(name, = {}) add_property(name, "boolean", ) end |
#build_with_instance(instance, &block) ⇒ JsonSchemaBuilder
Build schema with instance context for instance-dependent schemas The block receives the instance as a parameter and has access to the builder methods
19 20 21 22 23 24 25 |
# File 'lib/raif/json_schema_builder.rb', line 19 def build_with_instance(instance, &block) # Evaluate the block in the context of the builder, passing the instance as parameter # This allows the block to use both builder methods (string, integer, etc.) # and access the instance parameter for conditional logic instance_exec(instance, &block) self end |
#integer(name, options = {}) ⇒ Object
31 32 33 |
# File 'lib/raif/json_schema_builder.rb', line 31 def integer(name, = {}) add_property(name, "integer", ) end |
#items(options = {}) ⇒ Object
Allow setting array items directly
93 94 95 |
# File 'lib/raif/json_schema_builder.rb', line 93 def items( = {}) @items_schema = end |
#number(name, options = {}) ⇒ Object
35 36 37 |
# File 'lib/raif/json_schema_builder.rb', line 35 def number(name, = {}) add_property(name, "number", ) end |
#object(name = nil, options = {}, &block) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/raif/json_schema_builder.rb', line 43 def object(name = nil, = {}, &block) schema = {} if block_given? nested_builder = self.class.new nested_builder.instance_eval(&block) schema[:properties] = nested_builder.properties schema[:additionalProperties] = false # We currently use strict mode, which means that all properties are required schema[:required] = nested_builder.required_properties end # If name is nil, we're inside an array and should return the schema directly if name.nil? @items_schema = { type: "object" }.merge(schema) else add_property(name, "object", .merge(schema)) end end |
#string(name, options = {}) ⇒ Object
27 28 29 |
# File 'lib/raif/json_schema_builder.rb', line 27 def string(name, = {}) add_property(name, "string", ) end |
#to_schema ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/raif/json_schema_builder.rb', line 97 def to_schema { type: "object", additionalProperties: false, properties: @properties, required: @required_properties } end |