Added validation for one only IP

This commit is contained in:
Sergei Tsoganov 2023-05-30 14:42:56 +03:00
parent bf3d971d7c
commit 18cd6f5046
2 changed files with 8 additions and 0 deletions

View file

@ -5,6 +5,7 @@ class WhiteIp < ApplicationRecord
validate :valid_ipv4?
validate :valid_ipv6?
validate :validate_ipv4_and_ipv6
validate :validate_only_one_ip
before_save :normalize_blank_values
def normalize_blank_values
@ -17,6 +18,12 @@ class WhiteIp < ApplicationRecord
errors.add(:base, I18n.t(:ipv4_or_ipv6_must_be_present))
end
def validate_only_one_ip
return unless ipv4.present? && ipv6.present?
errors.add(:base, I18n.t(:ip_must_be_one))
end
def valid_ipv4?
return if ipv4.blank?