class Siren::JSONSchema::SchemaBuilder

Public Class Methods

new(&block) click to toggle source
# 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

Public Instance Methods

any(*options) click to toggle source
# File lib/siren/schemabuilder.rb, line 50
def any (*options)
  { anyOf: options }
end
array(itemType, min: nil, max: nil, uniq: nil, extra: nil) click to toggle source
# 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
bool() click to toggle source
# File lib/siren/schemabuilder.rb, line 21
def bool ()
  { type: "boolean" }
end
Also aliased as: boolean
boolean()
Alias for: bool
hash(propType) click to toggle source
# 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) click to toggle source
# 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
null() click to toggle source
# File lib/siren/schemabuilder.rb, line 27
def null ()
  { type: "null" }
end
number(min: nil, max: nil, excl: nil, mul: nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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