Class: Meta::JsonSchema::DynamicSchema

Inherits:
BaseSchema
  • Object
show all
Defined in:
lib/meta/json_schema/schemas/dynamic_schema.rb

Instance Attribute Summary

Attributes inherited from BaseSchema

#options

Instance Method Summary collapse

Methods inherited from BaseSchema

#defined_scopes, #filter?, #find_schema, #if?, #scoped, #staged, #to_schema, #value?

Constructor Details

#initialize(resolver, one_of: nil, **base_options) ⇒ DynamicSchema

Returns a new instance of DynamicSchema.



6
7
8
9
10
11
# File 'lib/meta/json_schema/schemas/dynamic_schema.rb', line 6

def initialize(resolver, one_of: nil, **base_options)
  super(base_options)

  @resolver = resolver
  @one_of = one_of
end

Instance Method Details

#filter(value, user_options = {}) ⇒ Object



13
14
15
16
17
# File 'lib/meta/json_schema/schemas/dynamic_schema.rb', line 13

def filter(value, user_options = {})
  value = super(value, user_options)
  schema = @resolver.call(value).to_schema
  schema.filter(value, user_options)
end

#to_schema_doc(user_options) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/meta/json_schema/schemas/dynamic_schema.rb', line 19

def to_schema_doc(user_options)
  schema = { type: 'object' }
  schema[:oneOf] = @one_of.map do |schema|
    schema.to_schema_doc(user_options)
  end if @one_of

  schema
end