Skip to content

Commit

Permalink
Conditional validations for user github_username and twitter_username f…
Browse files Browse the repository at this point in the history
…orem#2061  (forem#2312)

* Conditional validation for usernames

* Allow nil, not blank for usernames
  • Loading branch information
lightalloy authored and benhalpern committed Apr 5, 2019
1 parent d7e2a07 commit 1b9f243
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class User < ApplicationRecord
length: { in: 2..30 },
exclusion: { in: ReservedWords.all, message: "username is reserved" }
validates :username, uniqueness: { case_sensitive: false }, if: :username_changed?
validates :twitter_username, uniqueness: { allow_blank: true }
validates :github_username, uniqueness: { allow_blank: true }
validates :twitter_username, uniqueness: { allow_nil: true }, if: :twitter_username_changed?
validates :github_username, uniqueness: { allow_nil: true }, if: :github_username_changed?
validates :experience_level, numericality: { less_than_or_equal_to: 10 }, allow_blank: true
validates :text_color_hex, format: /\A#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\z/, allow_blank: true
validates :bg_color_hex, format: /\A#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\z/, allow_blank: true
Expand Down
4 changes: 2 additions & 2 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
it { is_expected.to have_many(:chat_channels).through(:chat_channel_memberships) }
it { is_expected.to have_many(:push_notification_subscriptions).dependent(:destroy) }
it { is_expected.to validate_uniqueness_of(:username).case_insensitive }
it { is_expected.to validate_uniqueness_of(:github_username).allow_blank }
it { is_expected.to validate_uniqueness_of(:twitter_username).allow_blank }
it { is_expected.to validate_uniqueness_of(:github_username).allow_nil }
it { is_expected.to validate_uniqueness_of(:twitter_username).allow_nil }
it { is_expected.to validate_presence_of(:username) }
it { is_expected.to validate_length_of(:username).is_at_most(30).is_at_least(2) }
it { is_expected.to validate_length_of(:name).is_at_most(100) }
Expand Down

0 comments on commit 1b9f243

Please sign in to comment.