1
0
Fork 0

Add GoodBigTextValidator

This commit is contained in:
Alex Kotov 2019-09-29 18:23:06 +05:00
parent 6db0f083e2
commit c6cb85310b
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
8 changed files with 57 additions and 9 deletions

View file

@ -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

View file

@ -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

View 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

View 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

View file

@ -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');

View file

@ -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,

View file

@ -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

View 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