lib/json_schema/schema.rb in json_schema-0.17.2 vs lib/json_schema/schema.rb in json_schema-0.18.0
- old
+ new
@@ -1,9 +1,19 @@
require "json"
module JsonSchema
class Schema
+ TYPE_MAP = {
+ "array" => Array,
+ "boolean" => [FalseClass, TrueClass],
+ "integer" => Integer,
+ "number" => [Integer, Float],
+ "null" => NilClass,
+ "object" => Hash,
+ "string" => String,
+ }
+
include Attributes
def initialize
# nil out all our fields so that it's possible to instantiate a schema
# instance without going through the parser and validate against it
@@ -127,11 +137,11 @@
# An array of types that data is allowed to be. The spec allows this to be
# a string as well, but the parser will always normalize this to an array
# of strings.
#
# Type: Array[String]
- attr_schema :type, :default => []
+ attr_schema :type, :default => [], :clear_cache => :@type_parsed
# validation: array
attr_schema :additional_items, :default => true, :schema_name => :additionalItems
attr_schema :items
attr_schema :max_items, :schema_name => :maxItems
@@ -195,9 +205,17 @@
end
def expand_references!(options = {})
ReferenceExpander.new.expand!(self, options)
true
+ end
+
+ # An array of Ruby classes that are equivalent to the types defined in the
+ # schema.
+ #
+ # Type: Array[Class]
+ def type_parsed
+ @type_parsed ||= type.flat_map { |t| TYPE_MAP[t] }.compact
end
def inspect
"\#<JsonSchema::Schema pointer=#{pointer}>"
end