From 2829e082d711fe530fa73c39883de83dfccf73e0 Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Wed, 16 Jun 2021 17:14:04 -0500 Subject: [PATCH] [Vmdb::Settings::Validator] Add .integer_with_method? This tests that a value in the settings is in fact a integer, or a integer that can be evaluated via `.to_i_with_method` to something you would expect. This becomes challenging because `.to_i` by default will take any string and convert it to zero. This means that: irb> "".to_i == 0 true irb> "Five".to_i == 0 true irb> "Foobar".to_i == 0 true Hence the complexity of the method, since it requires checking that: - it is something that is a reasonable to expect for an int - zero when explicitly asked for - a blank value --- lib/vmdb/settings/validator.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/vmdb/settings/validator.rb b/lib/vmdb/settings/validator.rb index 0b84c60d6d8..d20ce6813a1 100644 --- a/lib/vmdb/settings/validator.rb +++ b/lib/vmdb/settings/validator.rb @@ -31,6 +31,23 @@ def validate private + def integer_with_method?(num) + if is_integer?(num.to_i_with_method) && num.present? + # handling the "Five".to_i == 0 case + # + # Allows: + # + # - 0 + # - 0.minutes (always will be zero) + # - any integer + # - any integer + method + # + num.to_i_with_method != 0 || num.to_s.match?(/^0(?=\..*|$)/) + else + num.blank? + end + end + def webservices(data) valid, errors = true, []