Class: Respect::NumericSchema

Inherits:
Schema
  • Object
show all
Includes:
HasConstraints
Defined in:
lib/respect/numeric_schema.rb

Direct Known Subclasses

FloatSchema, IntegerSchema, UTCTimeSchema

Instance Attribute Summary

Attributes inherited from Schema

#last_error, #options, #sanitized_object

Instance Method Summary (collapse)

Methods included from HasConstraints

#validate, #validate_constraints

Methods inherited from Schema

#==, #allow_nil?, def_class, def_class_name, #default, default_options, define, #documentation, #documented?, #has_default?, #initialize, #initialize_copy, #inspect, #non_default_options, #optional?, #required?, #sanitize!, #sanitize_object!, statement_name, #to_h, #to_json, #to_s, #validate, #validate!, #validate?

Methods included from DocHelper

#description, #title

Constructor Details

This class inherits a constructor from Respect::Schema

Instance Method Details

- (Object) validate_type(object)



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/respect/numeric_schema.rb', line 7

def validate_type(object)
  case object
  when String
    if match_data = /^[-+]?\d+(\.\d+)?$/.match(object)
      if match_data[1]
        object.to_f
      else
        object.to_i
      end
    else
      raise ValidationError, "malformed numeric value: `#{object}'"
    end
  when Integer, Float
    object
  when NilClass
    if allow_nil?
      nil
    else
      raise ValidationError, "object is nil but this #{self.class} does not allow nil"
    end
  else
    raise ValidationError, "object is not a numeric but a '#{object.class}'"
  end
end