Sha256: 3731ddab7e7ed8fd81cfbf2f6886c65ec4a033a844cd50afee6e0e833c9dd0b3

Contents?: true

Size: 989 Bytes

Versions: 2

Compression:

Stored size: 989 Bytes

Contents

# frozen_string_literal: true

require "active_support/all"
require "grape"

module Grape
  module ExtraValidators
    class MinimumLength < Grape::Validations::Base
      # ------------------------------------------------------------------------------------------------------------------------
      # Methods
      # ------------------------------------------------------------------------------------------------------------------------
      # Public Methods
      # ------------------------------------------------------------------------------------------------------------------------
      def validate_param!(attr_name, params)
        return if !@required && params[attr_name].blank?
        return if params[attr_name].length >= @option

        unit = "character".pluralize(@option)
        message = "must be at least #{@option} #{unit} long"

        fail Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
grape-extra_validators-2.0.0 lib/grape/extra_validators/minimum_length.rb
grape-extra_validators-1.0.0 lib/grape/extra_validators/minimum_length.rb