class RegexReservedValidator
Constants
- DEFAULT_MESSAGE
Public Instance Methods
add_error(record, attr_name, message)
click to toggle source
# File lib/rere_validator.rb, line 26 def add_error(record, attr_name, message) message_options = { default: [DEFAULT_MESSAGE[message], options[:message]], scope: [:errors, :messages] } record.errors.add(attr_name, I18n.t(message, message_options)) end
reserved?(value)
click to toggle source
# File lib/rere_validator.rb, line 34 def reserved?(value) add_reserved_words = @options[:add_reserved_words] || [] (RESERVED_WORDS + add_reserved_words).include?(value.downcase) end
validate_each(record, attribute, value)
click to toggle source
# File lib/rere_validator.rb, line 12 def validate_each(record, attribute, value) if reserved?(value) add_error(record, attribute, :reserved_words) end if value !~ /\A[a-zA-Z0-9_.-]+\z/ add_error(record, attribute, :invalid_characters) elsif value.match(/\A[_.-].*\z/) add_error(record, attribute, :invalid_characters_at_begin) elsif value.match(/\A.*[_.-]\z/) add_error(record, attribute, :invalid_characters_at_end) end end