Sha256: d752641dd6cba533e80c3709264c731319c35e270e5614ec7706d991c9cfe27e

Contents?: true

Size: 669 Bytes

Versions: 20

Compression:

Stored size: 669 Bytes

Contents

# frozen_string_literal: true

# https://guides.rubyonrails.org/active_record_validations.html#validates-with
module Dsu
  module Validators
    class TimeValidator < ActiveModel::Validator
      def validate(record)
        time = record.time

        if time.nil?
          record.errors.add(:time, :blank)
          return
        end

        unless time.is_a?(Time)
          record.errors.add(:time, 'is the wrong object type. ' \
                                   "\"Time\" was expected, but \"#{time.class}\" was received.")
          return
        end

        record.errors.add(:time, 'is not in localtime format.') if time.utc?
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
dsu-2.1.4 lib/dsu/validators/time_validator.rb
dsu-2.1.3 lib/dsu/validators/time_validator.rb
dsu-2.1.2 lib/dsu/validators/time_validator.rb
dsu-2.1.1 lib/dsu/validators/time_validator.rb
dsu-2.0.8 lib/dsu/validators/time_validator.rb
dsu-2.0.7 lib/dsu/validators/time_validator.rb
dsu-2.0.6 lib/dsu/validators/time_validator.rb
dsu-2.0.5 lib/dsu/validators/time_validator.rb
dsu-2.0.4 lib/dsu/validators/time_validator.rb
dsu-2.0.3 lib/dsu/validators/time_validator.rb
dsu-2.0.2 lib/dsu/validators/time_validator.rb
dsu-2.0.1 lib/dsu/validators/time_validator.rb
dsu-2.0.0.alpha.1 lib/dsu/validators/time_validator.rb
dsu-1.2.1 lib/dsu/validators/time_validator.rb
dsu-1.2.0 lib/dsu/validators/time_validator.rb
dsu-1.1.2 lib/dsu/validators/time_validator.rb
dsu-1.1.1 lib/dsu/validators/time_validator.rb
dsu-1.1.0.alpha.2 lib/dsu/validators/time_validator.rb
dsu-1.1.0.alpha.1 lib/dsu/validators/time_validator.rb
dsu-1.0.0 lib/dsu/validators/time_validator.rb