Sha256: 709915fe290b28dbf55da5510b550c2edd3729a6367f269f03e5ef8a00b0b5e5

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 KB

Contents

require 'json-schema/errors/validation_error'

module JSON
  class Schema
    class Attribute
      def self.validate(current_schema, data, fragments, processor, validator, options = {})
      end

      def self.build_fragment(fragments)
        "#/#{fragments.join('/')}"
      end

      def self.validation_error(processor, message, fragments, current_schema, failed_attribute, record_errors)
        error = ValidationError.new(message, fragments, failed_attribute, current_schema)
        if record_errors
          processor.validation_error(error)
        else
          raise error
        end
      end

      def self.validation_errors(validator)
        validator.validation_errors
      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

      # Lookup Schema type of given class instance
      def self.type_of_data(data)
        type, _ = TYPE_CLASS_MAPPINGS.map { |k,v| [k,v] }.sort_by { |(_, v)|
          -Array(v).map { |klass| klass.ancestors.size }.max
        }.find { |(_, v)|
          Array(v).any? { |klass| data.kind_of?(klass) }
        }
        type
      end

      def self.validate_on_false?
        true
      end
    end
  end
end

Version data entries

4 entries across 3 versions & 2 rubygems

Version Path
json-schema-ouidou-2.9.1 lib/json-schema/attribute.rb
json-schema-ouidou-2.9.0 lib/json-schema/attribute.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/bundler/gems/json-schema-2253a5ee6679/lib/json-schema/attribute.rb
mountapi-0.11.1 vendor/json-schema/lib/json-schema/attribute.rb