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
-
#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
[View source]
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/raif/json_schema_builder.rb', line 51 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
[View source]
25 26 27 |
# File 'lib/raif/json_schema_builder.rb', line 25 def boolean(name, = {}) add_property(name, "boolean", ) end |
#integer(name, options = {}) ⇒ Object
[View source]
17 18 19 |
# File 'lib/raif/json_schema_builder.rb', line 17 def integer(name, = {}) add_property(name, "integer", ) end |
#items(options = {}) ⇒ Object
Allow setting array items directly
79 80 81 |
# File 'lib/raif/json_schema_builder.rb', line 79 def items( = {}) @items_schema = end |
#number(name, options = {}) ⇒ Object
[View source]
21 22 23 |
# File 'lib/raif/json_schema_builder.rb', line 21 def number(name, = {}) add_property(name, "number", ) end |
#object(name = nil, options = {}, &block) ⇒ Object
[View source]
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/raif/json_schema_builder.rb', line 29 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
[View source]
13 14 15 |
# File 'lib/raif/json_schema_builder.rb', line 13 def string(name, = {}) add_property(name, "string", ) end |
#to_schema ⇒ Object
[View source]
83 84 85 86 87 88 89 90 |
# File 'lib/raif/json_schema_builder.rb', line 83 def to_schema { type: "object", additionalProperties: false, properties: @properties, required: @required_properties } end |