class NameValidator < ActiveModel::EachValidator MAX_NAME_LEN = 200 NAME_REGEX = /\A[\w\-]{1,#{MAX_NAME_LEN}}\z/ def validate_each(record, attribute, value) unless NAME_REGEX.match(value) record.errors.add attribute, (options[:message] || "Invalid #{attribute} value '#{value}': it should contains symbols 'a-zA-Z0-9_-' and length should be more then 1 and less or equals then #{MAX_NAME_LEN} symbols") end end end