<commit_before># frozen_string_literal: true
module ValidatesZipcode
  class Zipcode
    include CldrRegexpCollection

    def initialize(args = {})
      @zipcode                = args.fetch(:zipcode)
      @country_alpha2         = args.fetch(:country_alpha2)
      @excluded_country_codes = args.fetch(:excluded_country_codes, [])
      @format                 = args.fetch(:format, false)
    end

    def valid?(zipcode = nil)
      return true if @excluded_country_codes.include?(@country_alpha2)
      return true unless regexp

      zipcode ||= @format ? formatted_zip : @zipcode
      !!(regexp =~ zipcode)
    end
    alias_method :validate, :valid?

    def format
      raise InvalidZipcodeError, "invalid zipcode #{@zipcode} for country #{@country_alpha2.to_s.upcase}" unless valid?(formatted_zip)

      formatted_zip
    end

    private

    def regexp
      @regexp ||= regexp_for_country_alpha2(@country_alpha2)
    end

    def formatted_zip
      @formatted_zip ||= Formatter.new(zipcode: @zipcode, country_alpha2: @country_alpha2).format
    end
  end
end
<commit_msg>Do not validate format on formatted zipcode as this can have unintended consequences<commit_after># frozen_string_literal: true
module ValidatesZipcode
  class Zipcode
    include CldrRegexpCollection

    def initialize(args = {})
      @zipcode                = args.fetch(:zipcode)
      @country_alpha2         = args.fetch(:country_alpha2)
      @excluded_country_codes = args.fetch(:excluded_country_codes, [])
      @format                 = args.fetch(:format, false)
    end

    def valid?
      return true if @excluded_country_codes.include?(@country_alpha2)
      return true unless regexp

      zipcode = @format ? formatted_zip : @zipcode
      !!(regexp =~ zipcode)
    end
    alias_method :validate, :valid?

    def format
      raise InvalidZipcodeError, "invalid zipcode #{@zipcode} for country #{@country_alpha2.to_s.upcase}" unless valid?

      formatted_zip
    end

    private

    def regexp
      @regexp ||= regexp_for_country_alpha2(@country_alpha2)
    end

    def formatted_zip
      @formatted_zip ||= Formatter.new(zipcode: @zipcode, country_alpha2: @country_alpha2).format
    end
  end
end

{'fraction_non_alphanumeric': 0.0878774222622803, 'fraction_numerical': 0.007210455159981974, 'mean_word_length': 3.029038112522686, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '341976', 'n_tokens_mistral': 694, 'n_tokens_neox': 666, 'n_words': 155}