Class: Schemaful::Schema::Type::Any

Inherits:
Base
  • Object
show all
Defined in:
lib/schemaful/schema/type/any.rb

Overview

This is a base class for all Types. A Type class has an associated Ruby type and is responsible for validation of values accorging to that type.

This class represents the "wildcard" type and accepts every value as valid.

Subclasses should implement #on_validate hook to provide custom validation according to their type.

Examples:

Basic usage

any = Schemaful::Schema::Type::Any.new
any.validate('any value, literally!') #=> nil

Specify a validator using the keyword argument

any = Schemaful::Schema::Type::Any.new(validator: Integer)
any.validate(2) #=> nil
any.validate('invalid') #=> raise Schemaful::ValidationError

Specify a validator using #validator

any = Schemaful::Schema::Type::Any.new

# Any accepts any value by default:
any.validate('string') #=> nil

# check if a value is Integer:
any.validator(Integer)
any.validate('string') #=> raise Schemaful::ValidationError
any.validate(1) #=> nil

# check if a value is even:
any.validator(:even?)
any.validate(1) #=> raise Schemaful::ValidationError
any.validate(2) #=> nil

Defining a subclass

class StringType < Schemaful::Schema::Type::Any
  type String
end

StringType.type #=> String
str = StringType.new
str.validate(42) #=> raise Schemaful::ValidationError
str.validate('42') #=> nil
# restrict strings to ASCII charset
str.validator(:ascii_only?)
str.validate('строка') #=> raise Schemaful::ValidationError

Direct Known Subclasses

Numeric

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(validator: nil) ⇒ Any

A new instance of Any.

To restrict accepted values, you should provide additional validators using the keyword parameter validator: or #validator.


86
87
88
89
90
# File 'lib/schemaful/schema/type/any.rb', line 86

def initialize(validator: nil)
  super()
  validator ||= []      # rbx fix
  Array(validator).each { |v| validator(v) }
end

Class Method Details

.typeClass .type(value) ⇒ Object

Overloads:

  • .type(value) ⇒ Object

    Set the type associated with the class.


61
62
63
64
65
66
67
68
# File 'lib/schemaful/schema/type/any.rb', line 61

def self.type(value = nil)
  if value.nil?
    @type
  else
    @type = value
    nil
  end
end

Instance Method Details

#on_validate(value) ⇒ Boolean

Check if a value is valid.

This is a hook which is meant to be overriden by subclasses. Default implementation checks if a value is an instance of type (with regard to inheritance).


140
141
142
# File 'lib/schemaful/schema/type/any.rb', line 140

def on_validate(value)
  value.is_a?(self.class.type)
end

#validate(value) ⇒ Object

Check if a value is valid.

This method invokes (#on_validate) hook and each one of (#validators), in order. If any of these validators return a false or nil, the value is considered invalid, and ValidationError is raised.

Raises:


124
125
126
127
128
129
130
# File 'lib/schemaful/schema/type/any.rb', line 124

def validate(value)
  raise ValidationError unless on_validate(value)
  validators.each do |validator|
    raise ValidationError unless validator.to_proc.call(value)
  end
  nil
end

#validator(validator) ⇒ self

Convert a validator to a callable and add it to the list of validators.

Acceptable validator types are:

  • #call - any callable which return true or false.
  • Class - the value should be an instance of that class (with regard to inheritance).
  • Symbol - converted to proc.

108
109
110
111
112
113
114
115
# File 'lib/schemaful/schema/type/any.rb', line 108

def validator(validator)
  validators << case validator
                when Symbol then validator.to_proc
                when Class then ->(v) { v.is_a?(validator) }
                else validator
                end
  self
end

#validatorsArray<#call>


93
94
95
# File 'lib/schemaful/schema/type/any.rb', line 93

def validators
  @validators ||= []
end