Class: Lutaml::Jsonschema::Schema

Inherits:
Base
  • Object
show all
Defined in:
lib/lutaml/jsonschema/base.rb,
lib/lutaml/jsonschema/schema.rb

Overview

Forward declaration for circular references (Schema ↔ PropertyEntry ↔ Link).

Instance Method Summary collapse

Instance Method Details

#all_property_entriesObject



185
186
187
188
189
# File 'lib/lutaml/jsonschema/schema.rb', line 185

def all_property_entries
  result = property_entries.dup
  all_of.each { |s| result.concat(s.all_property_entries) }
  result
end

#array?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/lutaml/jsonschema/schema.rb', line 171

def array?
  types.include?("array")
end

#child_at(segment) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/lutaml/jsonschema/schema.rb', line 191

def child_at(segment)
  case segment
  when "definitions", "$defs" then definition_entries
  when "properties" then property_entries
  when "patternProperties" then pattern_property_entries
  when "items" then items
  when "allOf" then all_of
  when "anyOf" then any_of
  when "oneOf" then one_of
  when "not" then not_schema
  when "if" then if_schema
  when "then" then then_schema
  when "else" then else_schema
  else
    definition_entries.find { |e| e.name == segment }&.schema
  end
end

#each_child {|items, "items"| ... } ⇒ Object

Yields:

  • (items, "items")


209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/lutaml/jsonschema/schema.rb', line 209

def each_child
  return enum_for(:each_child) unless block_given?

  property_entries.each do |e|
    yield e.schema, "properties/#{e.name}" if e.schema
  end
  definition_entries.each do |e|
    yield e.schema, "$defs/#{e.name}" if e.schema
  end
  pattern_property_entries.each do |e|
    yield e.schema, "patternProperties/#{e.name}" if e.schema
  end
  all_of.each_with_index { |s, i| yield s, "allOf[#{i}]" }
  any_of.each_with_index { |s, i| yield s, "anyOf[#{i}]" }
  one_of.each_with_index { |s, i| yield s, "oneOf[#{i}]" }
  yield items, "items" if items
  yield not_schema, "not" if not_schema
  yield if_schema, "if" if if_schema
  yield then_schema, "then" if then_schema
  yield else_schema, "else" if else_schema
  links.each do |link|
    yield link.schema, "link.schema" if link.schema
    yield link.target_schema, "link.target_schema" if link.target_schema
  end
end

#object?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/lutaml/jsonschema/schema.rb', line 167

def object?
  types.include?("object")
end

#parse_additional_properties(instance, value) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/lutaml/jsonschema/schema.rb', line 149

def parse_additional_properties(instance, value)
  case value
  when true, false
    instance.additional_properties = value
  when Hash
    instance.additional_properties_schema = Schema.from_json(value.to_json)
  end
end

#parse_legacy_definitions(instance, value) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/lutaml/jsonschema/schema.rb', line 237

def parse_legacy_definitions(instance, value)
  return unless value.is_a?(Hash)

  value.each do |name, schema_hash|
    next if instance.definition_entries.any? { |e| e.name == name }

    instance.definition_entries.push(
      PropertyEntry.new(
        name: name,
        schema: Schema.from_json(schema_hash.to_json),
      ),
    )
  end
end

#parse_type(instance, value) ⇒ Object



137
138
139
# File 'lib/lutaml/jsonschema/schema.rb', line 137

def parse_type(instance, value)
  instance.type = value.is_a?(Array) ? value.join(",") : value
end

#ref?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/lutaml/jsonschema/schema.rb', line 181

def ref?
  !!dollar_ref
end

#serialize_additional_properties(instance, hash) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/lutaml/jsonschema/schema.rb', line 158

def serialize_additional_properties(instance, hash)
  if instance.additional_properties_schema
    hash["additionalProperties"] =
      JSON.parse(instance.additional_properties_schema.to_json)
  elsif !instance.additional_properties.nil?
    hash["additionalProperties"] = instance.additional_properties
  end
end

#serialize_type(instance, hash) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/lutaml/jsonschema/schema.rb', line 141

def serialize_type(instance, hash)
  t = instance.type
  return unless t

  parts = t.split(",")
  hash["type"] = parts.length == 1 ? parts.first : parts
end

#suppress_output(_instance, _hash) ⇒ Object



235
# File 'lib/lutaml/jsonschema/schema.rb', line 235

def suppress_output(_instance, _hash); end

#typesObject



175
176
177
178
179
# File 'lib/lutaml/jsonschema/schema.rb', line 175

def types
  return [] unless type

  type.split(",")
end