Sha256: f1ec6b612d38683b0bae74bc9c955dced43e090cf2842f4b1238847686fb4a37

Contents?: true

Size: 1.02 KB

Versions: 48

Compression:

Stored size: 1.02 KB

Contents

module Incline
  ##
  # Validates a string value to ensure it is a safe name.
  #
  # A safe name is one that only contains letters, numbers, and underscore.
  # It must also start with a letter and cannot end with an underscore.
  class SafeNameValidator < ActiveModel::EachValidator

    ##
    # Validates a string to ensure it is a safe name.
    VALID_MASK = /\A[a-z](?:_*[a-z0-9]+)*\z/i

    ##
    # Validates attributes to determine if the values match the requirements of a safe name.
    def validate_each(record, attribute, value)
      unless value.blank?
        unless value =~ VALID_MASK
          if value =~ /\A[^a-z]/i
            record.errors[attribute] << (options[:message] || 'must start with a letter')
          elsif value =~ /_\z/
            record.errors[attribute] << (options[:message] || 'must not end with an underscore')
          else
            record.errors[attribute] << (options[:message] || 'must contain only letters, numbers, and underscore')
          end
        end
      end
    end

  end
end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
incline-0.3.14 lib/incline/validators/safe_name_validator.rb
incline-0.3.13 lib/incline/validators/safe_name_validator.rb
incline-0.3.12 lib/incline/validators/safe_name_validator.rb
incline-0.3.11 lib/incline/validators/safe_name_validator.rb
incline-0.3.10 lib/incline/validators/safe_name_validator.rb
incline-0.3.9 lib/incline/validators/safe_name_validator.rb
incline-0.3.8 lib/incline/validators/safe_name_validator.rb
incline-0.3.7 lib/incline/validators/safe_name_validator.rb
incline-0.3.6 lib/incline/validators/safe_name_validator.rb
incline-0.3.5 lib/incline/validators/safe_name_validator.rb
incline-0.3.4 lib/incline/validators/safe_name_validator.rb
incline-0.3.3 lib/incline/validators/safe_name_validator.rb
incline-0.3.2 lib/incline/validators/safe_name_validator.rb
incline-0.3.1 lib/incline/validators/safe_name_validator.rb
incline-0.3.0 lib/incline/validators/safe_name_validator.rb
incline-0.2.36 lib/incline/validators/safe_name_validator.rb
incline-0.2.35 lib/incline/validators/safe_name_validator.rb
incline-0.2.34 lib/incline/validators/safe_name_validator.rb
incline-0.2.28 lib/incline/validators/safe_name_validator.rb
incline-0.2.27 lib/incline/validators/safe_name_validator.rb