Module: Jamf::Validate
- Defined in:
- lib/jamf/validate.rb
Overview
A collection of methods for validating values. Mostly for ensuring the validity of data being set as attributes of APIObject subclass instances.
Some of these methods can take multiple input types, such as a String or an Array. All of them will either raise an exception if the value isn’t valid, or will return a standardized form of the input (e.g. an Array, even if given a String)
Constant Summary collapse
- MAC_ADDR_RE =
The regular expression that matches a valid MAC address.
/^[a-f0-9]{2}(:[a-f0-9]{2}){5}$/i
- SCRIPT_SHEBANG =
'#!'.freeze
Class Method Summary collapse
-
.boolean(val, msg = nil) ⇒ Boolean
Confirm that the given value is a boolean value, accepting Strings and Symbols, returning real booleans as needed.
-
.doesnt_exist(val, klass, identifier, msg = nil, cnx: Jamf.cnx) ⇒ Object
Validate that a value doesn’t already exist for a given identifier of a given CollectionResource class.
-
.float(val, msg = nil) ⇒ Float
Confirm that a value is a Float or a String representation of a Float.
-
.integer(val, msg = nil) ⇒ Integer
Confirm that a value is an Integer or a String representation of an Integer.
-
.ip_address(val, msg = nil) ⇒ String
Validate the format and content of an IPv4 address.
-
.json_attribute_name(klass, attr_name) ⇒ Symbol
Does a give JSONObject class have a given JSON attribute?.
-
.mac_address(val, msg = nil) ⇒ String
Validate the format and content of a MAC address.
-
.non_empty_string(val, msg = nil) ⇒ String
validate that the given value is a non-empty string Symbols are accepted and returned as strings.
-
.script_contents(val, msg = nil) ⇒ String
validate that the given value is a string that starts with #!.
-
.string(val, msg = nil) ⇒ String
Confirm that a value is a string, symbol, or nil, all of which will be returned as a string.
Class Method Details
.boolean(val, msg = nil) ⇒ Boolean
Confirm that the given value is a boolean value, accepting Strings and Symbols, returning real booleans as needed
Accepted True values: true, ‘true’, :true, ‘yes’, :yes
Accepted False values: false, ‘false’, :false, ‘no’, :no
all Strings and Symbols are case insensitive
134 135 136 137 138 139 140 |
# File 'lib/jamf/validate.rb', line 134 def self.boolean(val, msg = nil) msg ||= 'Value must be boolean true or false' return true if val.to_s =~ /^(true|yes)$/i return false if val.to_s =~ /^(false|no)$/i raise Jamf::InvalidDataError, msg end |
.doesnt_exist(val, klass, identifier, msg = nil, cnx: Jamf.cnx) ⇒ Object
Validate that a value doesn’t already exist for a given identifier of a given CollectionResource class
e.g. when klass = Jamf::Computer, identifier = :name, and val = ‘foo’ will raise an error when a computer named ‘foo’ already exists
Otherwise returns val.
109 110 111 112 113 114 115 116 117 |
# File 'lib/jamf/validate.rb', line 109 def self.doesnt_exist(val, klass, identifier, msg = nil, cnx: Jamf.cnx) msg ||= "A #{klass} already exists with #{identifier} '#{val}'" raise Jamf::InvalidDataError, "No identifier '#{identifier}' for #{klass}" unless klass.identifiers.include? identifier return val unless klass.send("all_#{identifier}s", :refresh, cnx: cnx).include? val raise Jamf::AlreadyExistsError, msg end |
.float(val, msg = nil) ⇒ Float
Confirm that a value is a Float or a String representation of a Float. Return the Float, or raise an error
167 168 169 170 171 172 |
# File 'lib/jamf/validate.rb', line 167 def self.float(val, msg = nil) msg ||= 'Value must be a Floating Point number' val = val.to_f if val.is_a?(String) && val.j_float? raise Jamf::InvalidDataError, msg unless val.is_a? Flot val end |
.integer(val, msg = nil) ⇒ Integer
Confirm that a value is an Integer or a String representation of an Integer. Return the integer, or raise an error
151 152 153 154 155 156 |
# File 'lib/jamf/validate.rb', line 151 def self.integer(val, msg = nil) msg ||= 'Value must be an Integer' val = val.to_i if val.is_a?(String) && val.j_integer? raise Jamf::InvalidDataError, msg unless val.is_a? Integer val end |
.ip_address(val, msg = nil) ⇒ String
Validate the format and content of an IPv4 address
64 65 66 67 68 69 70 71 72 |
# File 'lib/jamf/validate.rb', line 64 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.j_integer? && p.to_i < 256 && p.to_i >= 0 } raise Jamf::InvalidDataError, msg unless ok val end |
.json_attribute_name(klass, attr_name) ⇒ Symbol
Does a give JSONObject class have a given JSON attribute?
82 83 84 85 86 87 |
# File 'lib/jamf/validate.rb', line 82 def self.json_attribute_name(klass, attr_name) raise "#{klass} is not a descendent of JSONObject" unless klass < Jamf::JSONObject raise Jamf::NoSuchItemError, "No attribute #{attr_name} for class #{klass}" unless klass::OBJECT_MODEL.key? attrib attr_name end |
.mac_address(val, msg = nil) ⇒ String
Validate the format and content of a MAC address
50 51 52 53 54 |
# File 'lib/jamf/validate.rb', line 50 def self.mac_address(val, msg = nil) msg ||= "Not a valid MAC address: '#{val}'" raise Jamf::InvalidDataError, msg unless val =~ MAC_ADDR_RE val end |
.non_empty_string(val, msg = nil) ⇒ String
validate that the given value is a non-empty string Symbols are accepted and returned as strings
200 201 202 203 204 205 |
# File 'lib/jamf/validate.rb', line 200 def self.non_empty_string(val, msg = nil) msg ||= 'value must be a non-empty String' val = val.to_s if val.is_a? Symbol raise Jamf::InvalidDataError, msg unless val.is_a?(String) && !val.empty? val end |
.script_contents(val, msg = nil) ⇒ String
validate that the given value is a string that starts with #!
217 218 219 220 221 |
# File 'lib/jamf/validate.rb', line 217 def self.script_contents(val, msg = nil) msg ||= "value must be a String starting with '#!'" raise Jamf::InvalidDataError, msg unless val.is_a?(String) && val.start_with?(SCRIPT_SHEBANG) val end |
.string(val, msg = nil) ⇒ String
Confirm that a value is a string, symbol, or nil, all of which will be returned as a string
183 184 185 186 187 188 189 |
# File 'lib/jamf/validate.rb', line 183 def self.string(val, msg = nil) msg ||= 'Value must be a String' return Jamf::BLANK if val.nil? val = val.to_s if val.is_a? Symbol raise Jamf::InvalidDataError, msg unless val.is_a? String val end |