class RSchema::Schemas::Type

A schema that matches values of a given type (i.e. `value.is_a?(type)`)

@example An Integer schema

schema = RSchema.define { _Integer }
schema.valid?(5) #=> true

@example A namespaced type

schema = RSchema.define do
  # This will not work:
  # _ActiveWhatever::Thing

  # This will work:
  type(ActiveWhatever::Thing)
end

Attributes

type[R]

Public Class Methods

new(type) click to toggle source
# File lib/rschema/schemas/type.rb, line 24
def initialize(type)
  @type = type
end

Public Instance Methods

call(value, _options) click to toggle source
# File lib/rschema/schemas/type.rb, line 28
def call(value, _options)
  if value.is_a?(@type)
    Result.success(value)
  else
    Result.failure(error(value))
  end
end
with_wrapped_subschemas(_wrapper) click to toggle source
# File lib/rschema/schemas/type.rb, line 36
def with_wrapped_subschemas(_wrapper)
  self
end

Private Instance Methods

error(value) click to toggle source
# File lib/rschema/schemas/type.rb, line 42
def error(value)
  Error.new(
    schema: self,
    value: value,
    symbolic_name: :wrong_type,
  )
end