Class: Siren::JSONSchema::SchemaBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/siren/schemabuilder.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ SchemaBuilder

Returns a new instance of SchemaBuilder.



6
7
8
9
10
11
# File 'lib/siren/schemabuilder.rb', line 6

def initialize (&block)
  # @context = { type: "object", additionalProperties: false, properties: {} }
  singleton_class.class_exec(&block)
  schema = build()
  puts JSON.parse(schema.to_json).to_yaml
end

Instance Method Details

#any(*options) ⇒ Object



50
51
52
# File 'lib/siren/schemabuilder.rb', line 50

def any (*options)
  { anyOf: options }
end

#array(itemType, min: nil, max: nil, uniq: nil, extra: nil) ⇒ Object



38
39
40
# File 'lib/siren/schemabuilder.rb', line 38

def array (itemType, min: nil, max: nil, uniq: nil, extra: nil)
  { type: "array", items: itemType, minItems: min, maxItems: max, uniqueItems: uniq, additionalItems: extra}.compact
end

#boolObject Also known as: boolean



21
22
23
# File 'lib/siren/schemabuilder.rb', line 21

def bool ()
  { type: "boolean" }
end

#hash(propType) ⇒ Object



42
43
44
# File 'lib/siren/schemabuilder.rb', line 42

def hash (propType)
  { type: "object", additionalProperties: propType }.compact
end

#integer(min: nil, max: nil, excl: nil, mul: nil) ⇒ Object



17
18
19
# File 'lib/siren/schemabuilder.rb', line 17

def integer (min: nil, max: nil, excl: nil, mul: nil)
  { type: "integer", minimum: min, maximum: max, exclusiveMaximum: excl, multipleOf: mul }.compact
end

#nullObject



27
28
29
# File 'lib/siren/schemabuilder.rb', line 27

def null ()
  { type: "null" }
end

#number(min: nil, max: nil, excl: nil, mul: nil) ⇒ Object



13
14
15
# File 'lib/siren/schemabuilder.rb', line 13

def number (min: nil, max: nil, excl: nil, mul: nil)
  { type: "number", minimum: min, maximum: max, exclusiveMaximum: excl, multipleOf: mul }.compact
end

#object(props, extra: false, required: nil) ⇒ Object



46
47
48
# File 'lib/siren/schemabuilder.rb', line 46

def object (props, extra: false, required: nil)
  { type: "object", additionalProperties: extra, required: required, properties: props }.compact
end

#string(pattern = nil, min: nil, max: nil) ⇒ Object



31
32
33
34
35
36
# File 'lib/siren/schemabuilder.rb', line 31

def string (pattern = nil, min: nil, max: nil)
  pattern = Regexp.escape(pattern) if pattern.is_a?(String)
  pattern = Regexp.new(pattern.map{|p|Regexp.escape(p)}.join("|")) if pattern.is_a?(Array)
  pattern = pattern.inspect[1..-2] if pattern.is_a?(Regexp)
  { type: "string", minLength: min, maxLength: max, pattern: pattern }.compact
end