Sha256: 41b5eac79906fd385cfa94caa082b45f5a7374204d37ad71bf66efcf8b0ccb58

Contents?: true

Size: 1.74 KB

Versions: 20

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module JSI
  module Schema::Validation::Type
    # @private
    def internal_validate_type(result_builder)
      if keyword?('type')
        value = schema_content['type']
        instance = result_builder.instance
        # The value of this keyword MUST be either a string or an array. If it is an array, elements of
        # the array MUST be strings and MUST be unique.
        if value.respond_to?(:to_str) || value.respond_to?(:to_ary)
          types = value.respond_to?(:to_str) ? [value] : value
          matched_type = types.each_with_index.any? do |type, i|
            if type.respond_to?(:to_str)
              case type.to_str
              when 'null'
                instance == nil
              when 'boolean'
                instance == true || instance == false
              when 'object'
                instance.respond_to?(:to_hash)
              when 'array'
                instance.respond_to?(:to_ary)
              when 'string'
                instance.respond_to?(:to_str)
              when 'number'
                instance.is_a?(Numeric)
              when 'integer'
                internal_integer?(instance)
              else
                result_builder.schema_error("`type` is not one of: null, boolean, object, array, string, number, or integer", 'type')
              end
            else
              result_builder.schema_error("`type` is not a string at index #{i}", 'type')
            end
          end
          result_builder.validate(
            matched_type,
            'instance type does not match `type` value',
            keyword: 'type',
          )
        else
          result_builder.schema_error('`type` is not a string or array', 'type')
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
jsi-0.8.1 lib/jsi/schema/validation/type.rb
jsi-0.8.0 lib/jsi/schema/validation/type.rb
jsi-dev-0.0.4 lib/jsi/schema/validation/type.rb
jsi-dev-0.0.5 lib/jsi/schema/validation/type.rb
jsi-dev-0.0.6 lib/jsi/schema/validation/type.rb
jsi-dev-0.0.7 lib/jsi/schema/validation/type.rb
jsi-dev-0.0.8 lib/jsi/schema/validation/type.rb
jsi-dev-0.0.8.pre.maruku lib/jsi/schema/validation/type.rb
jsi-dev-0.0.7.pre.rdiscount lib/jsi/schema/validation/type.rb
jsi-dev-0.0.6.pre.redcarpet lib/jsi/schema/validation/type.rb
jsi-dev-0.0.5.pre.kramdown lib/jsi/schema/validation/type.rb
jsi-dev-0.0.4.pre.commonmarker lib/jsi/schema/validation/type.rb
jsi-dev-0.0.0.pre.commonmarker lib/jsi/schema/validation/type.rb
jsi-dev-0.0.0.pre.kramdown lib/jsi/schema/validation/type.rb
jsi-dev-0.0.0.pre.redcarpet lib/jsi/schema/validation/type.rb
jsi-dev-0.0.0.pre.rdiscount lib/jsi/schema/validation/type.rb
jsi-dev-0.0.0.pre.maruku lib/jsi/schema/validation/type.rb
jsi-dev-0.0.3 lib/jsi/schema/validation/type.rb
jsi-dev-0.0.2 lib/jsi/schema/validation/type.rb
jsi-0.7.0 lib/jsi/schema/validation/type.rb