Class: SimpleJSONSchema::Validators::Number

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

Constant Summary collapse

FLOAT_FORMAT =
/\A-?\d+\.\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/number.rb', line 17

def casting(value)
  BigDecimal(value) if value.is_a?(::String) && FLOAT_FORMAT.match?(value)
end

#validate(scope) ⇒ Object



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

def validate(scope)
  value = scope.value

  return scope.error(:number) unless value.is_a?(::Numeric)
  return scope.error(:zero_not_allowed) if scope[:not_zero] == true && value.zero?

  super
end