lib/jss/validate.rb in ruby-jss-1.1.3 vs lib/jss/validate.rb in ruby-jss-1.2.0b1
- old
+ new
@@ -47,13 +47,17 @@
# @return [String] The valid value
#
def self.mac_address(val, msg = nil)
msg ||= "Not a valid MAC address: '#{val}'"
raise JSS::InvalidDataError, msg unless val =~ MAC_ADDR_RE
+
val
end
+ # Segments of a valid IPv4 address are integers in this range.
+ IP_SEGMENT_RANGE = 0..255
+
# Validate the format and content of an IPv4 address
#
# @param val[String] The value to validate
#
# @param msg[String] A custom error message when the value is invalid
@@ -63,16 +67,18 @@
def self.ip_address(val, msg = nil)
msg ||= "Not a valid IPv4 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 && p.to_i >= 0 }
+ parts.each { |p| ok = false unless p.jss_integer? && IP_SEGMENT_RANGE.include?(p.to_i) }
raise JSS::InvalidDataError, msg unless ok
+
val
end
- # Validate that a value doesn't already exist for a given identifier of a given class
+ # 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.
@@ -87,13 +93,21 @@
#
# @param api[JSS::APIConnection] The api connection to use for validation
#
# @return [Object] the validated unique value
#
- def self.unique_identifier(klass, identifier, val, msg = nil, api: JSS.api)
+ def self.doesnt_already_exist(klass, identifier, val, msg = nil, api: JSS.api)
msg ||= "A #{klass} already exists with #{identifier} '#{val}'"
return val unless klass.all(:refresh, api: api).map { |i| i[identifier] }.include? val
+
+ key = klass.real_lookup_key identifier
+
+ # use map_all_ids_to cuz it works with any identifer, even non-existing
+ existing_values = klass.map_all_ids_to( key, api: api).values
+ matches = existing_values.select { |existing_val| existing_val.casecmp? val }
+ return val if matches.empty?
+
raise JSS::AlreadyExistsError, msg
end
# Confirm that the given value is a boolean value, accepting
# strings and symbols and returning real booleans as needed
@@ -111,10 +125,11 @@
def self.boolean(bool, msg = nil)
msg ||= 'Value must be boolean true or false'
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, msg
end
# Confirm that a value is an integer or a string representation of an
# integer. Return the integer, or raise an error
@@ -129,10 +144,11 @@
#
def self.integer(val, msg = nil)
msg ||= 'Value must be an integer'
val = val.to_i if val.is_a?(String) && val.jss_integer?
raise JSS::InvalidDataError, msg unless val.is_a? Integer
+
val
end
# validate that the given value is a non-empty string
#
@@ -143,9 +159,45 @@
# @return [String] the valid non-empty string
#
def self.non_empty_string(val, msg = nil)
msg ||= 'value must be a non-empty String'
raise JSS::InvalidDataError, msg unless val.is_a?(String) && !val.empty?
+
+ val
+ end
+
+ UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.freeze
+
+ # validate that the given value is a valid uuid string
+ #
+ # @param val [Object] the thing to validate
+ #
+ # @param msg[String] A custom error message when the value is invalid
+ #
+ # @return [String] the valid uuid string
+ #
+ def self.uuid(val, msg = nil)
+ msg ||= 'value must be valid uuid'
+ raise JSS::InvalidDataError, msg unless val.is_a?(String) && val =~ UUID_RE
+
+ val
+ end
+
+ # validate that the given value is an integer in the JSS::IBeacon::MAJOR_MINOR_RANGE
+ #
+ # @param val [Object] the thing to validate
+ #
+ # @param msg[String] A custom error message when the value is invalid
+ #
+ # @return [String] the valid integer
+ #
+ def self.ibeacon_major_minor(val, msg = nil)
+ msg ||= "value must be an integer in the range #{JSS::IBeacon::MAJOR_MINOR_RANGE}"
+ val = val.to_i if val.is_a?(String) && val.jss_integer?
+ ok = val.is_a? Integer
+ ok = JSS::IBeacon::MAJOR_MINOR_RANGE.include? val if ok
+ raise JSS::InvalidDataError, msg unless ok
+
val
end
end # module validate