Module: JSON::SchemaBuilder::Helpers

Included in:
Entity
Defined in:
lib/json/schema_builder/helpers.rb

Instance Method Summary collapse

Instance Method Details

#empty_string(name = nil, opts = { }, &block) ⇒ Object



39
40
41
# File 'lib/json/schema_builder/helpers.rb', line 39

def empty_string(name = nil, opts = { }, &block)
  string name, opts.merge(max_length: 0), &block
end

#id(name, opts = { }, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/json/schema_builder/helpers.rb', line 4

def id(name, opts = { }, &block)
  nullable = opts.delete :null
  entity name, opts do |child|
    types = [
      integer(minimum: 1),
      string(pattern: '^[1-9]\d*$')
    ]
    types << null if nullable
    one_of types
    child.eval_block &block
  end
end

#object_or_array(*args, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/json/schema_builder/helpers.rb', line 17

def object_or_array(*args, &block)
  opts = args.extract_options!
  name = args.shift
  nullable = opts.delete :null
  any_ofs = opts.delete :any_of

  entity name do
    list = [
      object(*args, opts) { |child| child.eval_block(&block) },
      array {
        items {
          object(*args, opts) { |child| child.eval_block(&block) }
        }
      }
    ]

    list |= any_ofs if any_ofs
    list |= [null] if nullable && list.none? { |item| item.is_a?(Null) }
    any_of list
  end
end