Class: SimpleJSONSchema::Validators::Integer

Inherits:
Numeric
  • Object
show all
Defined in:
lib/simple_json_schema/validators/integer.rb

Constant Summary collapse

INTEGER_FORMAT =
/\A-?\d+\Z/.freeze

Instance Method Summary collapse

Methods inherited from Base

#valid

Instance Method Details

#casting(value) ⇒ Object



17
18
19
# File 'lib/simple_json_schema/validators/integer.rb', line 17

def casting(value)
  value.to_i if value.is_a?(::String) && INTEGER_FORMAT.match?(value)
end

#validate(scope) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/simple_json_schema/validators/integer.rb', line 8

def validate(scope)
  value = scope.value

  return scope.error(:integer) if !value.is_a?(::Numeric) || (!value.is_a?(::Integer) && value.floor != value)
  return scope.error(:zero_not_allowed) if scope[:not_zero] == true && value.zero?

  super
end