Sha256: 3ef025d3e24ece97231af69f89762e2979856c24c1c4ec9cf3fe13596379e0f4

Contents?: true

Size: 792 Bytes

Versions: 1

Compression:

Stored size: 792 Bytes

Contents

require 'bigdecimal'

module OpenActive
  module Validators
    class NumberValidator < BaseValidator
      # Coerce given value to the type the validator is validating against.
      # PLEASE NOTE: no checks are performed on the given value.
      # It is therefore recommended to call the "run" method first before this.
      #
      # @param value [mixed] The value to coerce.
      # @return [float] The coerced value
      def coerce(value)
        BigDecimal(value.to_s)
      end

      # Run validation on the given value.
      #
      # @param value [mixed] The value to validate.
      # @return [Boolean] Whether validation passes or not.
      def run(value)
        BigDecimal(value.to_s)
        true
      rescue ArgumentError => _e
        false
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
openactive-0.5.0 lib/openactive/validators/number_validator.rb