<commit_before>module Crm
  module Helpers
    module Validators
      class CrmTypeValidator < ActiveModel::Validator
        include CrmValidatorHelper

        def validate(record)
          crm_type_definition(record).each_pair do |attribute, definition|
            record.validates_presence_of attribute if definition[:mandatory]
            value = record.send(attribute.to_sym)
            next if value.blank?

            attribute_type = definition['attribute_type']
            validator = "::Crm::Helpers::Validators::Crm#{attribute_type.camelcase}Validator".constantize
            record.validates_with validator, attributes: [attribute]
          end
        end
      end
    end
  end
end<commit_msg>Don't validate non-mandatory attributes we can't read normally.<commit_after>module Crm
  module Helpers
    module Validators
      class CrmTypeValidator < ActiveModel::Validator
        include CrmValidatorHelper

        def validate(record)
          crm_type_definition(record).each_pair do |attribute, definition|
            record.validates_presence_of attribute if definition[:mandatory]

            reader = attribute.to_sym
            next unless record.respond_to?(reader)
            value = record.send(reader)
            next if value.blank?

            attribute_type = definition['attribute_type']
            validator = "::Crm::Helpers::Validators::Crm#{attribute_type.camelcase}Validator".constantize
            record.validates_with validator, attributes: [attribute]
          end
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07088122605363985, 'fraction_numerical': 0.0, 'mean_word_length': 2.661214953271028, 'pattern_counts': {'":': 2, '<': 5, '<?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': '2395195', 'n_tokens_mistral': 401, 'n_tokens_neox': 398, 'n_words': 91}