Module: Croods::Resource::JsonSchema::Definition

Defined in:
lib/croods/resource/json_schema/definition.rb

Constant Summary collapse

TYPES =
{
  datetime: ['string'],
  date: ['string'],
  text: ['string'],
  json: %w[object array],
  jsonb: %w[object array],
  float: ['number']
}.freeze

Class Method Summary collapse

Class Method Details

.converted_types(type) ⇒ Object



52
53
54
# File 'lib/croods/resource/json_schema/definition.rb', line 52

def converted_types(type)
  TYPES[type] || [type.to_s]
end

.format(attribute) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/croods/resource/json_schema/definition.rb', line 29

def format(attribute)
  case attribute.type
  when :datetime
    { format: 'date-time' }
  when :date
    { format: 'date' }
  else
    {}
  end
end

.items(attribute) ⇒ Object



23
24
25
26
27
# File 'lib/croods/resource/json_schema/definition.rb', line 23

def items(attribute)
  return {} unless %i[json jsonb].include?(attribute.type)

  { items: { type: %w[string number object] } }
end

.schema(attribute) ⇒ Object



17
18
19
20
21
# File 'lib/croods/resource/json_schema/definition.rb', line 17

def schema(attribute)
  { type: types(attribute) }
    .merge(format(attribute))
    .merge(items(attribute))
end

.types(attribute) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/croods/resource/json_schema/definition.rb', line 40

def types(attribute)
  types = []
  converted_types(attribute.type).each do |converted_type|
    types << converted_type
  end
  null = (
    attribute.null || attribute.default || attribute.default_function
  )
  types << 'null' if null
  types
end