Sha256: 6dee93a5f1136c39eb74a71cf613f3a67fb8b6f0e67b6e87aec78d212ac88906

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module ActiveFields
  module Validators
    # Validates the active_value value
    class BaseValidator
      attr_reader :options, :errors

      def initialize(**options)
        @options = options
        @errors = Set.new
      end

      # Performs the validation and adds errors to the `errors` list.
      # Returns `true` if no errors are found, `false` otherwise.
      def validate(value)
        perform_validation(value)
        valid?
      end

      # Returns `true` if no errors are found, `false` otherwise.
      def valid?
        errors.empty?
      end

      private

      # Performs the validation. If there are any errors, it should save them in `errors`.
      def perform_validation(value)
        raise NotImplementedError
      end

      def validate_size(value, min:, max:)
        if min && value.size < min
          errors << [:size_too_short, count: min]
        end

        if max && value.size > max
          errors << [:size_too_long, count: max]
        end
      end

      def validate_length(value, min:, max:)
        if min && value.length < min
          errors << [:too_short, count: min]
        end

        if max && value.length > max
          errors << [:too_long, count: max]
        end
      end

      def validate_minmax(value, min:, max:)
        # maybe acts_like?(:date) || acts_like?(:time)
        if min && value < min
          formatted =
            if min.respond_to?(:strftime) && defined?(I18n) && I18n.respond_to?(:l)
              I18n.l(min)
            else
              min
            end
          errors << [:greater_than_or_equal_to, count: formatted]
        end

        if max && value > max
          formatted =
            if max.respond_to?(:strftime) && defined?(I18n) && I18n.respond_to?(:l)
              I18n.l(max)
            else
              max
            end
          errors << [:less_than_or_equal_to, count: formatted]
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_fields-2.0.0 lib/active_fields/validators/base_validator.rb
active_fields-1.1.0 lib/active_fields/validators/base_validator.rb
active_fields-1.0.0 lib/active_fields/validators/base_validator.rb