Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bytesize_validator.rb « validators « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: adbdd81d5c4d09f007f0cb32a4cd181d2e287c6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true

# BytesizeValidator
#
# Custom validator for verifying that bytesize of a field doesn't exceed the specified limit.
# It is different from Rails length validator because it takes .bytesize into account instead of .size/.length
#
# Example:
#
#   class Snippet < ActiveRecord::Base
#     validates :content, bytesize: { maximum: -> { Gitlab::CurrentSettings.snippet_size_limit } }
#   end
#
# Configuration options:
# * <tt>maximum</tt> - Proc that evaluates the bytesize limit that cannot be exceeded
class BytesizeValidator < ActiveModel::EachValidator
  def validate_each(record, attr, value)
    size = value.to_s.bytesize
    max_size = options[:maximum].call

    return if size <= max_size

    error_message = format(_('is too long (%{size}). The maximum size is %{max_size}.'), {
                             size: ActiveSupport::NumberHelper.number_to_human_size(size),
                             max_size: ActiveSupport::NumberHelper.number_to_human_size(max_size)
                           })

    record.errors.add(attr, error_message)
  end
end