Class: Schemaful::Schema::Type::Numeric

Inherits:
Any
  • Object
show all
Defined in:
lib/schemaful/schema/type/numeric.rb

Overview

Numeric validation type.

Examples:

Range validator

numeric = Schemaful::Schema::Type::Numeric.new
numeric.validate(42) #=> nil
numeric.validate(-42) #=> nil
numeric.validator(0..Float::INFINITY)
numeric.validate(-42) #=> raise Schemaful::ValidationError

Instance Method Summary collapse

Methods inherited from Any

#initialize, #on_validate, type, #validate, #validators

Methods inherited from Base

#validate

Constructor Details

This class inherits a constructor from Schemaful::Schema::Type::Any

Instance Method Details

#validator(validator) ⇒ self

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

In additional to the validators, introduced in Any#validator, the following types are accepted:

  • Range - the value should lie in that range.

26
27
28
29
# File 'lib/schemaful/schema/type/numeric.rb', line 26

def validator(validator)
  callable = ->(v) { validator.include?(v) } if validator.is_a?(Range)
  super(callable)
end