lib/solid/validators/uuid_validator.rb in solid-process-0.3.0 vs lib/solid/validators/uuid_validator.rb in solid-process-0.4.0

- old
+ new

@@ -3,19 +3,19 @@ class UuidValidator < ActiveModel::EachValidator PATTERN = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" CASE_SENSITIVE = /\A#{PATTERN}\z/.freeze CASE_INSENSITIVE = /\A#{PATTERN}\z/i.freeze - def validate_each(obj, attribute, value) + def validate_each(model, attribute, value) case_sensitive = options.fetch(:case_sensitive, true) + return model.errors.add(attribute, :blank, **options) if value.blank? + regexp = case_sensitive ? CASE_SENSITIVE : CASE_INSENSITIVE return if value.is_a?(String) && value.match?(regexp) - message = options[:message] || "is not a valid UUID (case #{case_sensitive ? "sensitive" : "insensitive"})" - - obj.errors.add(attribute, message) + model.errors.add(attribute, :invalid, **options) end private_constant :PATTERN end