Add GoodBigTextValidator
This commit is contained in:
parent
6db0f083e2
commit
c6cb85310b
8 changed files with 57 additions and 9 deletions
|
@ -39,7 +39,9 @@ class Account < ApplicationRecord
|
|||
|
||||
validates :public_name, allow_nil: true, length: { in: 1..255 }
|
||||
|
||||
validates :biography, allow_nil: true, length: { in: 1..10_000 }
|
||||
validates :biography,
|
||||
allow_nil: true,
|
||||
good_big_text: true
|
||||
|
||||
validates :avatar, allow_nil: true, image: true
|
||||
|
||||
|
|
|
@ -22,12 +22,14 @@ class Session < ApplicationRecord
|
|||
|
||||
validates :ip_address, presence: true
|
||||
|
||||
validates :user_agent, length: { maximum: 10_000 }
|
||||
validates :user_agent,
|
||||
allow_nil: true,
|
||||
good_big_text: true
|
||||
|
||||
private
|
||||
|
||||
def turn_nils_into_blanks
|
||||
self.user_agent = '' if user_agent.blank?
|
||||
self.user_agent = nil if user_agent.blank?
|
||||
end
|
||||
|
||||
def strip_extra_spaces
|
||||
|
|
14
app/validators/good_big_text_validator.rb
Normal file
14
app/validators/good_big_text_validator.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class GoodBigTextValidator < GoodTextValidator
|
||||
class Validation < Validation
|
||||
MIN = 1
|
||||
MAX = 10_000
|
||||
|
||||
def perform
|
||||
super
|
||||
error! :too_short, count: MIN if value.to_s.length < MIN
|
||||
error! :too_long, count: MAX if value.to_s.length > MAX
|
||||
end
|
||||
end
|
||||
end
|
18
db/migrate/20190929131544_change_user_agent.rb
Normal file
18
db/migrate/20190929131544_change_user_agent.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class ChangeUserAgent < ActiveRecord::Migration[6.0]
|
||||
include Partynest::Migration
|
||||
|
||||
def change
|
||||
drop_constraint :sessions, :user_agent, <<~SQL
|
||||
user_agent = '' OR is_good_big_text(user_agent)
|
||||
SQL
|
||||
|
||||
remove_column :sessions, :user_agent, :string, null: false, default: ''
|
||||
add_column :sessions, :user_agent, :string, null: true
|
||||
|
||||
constraint :sessions, :user_agent, <<~SQL
|
||||
user_agent IS NULL OR is_good_big_text(user_agent)
|
||||
SQL
|
||||
end
|
||||
end
|
|
@ -763,9 +763,9 @@ CREATE TABLE public.sessions (
|
|||
account_id bigint NOT NULL,
|
||||
logged_at timestamp without time zone NOT NULL,
|
||||
ip_address character varying NOT NULL,
|
||||
user_agent character varying DEFAULT ''::character varying NOT NULL,
|
||||
user_agent character varying,
|
||||
CONSTRAINT ip_address CHECK (public.is_good_small_text((ip_address)::text)),
|
||||
CONSTRAINT user_agent CHECK ((((user_agent)::text = ''::text) OR public.is_good_big_text((user_agent)::text)))
|
||||
CONSTRAINT user_agent CHECK (((user_agent IS NULL) OR public.is_good_big_text((user_agent)::text)))
|
||||
);
|
||||
|
||||
|
||||
|
@ -1617,6 +1617,7 @@ INSERT INTO "schema_migrations" (version) VALUES
|
|||
('20190921142404'),
|
||||
('20190921161613'),
|
||||
('20190921191213'),
|
||||
('20190928171705');
|
||||
('20190928171705'),
|
||||
('20190929131544');
|
||||
|
||||
|
||||
|
|
|
@ -13,6 +13,10 @@ FactoryBot.define do
|
|||
trait :with_ipv6_address do
|
||||
ip_address { Faker::Internet.ip_v6_address }
|
||||
end
|
||||
|
||||
trait :without_user_agent do
|
||||
user_agent { nil }
|
||||
end
|
||||
end
|
||||
|
||||
factory :some_session_with_ipv6_address,
|
||||
|
|
|
@ -33,19 +33,19 @@ RSpec.describe Session do
|
|||
|
||||
it { is_expected.to validate_length_of(:user_agent).is_at_most(10_000) }
|
||||
|
||||
it { is_expected.to allow_value '' }
|
||||
it { is_expected.to allow_value nil }
|
||||
it { is_expected.to allow_value Faker::Internet.user_agent }
|
||||
|
||||
context 'when it was set to nil' do
|
||||
subject { build :some_session, user_agent: nil }
|
||||
before { subject.validate }
|
||||
specify { expect(subject.user_agent).to eq '' }
|
||||
specify { expect(subject.user_agent).to eq nil }
|
||||
end
|
||||
|
||||
context 'when it was set to blank' do
|
||||
subject { build :some_session, user_agent: ' ' * rand(1..3) }
|
||||
before { subject.validate }
|
||||
specify { expect(subject.user_agent).to eq '' }
|
||||
specify { expect(subject.user_agent).to eq nil }
|
||||
end
|
||||
|
||||
context 'when it has leading spaces' do
|
||||
|
|
7
spec/validators/good_big_text_validator_spec.rb
Normal file
7
spec/validators/good_big_text_validator_spec.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe GoodBigTextValidator do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
Reference in a new issue