class JsonSchema::Draft07
Attributes
options[R]
parsed_json[R]
raw_json[R]
Public Class Methods
new(raw_json, options = {})
click to toggle source
# File lib/json_schema/draft07.rb, line 7 def initialize(raw_json, options = {}) @raw_json = raw_json @options = options @parsed_json = parse(raw_json) end
Public Instance Methods
generate()
click to toggle source
# File lib/json_schema/draft07.rb, line 13 def generate raise StandardError, 'Invalid Input. Input should in the valid JSON format' unless parsed_json { 'title' => ROOT_TITLE, 'description' => ROOT_DESCRIPTION, "$schema" => "http://json-schema.org/draft-07/schema" }.merge(schema_generate(parsed_json)) end
Private Instance Methods
datatype(value)
click to toggle source
# File lib/json_schema/draft07.rb, line 68 def datatype(value) case value when TrueClass, FalseClass 'boolean' when Float 'number' when Hash 'object' when String, Integer, Array value.class.to_s.downcase when NilClass 'null' else raise Error, "Invalid type #{value.class}" end end
generate_array(array)
click to toggle source
# File lib/json_schema/draft07.rb, line 50 def generate_array(array) params = { 'type' => 'array', 'items' => {}, } case @options[:array_validation] when :any_of params['items']['anyOf'] = [] array.each_with_object(params) do |elem, hsh| hsh['items']['anyOf'].push(schema_generate(elem)) end when :first_element array.values_at(0).each_with_object(params) do |elem, hsh| hsh['items'].merge!(schema_generate(elem)) end end end
generate_object(object)
click to toggle source
# File lib/json_schema/draft07.rb, line 39 def generate_object(object) params = { 'type' => 'object', 'required' => object.keys, 'properties' => {} } object.each_with_object(params) do |(key, value), hsh| hsh['properties'].merge!({key => schema_generate(value)}) end end
parse(json)
click to toggle source
# File lib/json_schema/draft07.rb, line 22 def parse(json) JSON.parse(json) rescue StandardError nil end
schema_generate(input)
click to toggle source
# File lib/json_schema/draft07.rb, line 28 def schema_generate(input) case input when Hash generate_object(input) when Array generate_array(input) else {'type' => datatype(input)} end end