class Randrizer::Drivers::JSONSchema::Typegen

Constants

DATE_SEQUENCE
DAY_INT_TYPE
EMAIL_SEQUENCE
HOUR_INT_TYPE
MIN_SEC_INT_TYPE
MONTH_INT_TYPE
TIMEZONE_SEQUENCE
TIME_SEQUENCE

Public Class Methods

t_boolean(_attrs) click to toggle source
# File lib/randrizer/drivers/json_schema/typegen.rb, line 58
def t_boolean(_attrs)
  Types::Bool[]
end
t_integer(attrs) click to toggle source
# File lib/randrizer/drivers/json_schema/typegen.rb, line 75
def t_integer(attrs)
  t_number(attrs)
end
t_null(_attrs) click to toggle source
# File lib/randrizer/drivers/json_schema/typegen.rb, line 52
def t_null(_attrs)
  # `Types::Nullable` would require an inner type, as this will never change
  # we can just return a constant null value.
  Types::Const[nil]
end
t_number(attrs) click to toggle source
# File lib/randrizer/drivers/json_schema/typegen.rb, line 62
def t_number(attrs)
  # TODO: support multipleOf

  restrictions = {}
  restrictions[:min] = attrs.fetch("minimum", -999_999_999)
  restrictions[:max] = attrs.fetch("maximum", 999_999_999)

  restrictions[:min] += 1 if attrs.fetch("exclusiveMinimum", false)
  restrictions[:max] -= 1 if attrs.fetch("exclusiveMaximum", false)

  Types::Int.build(**restrictions)
end
t_string(attrs) click to toggle source
# File lib/randrizer/drivers/json_schema/typegen.rb, line 100
def t_string(attrs)
  # TODO: support String patterns format specs

  if attrs.include?("enum")
    return Types::OneOf[
      attrs["enum"].map { |e| Types::Const[e] }
    ]
  end

  if attrs.include?("format")
    return t_string_with_format(attrs, attrs["format"])
  end

  props = {}
  props[:min_length] = attrs["minLength"].to_i if attrs.include?("minLength")
  props[:max_length] = attrs["maxLength"].to_i if attrs.include?("maxLength")

  Types::String.build(**props)
end
t_string_with_format(_attrs, format) click to toggle source
# File lib/randrizer/drivers/json_schema/typegen.rb, line 79
def t_string_with_format(_attrs, format)
  # https://json-schema.org/understanding-json-schema/reference/string.html#built-in-formats

  case format
  when "date"
    Types::StringSequence.build(DATE_SEQUENCE)
  when "time"
    Types::StringSequence.build(TIME_SEQUENCE)
  when "date-time"
    Types::StringSequence.build([
      DATE_SEQUENCE,
      Types::Const["T"],
      TIME_SEQUENCE
    ].flatten)
  when "email"
    Types::StringSequence.build(EMAIL_SEQUENCE)
  else
    raise "Format not supported: #{format}"
  end
end