Sha256: 8afe1bb65664db40fb77f0f948e7b5b2cdff423313d367d5080363a5a170abc3

Contents?: true

Size: 1.21 KB

Versions: 6

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # @example
      #   # bad
      #   params do
      #     requires :id, type: Integer
      #   end
      #
      #   # good
      #   params do
      #     requires :id, type: Integer, positive_int32: true
      #   end
      class UsePositiveInt32Validator < Cop
        MSG = "If this Integer maps to a postgres Integer column, validate with `positive_int32: true`"

        # check if the param is `requires` / `optional`
        def_node_search :find_params_hashes, <<~PATTERN
          (send nil? {:requires :optional}
            (sym _)
            $(hash ...) ...)
        PATTERN
        # check if hash contains `type: Integer`
        def_node_search :is_type_integer?, "(pair (sym :type) (const nil? :Integer))"
        # check if the hash contains the `positive_int32` validator
        def_node_search :validates_integer?, "(pair (sym :positive_int32) {{true false} (int _)})"

        def on_block(node)
          return unless (hash = find_params_hashes(node))

          hash.each do |param|
            add_offense(param) if is_type_integer?(param) && !validates_integer?(param)
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rf-stylez-1.2.4 lib/rubocop/cop/lint/use_positive_int32_validator.rb
rf-stylez-1.2.3 lib/rubocop/cop/lint/use_positive_int32_validator.rb
rf-stylez-1.2.1 lib/rubocop/cop/lint/use_positive_int32_validator.rb
rf-stylez-1.1.2 lib/rubocop/cop/lint/use_positive_int32_validator.rb
rf-stylez-1.1.1 lib/rubocop/cop/lint/use_positive_int32_validator.rb
rf-stylez-1.1.0 lib/rubocop/cop/lint/use_positive_int32_validator.rb