<commit_before>module Bureaucrat
  module Validators
    extend self

    def empty_value?(value)
      value.nil? || value == '' || value == [] || value == {}
    end

    class RegexValidator
      attr_accessor :regex, :message, :code

      def initialize(options = {})
        @regex = Regexp.new(options.fetch(:regex, ''))
        @message = options.fetch(:message, 'Enter a valid value.')
        @code = options.fetch(:code, :invalid)
      end

      # Validates that the input validates the regular expression
      def call(value)
        if regex !~ value
          raise ValidationError.new(@message, code, :regex => regex)
        end
      end
    end

    def validate_integer
      lambda do |value|
        begin
          Integer(string)
        rescue ArgumentError
          raise ValidationError.new('')
        end
      end
    end

    # Original from Django's EmailField:
    # email_re = re.compile(
    #    r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*"  # dot-atom
    #    r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string
    #    r')@(?:[A-Z0-9]+(?:-*[A-Z0-9]+)*\.)+[A-Z]{2,6}$', re.IGNORECASE)  # domain
    EMAIL_RE = /(^[-!#\$%&'*+\/=?^_`{}|~0-9A-Z]+(\.[-!#\$%&'*+\/=?^_`{}|~0-9A-Z]+)*|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*")@(?:[A-Z0-9]+(?:-*[A-Z0-9]+)*\.)+[A-Z]{2,6}$/i

    def validate_email
      RegexValidator.new(:regex => EMAIL_RE,
                         :message => 'Enter a valid e-mail address.',
                         :code => :invalid)
    end

    SLUG_RE = /^[-\w]+$/

    def validate_slug
      RegexValidator.new(:regex => SLUG_RE,
                         :message => "Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens.",
                         :code => :invalid)
    end

    IPV4_RE = /^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$/

    def validate_ipv4_address
      RegexValidator.new(:regex => IPV4_RE,
                         :message => 'Enter a valid IPv4 address.',
                         :code => 'invalid')
    end

    COMMA_SEPARATED_INT_LIST_RE = /^[\d,]+$/

    def validate_comma_separated_integer_list
      RegexValidator.new(:regex => COMMA_SEPARATED_INT_LIST_RE,
                         :message => 'Enter only digits separated by commas.',
                         :code => :invalid)
    end

    class BaseValidator
      def initialize(limit_value)
        @limit_value = limit_value
      end

      def message<commit_msg>Memoize validators.<commit_after>module Bureaucrat
  module Validators
    extend self

    def empty_value?(value)
      value.nil? || value == '' || value == [] || value == {}
    end

    class RegexValidator
      attr_accessor :regex, :message, :code

      def initialize(options = {})
        @regex = Regexp.new(options.fetch(:regex, ''))
        @message = options.fetch(:message, 'Enter a valid value.')
        @code = options.fetch(:code, :invalid)
      end

      # Validates that the input validates the regular expression
      def call(value)
        if regex !~ value
          raise ValidationError.new(@message, code, :regex => regex)
        end
      end
    end

    def validate_integer
      @@validate_integer ||= lambda do |value|
        begin
          Integer(string)
        rescue ArgumentError
          raise ValidationError.new('')
        end
      end
    end

    # Original from Django's EmailField:
    # email_re = re.compile(
    #    r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*"  # dot-atom
    #    r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string
    #    r')@(?:[A-Z0-9]+(?:-*[A-Z0-9]+)*\.)+[A-Z]{2,6}$', re.IGNORECASE)  # domain
    EMAIL_RE = /(^[-!#\$%&'*+\/=?^_`{}|~0-9A-Z]+(\.[-!#\$%&'*+\/=?^_`{}|~0-9A-Z]+)*|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*")@(?:[A-Z0-9]+(?:-*[A-Z0-9]+)*\.)+[A-Z]{2,6}$/i

    def validate_email
      @@validate_email ||=
        RegexValidator.new(:regex => EMAIL_RE,
                           :message => 'Enter a valid e-mail address.',
                           :code => :invalid)
    end

    SLUG_RE = /^[-\w]+$/

    def validate_slug
      @@validate_slug ||=
        RegexValidator.new(:regex => SLUG_RE,
                           :message => "Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens.",
                           :code => :invalid)
    end

    IPV4_RE = /^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$/

    def validate_ipv4_address
      @@validate_ipv4_address ||=
        RegexValidator.new(:regex => IPV4_RE,
                           :message => 'Enter a valid IPv4 address.',
                           :code => 'invalid')
    end

    COMMA_SEPARATED_INT_LIST_RE = /^[\d,]+$/

    def validate_comma_separated_integer_list
      @@validate_comma_separated_integer_list ||=
        RegexValidator.new(:regex => COMMA_SEPARATED_INT_LIST_RE,
                           :message => 'Enter only digits separated by commas.',
                           :code => :invalid)
    end

    class BaseValidator
      def initialize(limit_value)
        @limit_value = limit_value
      end

      def message
{'fraction_non_alphanumeric': 0.19879969992498125, 'fraction_numerical': 0.04557389347336834, 'mean_word_length': 2.0078962210941906, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7363505', 'n_tokens_mistral': 2045, 'n_tokens_neox': 1868, 'n_words': 319}