Sha256: 7bff730786c61645fb9525046a311d0adfb31fcb48b1feee417255536ffcdbef

Contents?: true

Size: 986 Bytes

Versions: 2

Compression:

Stored size: 986 Bytes

Contents

# frozen_string_literal: true

require "active_support/all"
require "grape"

module Grape
  module ExtraValidators
    class MaximumLength < 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 up to #{@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/maximum_length.rb
grape-extra_validators-1.0.0 lib/grape/extra_validators/maximum_length.rb