Sha256: 1a320527aa5c62e5978a2b9acddae5e98ce2a5bbfbfaef65cc794e18a0552b13

Contents?: true

Size: 801 Bytes

Versions: 2

Compression:

Stored size: 801 Bytes

Contents

# frozen_string_literal: true

module ActiveFields
  module Validators
    class EnumArrayValidator < BaseValidator
      private

      def perform_validation(value)
        unless value.is_a?(Array)
          errors << :invalid
          return
        end

        validate_size(value, min: options[:min_size], max: options[:max_size])

        if options[:allowed_values].is_a?(Array)
          value.each do |elem_value|
            validate_value_allowed(elem_value, allowed_values: options[:allowed_values])
          end
        end

        if value.size != value.uniq.size
          errors << :duplicate
        end
      end

      def validate_value_allowed(value, allowed_values:)
        return if allowed_values.include?(value)

        errors << :inclusion
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_fields-1.1.0 lib/active_fields/validators/enum_array_validator.rb
active_fields-1.0.0 lib/active_fields/validators/enum_array_validator.rb