Sha256: 2af8093cecd566e0978c726665602484e94d1c5ccb43ce8c92f8f8ea8372e250

Contents?: true

Size: 1.26 KB

Versions: 10

Compression:

Stored size: 1.26 KB

Contents

module OpenActive
  module Validators
    class DateIntervalValidator < 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 [mixed] The same value.
      # @raise [::Exception] When value cannot be parsed as a ::DateInterval
      def coerce(value)
        return value if value.is_a?(ActiveSupport::Duration)

        # If not passing a DateInterval object, try and create one from value
        # Assuming that the string will be an interval spec in ISO 8601 format
        ActiveSupport::Duration.parse(value)
      end

      # Run validation on the given value.
      #
      # @param value [mixed] The value to validate.
      # @return [Boolean] Whether validation passes or not.
      def run(value)
        return true if value.is_a?(ActiveSupport::Duration)

        # If not a string - fail validation
        return false unless value.is_a?(String)

        begin
          ActiveSupport::Duration.parse(value)
          true
        rescue ArgumentError => _e
          false
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
openactive-0.5.0 lib/openactive/validators/date_interval_validator.rb
openactive-0.4.0 lib/openactive/validators/date_interval_validator.rb
openactive-0.3.0 lib/openactive/validators/date_interval_validator.rb
openactive-0.2.2 lib/openactive/validators/date_interval_validator.rb
openactive-0.2.1 lib/openactive/validators/date_interval_validator.rb
openactive-0.2.0 lib/openactive/validators/date_interval_validator.rb
openactive-0.1.2 lib/openactive/validators/date_interval_validator.rb
openactive-0.1.1 lib/openactive/validators/date_interval_validator.rb
openactive-0.1.0 lib/openactive/validators/date_interval_validator.rb
openactive-0.1.0.rc1 lib/openactive/validators/date_interval_validator.rb