Sha256: 317a616934c4fbca89ab104ed4cc802c83776d6f284b011185054c411e70f7f3

Contents?: true

Size: 1.88 KB

Versions: 22

Compression:

Stored size: 1.88 KB

Contents

module JSON
  class Schema
    class TypeV4Attribute < Attribute
      def self.validate(current_schema, data, fragments, processor, validator, options = {})
        union = true
        types = current_schema.schema['type']
        if !types.is_a?(Array)
          types = [types]
          union = false
        end
        valid = false

        # Create an array to hold errors that are generated during union validation
        union_errors = []

        types.each do |type|
          valid = data_valid_for_type?(data, type)
          break if valid
        end

        if !valid
          if union
            message = "The property '#{build_fragment(fragments)}' of type #{data.class} did not match one or more of the following types:"
            types.each {|type| message += type.is_a?(String) ? " #{type}," : " (schema)," }
            message.chop!
            validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
            validation_errors(processor).last.sub_errors = union_errors
          else
            message = "The property '#{build_fragment(fragments)}' of type #{data.class} did not match the following type:"
            types.each {|type| message += type.is_a?(String) ? " #{type}," : " (schema)," }
            message.chop!
            validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
          end
        end
      end

      TYPE_CLASS_MAPPINGS = {
        "string" => String,
        "number" => Numeric,
        "integer" => Integer,
        "boolean" => [TrueClass, FalseClass],
        "object" => Hash,
        "array" => Array,
        "null" => NilClass,
        "any" => Object
      }

      def self.data_valid_for_type?(data, type)
        valid_classes = TYPE_CLASS_MAPPINGS.fetch(type) { return true }
        Array(valid_classes).any? { |c| data.is_a?(c) }
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
json-schema-2.2.5 lib/json-schema/attributes/type_v4.rb
json-schema-2.2.4 lib/json-schema/attributes/type_v4.rb
json-schema-2.2.3 lib/json-schema/attributes/type_v4.rb
json-schema-2.2.2 lib/json-schema/attributes/type_v4.rb
json-schema-2.2.1 lib/json-schema/attributes/type_v4.rb
json-schema-2.2.0 lib/json-schema/attributes/type_v4.rb
json-schema-2.1.9 lib/json-schema/attributes/type_v4.rb
json-schema-2.1.8 lib/json-schema/attributes/type_v4.rb
json-schema-2.1.7 lib/json-schema/attributes/type_v4.rb
json-schema-2.1.6 lib/json-schema/attributes/type_v4.rb
json-schema-2.1.5 lib/json-schema/attributes/type_v4.rb
json-schema-2.1.4 lib/json-schema/attributes/type_v4.rb
json-schema-2.1.3 lib/json-schema/attributes/type_v4.rb
json-schema-2.1.2 lib/json-schema/attributes/type_v4.rb
json-schema-2.1.1 lib/json-schema/attributes/type_v4.rb
json-schema-2.1.0 lib/json-schema/attributes/type_v4.rb
json-schema-2.0.5 lib/json-schema/attributes/type_v4.rb
json-schema-2.0.4 lib/json-schema/attributes/type_v4.rb
json-schema-2.0.3 lib/json-schema/attributes/type_v4.rb
json-schema-2.0.2 lib/json-schema/attributes/type_v4.rb