| Module | Dsadmin::ValueChecker |
| In: |
lib/dsadmin/value_checker.rb
|
Collection of methods for checking values for validity.
All methods will throw an ArgumentErrror if they find the value to be invalid and silently return if it‘s valid.
| HOSTNAME_REGEX | = | /\A([\w-]+\.)*([a-zA-Z]{2,})\z/ | Might be useful for other code | |
| Q_HOSTNAME_REGEX | = | /\A([\w-]+\.)+([a-zA-Z]{2,})\z/ | ||
| EMAIL_REGEX | = | /\A[^@\s]+@[\w-]+(\.[\w-]+)*?(\.\w\w+)\z/ |
Check whether aValue is a well-formed email adress
# File lib/dsadmin/value_checker.rb, line 172
172: def checkEmail(aValue, aMsg = nil)
173: aMsg = "Not a well-formed email adress: '#{aValue}'" unless aMsg
174:
175: # strip comments
176: val = aValue
177: val.gsub!(/\([^()]*\)/, '')
178: val.gsub!(/"[^"]*"/, '')
179: val.sub!(/^.*<(.*)>\s*$/, '\\1')
180:
181: # strip leading/trailing whitespace
182: val.strip!
183:
184: ##puts "after preprocessing: '#{val}'\n"
185: # check for basic form
186: checkTrue((val =~ EMAIL_REGEX), aMsg)
187: end
Check whether aValue == anExpected
# File lib/dsadmin/value_checker.rb, line 56
56: def checkEqual(anExpected, aValue, aMsg = nil)
57: aMsg = "'#{anExpected}' expected, but got '#{aValue}'" unless aMsg
58: checkTrue((aValue == anExpected),aMsg)
59: end
Check whether aValue is a well-formed hostname or IP address (IPv4 only for now)
# File lib/dsadmin/value_checker.rb, line 192
192: def checkHostOrIp(value, aMsg = nil)
193: aMsg = "Not a well-formed hostname or ipv4 address: '#{value}'" unless aMsg
194:
195: # check for hostname
196: begin
197: checkMatch(HOSTNAME_REGEX, value, aMsg)
198: rescue ArgumentError => e
199: # otherwise check if it's an IP address
200: ip = IPAddr.new(value, Socket::AF_INET)
201: end
202: end
Check whether aString is a valid Identifier
# File lib/dsadmin/value_checker.rb, line 47
47: def checkIdentifier(aString, aMsg = nil)
48: checkKindOf(String, aString)
49:
50: aMsg = "'#{aString}' is not a valid identifier" unless aMsg
51: checkTrue((aString =~ /\A[a-zA-Z_]\w*\z/), aMsg)
52: end
Check whether anEnumerable.include?(aValue)
# File lib/dsadmin/value_checker.rb, line 154
154: def checkInSet(anEnumerable, aValue, aMsg = nil)
155: requireKindOf(Enumerable, anEnumerable)
156:
157: if(aMsg.nil?)
158: if(anEnumerable.kind_of?(Array))
159: aMsg = 'Has to be one of "' + anEnumerable.join('", "') + '"'
160: elsif(anEnumerable.kind_of?(Range))
161: aMsg = "Has to be in (#{anEnumerable.first}..#{anEnumerable.last}), but is #{aValue}"
162: else
163: aMsg = "Has to be in a given set"
164: end
165: end
166:
167: checkTrue(anEnumerable.include?(aValue), aMsg)
168: end
Check whether aValue.instance_of? aClass
# File lib/dsadmin/value_checker.rb, line 63
63: def checkInstanceOf(aClass, aValue, aMsg = nil)
64: requireKindOf(Class, aClass)
65:
66: aMsg = "Has to be of type '#{aClass.name}'" unless aMsg
67: checkTrue((aValue.instance_of? aClass),aMsg)
68: end
Check whether aValue.kind_of? aClass
# File lib/dsadmin/value_checker.rb, line 72
72: def checkKindOf(aClass, aValue, aMsg = nil)
73: requireKindOf(Class, aClass)
74:
75: aMsg = "Has to be of a subtype of '#{aClass.name}'" unless aMsg
76: checkTrue((aValue.kind_of? aClass), aMsg)
77: end
Check whether aString =~ aPattern
# File lib/dsadmin/value_checker.rb, line 88
88: def checkMatch(aPattern, aString, aMsg = nil)
89: checkKindOf(String, aString)
90:
91: if(aPattern.kind_of? Regexp)
92: pat = aPattern
93: else
94: pat = Regexp.new(aPattern.to_s)
95: end
96:
97: aMsg = "Has to match regular expression '#{aPattern.to_s}'" unless aMsg
98: checkTrue((aString =~ pat), aMsg)
99: end
Check whether aValue <= aMaxSize
# File lib/dsadmin/value_checker.rb, line 118
118: def checkMaxSize(aMaxSize, aValue, aMsg = nil)
119: requireKindOf(Integer, aMaxSize)
120: requireTrue(aMaxSize >= 0)
121:
122: aMsg = "Size(Length) has to be at most #{aMaxSize} but is #{aValue.size}" unless aMsg
123: checkTrue((aValue.size <= aMaxSize), aMsg)
124: end
Check whether aValue >= aMinSize
# File lib/dsadmin/value_checker.rb, line 128
128: def checkMinSize(aMinSize, aValue, aMsg = nil)
129: requireKindOf(Integer, aMinSize)
130: requireTrue(aMinSize >= 0)
131:
132: aMsg = "Size(Length) has to be at least #{aMinSize} but is #{aValue.size}" unless aMsg
133: checkTrue((aValue.size >= aMinSize), aMsg)
134: end
Check whether aValue == nil
# File lib/dsadmin/value_checker.rb, line 81
81: def checkNil(aValue, aMsg = nil)
82: aMsg = "Has to be nil (must not contain any value)" unless aMsg
83: checkTrue((aValue == nil),aMsg)
84: end
Check whether aString !~ aPattern
# File lib/dsadmin/value_checker.rb, line 103
103: def checkNotMatch(aPattern, aString, aMsg = nil)
104: checkKindOf(String, aString)
105:
106: if(aPattern.kind_of? String)
107: pat = Regexp.new(aPattern)
108: else
109: pat = aPattern
110: end
111:
112: aMsg = "May not match regular expression '#{aPattern.to_s}'" unless aMsg
113: checkTrue((aString !~ pat), aMsg)
114: end
Check whether aValue != nil
# File lib/dsadmin/value_checker.rb, line 147
147: def checkNotNil(aValue, aMsg = nil)
148: aMsg = "May not be nil (has to have a value)" unless aMsg
149: checkTrue((aValue != nil),aMsg)
150: end
Check whether aSizeRange === aValue
# File lib/dsadmin/value_checker.rb, line 138
138: def checkSize(aSizeRange, aValue, aMsg = nil)
139: requireKindOf(Range, aSizeRange)
140:
141: aMsg = "Size(Length) has to be in range (#{aSizeRange.first}..#{aSizeRange.last}) but is #{aValue.size}" unless aMsg
142: checkTrue((aSizeRange === aValue.size), aMsg)
143: end