Sha256: 31ef29aabf554aabcd9576ee7872d4ba7d41ba09c682acd372d89b194f3f3f8f

Contents?: true

Size: 953 Bytes

Versions: 9

Compression:

Stored size: 953 Bytes

Contents

# frozen_string_literal: true

require "pakyow/validator"

module Pakyow
  module Validations
    # Validates that the value is of an expected length.
    #
    # @api public
    module Length
      def self.message(minimum: nil, maximum: nil, **)
        if minimum && maximum
          "must have between #{minimum} and #{maximum} characters"
        elsif minimum
          "must have more than #{minimum} #{wording(minimum)}"
        elsif maximum
          "must have less than #{maximum} #{wording(maximum)}"
        end
      end

      def self.valid?(value, minimum: nil, maximum: nil, **)
        unless minimum.nil?
          return false if value.length < minimum
        end

        unless maximum.nil?
          return false if value.length > maximum
        end

        true
      end

      def self.wording(n)
        n == 1 ? "character" : "characters"
      end
    end

    Validator.register_validation(Length, :length)
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pakyow-core-1.0.6 lib/pakyow/validations/length.rb
pakyow-core-1.0.5 lib/pakyow/validations/length.rb
pakyow-core-1.0.4 lib/pakyow/validations/length.rb
pakyow-core-1.0.3 lib/pakyow/validations/length.rb
pakyow-core-1.0.2 lib/pakyow/validations/length.rb
pakyow-core-1.0.1 lib/pakyow/validations/length.rb
pakyow-core-1.0.0 lib/pakyow/validations/length.rb
pakyow-core-1.0.0.rc5 lib/pakyow/validations/length.rb
pakyow-core-1.0.0.rc4 lib/pakyow/validations/length.rb