lib/jss/validate.rb in ruby-jss-0.14.0 vs lib/jss/validate.rb in ruby-jss-1.0.0b2
- old
+ new
@@ -20,11 +20,10 @@
# KIND, either express or implied. See the Apache License for the specific
# language governing permissions and limitations under the Apache License.
#
#
-#
module JSS
# A collection of methods for validating values. Mostly for
# ensuring the validity of data being set as attributes of APIObject
# subclass instances.
@@ -58,27 +57,93 @@
#
def self.ip_address(val)
ok = true
parts = val.strip.split '.'
ok = false unless parts.size == 4
- parts.each { |p| ok = false unless p.jss_integer? && p.to_i < 256 }
+ parts.each { |p| ok = false unless p.jss_integer? && p.to_i < 256 && p.to_i >= 0 }
raise JSS::InvalidDataError, "Not a valid IPv4 address: '#{val}'" unless ok
val
end
# Validate that a value doesn't already exist for a given identifier of a given class
#
+ # e.g. when klass = JSS::Computer, identifier = :name, and val = 'foo'
+ # will raise an error when a computer named 'foo' exists
+ #
+ # Otherwise returns val.
+ #
# @param klass[JSS::APIObject] A subclass of JSS::APIObject, e.g. JSS::Computer
#
# @param identifier[Symbol] One of the keys of an Item of the class's #all Array
#
# @param val[Object] The value to check for uniqueness
#
# @return [Object] the validated unique value
#
def self.unique_identifier(klass, identifier, val, api: JSS.api)
- raise JSS::AlreadyExistsError, "A #{klass} already exists with #{identifier} '#{val}'" if klass.all(:refresh, api: api).map { |i| i[identifier] }.include? val
+ return val unless klass.all(:refresh, api: api).map { |i| i[identifier] }.include? val
+ raise JSS::AlreadyExistsError, "A #{klass} already exists with #{identifier} '#{val}'"
+ end
+
+ # Confirm that the given value is a boolean value, accepting
+ # strings and symbols and returning real booleans as needed
+ # Accepts: true, false, 'true', 'false', :true, :false, 'yes', 'no', :yes,
+ # or :no (all Strings and Symbols are case insensitive)
+ #
+ # TODO: use this throughout ruby-jss
+ #
+ # @param bool [Boolean,String,Symbol] The value to validate
+ #
+ # @return [Boolean] the valid boolean
+ #
+ def self.boolean(bool)
+ return bool if JSS::TRUE_FALSE.include? bool
+ return true if bool.to_s =~ /^(true|yes)$/i
+ return false if bool.to_s =~ /^(false|no)$/i
+ raise JSS::InvalidDataError, 'Value must be boolean true or false'
+ end
+
+ # Confirm that a value is an integer or a string representation of an
+ # integer. Return the integer, or raise an error
+ #
+ # TODO: use this throughout ruby-jss
+ #
+ # @param val[Object] the value to validate
+ #
+ # @return [void]
+ #
+ def self.integer(val)
+ val = val.to_i if val.is_a?(String) && val.jss_integer?
+ raise JSS::InvalidDataError, 'Value must be an integer' unless val.is_a? Integer
val
+ end
+
+ # validate that the given value is a non-empty string
+ #
+ # @param val [Object] the thing to validate
+ #
+ # @return [String] the valid non-empty string
+ #
+ def self.non_empty_string(val)
+ raise JSS::InvalidDataError, 'value must be a non-empty String' unless val.is_a?(String) && !val.empty?
+ val
+ end
+
+ # Confirm that the given value is a boolean value, accepting
+ # strings and symbols and returning real booleans as needed
+ # Accepts: true, false, 'true', 'false', :true, :false, 'yes', 'no', :yes,
+ # or :no (all Strings and Symbols are case insensitive)
+ #
+ #
+ # @param bool [Boolean,String,Symbol] The value to validate
+ #
+ # @return [Boolean] the valid boolean
+ #
+ def self.boolean(bool)
+ return bool if JSS::TRUE_FALSE.include? bool
+ return true if bool.to_s =~ /^(true|yes)$/i
+ return false if bool.to_s =~ /^(false|no)$/i
+ raise JSS::InvalidDataError, 'Value must be boolean true or false'
end
end # module validate
end # module JSS