1
0
Fork 0

Add method ApplicationEachValidator::Validation#str_value

This commit is contained in:
Alex Kotov 2019-10-01 01:08:18 +05:00
parent 7fb4404432
commit 63e1ba6e58
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
6 changed files with 13 additions and 9 deletions

View File

@ -35,5 +35,9 @@ class ApplicationEachValidator < ActiveModel::EachValidator
error(*args)
throw :stop_validating
end
def str_value
@str_value ||= String(value).dup.freeze
end
end
end

View File

@ -8,10 +8,10 @@ class CodenameValidator < ApplicationEachValidator
MAX = 36
def perform
error! :blank if value.to_s.blank?
error! :blank if str_value.blank?
error! :codename unless CODENAME_RE.match? value
error! :too_short, count: MIN if value.to_s.length < MIN
error! :too_long, count: MAX if value.to_s.length > MAX
error! :too_short, count: MIN if str_value.length < MIN
error! :too_long, count: MAX if str_value.length > MAX
end
end
end

View File

@ -7,8 +7,8 @@ class GoodBigTextValidator < GoodTextValidator
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
error :too_short, count: MIN if str_value.length < MIN
error :too_long, count: MAX if str_value.length > MAX
end
end
end

View File

@ -7,8 +7,8 @@ class GoodSmallTextValidator < GoodTextValidator
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
error :too_short, count: MIN if str_value.length < MIN
error :too_long, count: MAX if str_value.length > MAX
end
end
end

View File

@ -5,7 +5,7 @@ class GoodTextValidator < ApplicationEachValidator
GOOD_TEXT_RE = /\A[^\s](.*[^\s])?\z/.freeze
def perform
error :blank if value.to_s.blank?
error :blank if str_value.blank?
error :good_text unless GOOD_TEXT_RE.match? value
end
end

View File

@ -5,7 +5,7 @@ class TimezoneValidator < ApplicationEachValidator
TIMEZONE_RE = /\A-?\d\d:\d\d:00\z/.freeze
def perform
error! :blank if value.to_s.blank?
error! :blank if str_value.blank?
error! :timezone unless TIMEZONE_RE.match? value
end
end