Class: Mousevc::Validation

Inherits:
Object
  • Object
show all
Defined in:
lib/mousevc/validation.rb

Overview

Extend this class to perform validations on user input in your model. Use the @error attribute to pass notices to the Mousevc::Input class. From there the notice can be easily output in your views.

Each predicate method in this class sets the appropriate error message ready to be output to the user.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorString

Returns the error message generated during validation

Returns:

  • (String)

    the error message generated during validation


18
19
20
# File 'lib/mousevc/validation.rb', line 18

def error
  @error
end

Instance Method Details

#alpha?(value) ⇒ Boolean

Returns true if the string containers only uppercase or lowercase letters

Parameters:

  • value (String)

    the value

Returns:

  • (Boolean)

26
27
28
29
30
31
32
# File 'lib/mousevc/validation.rb', line 26

def alpha?(value)
	is_alpha = coerce_bool (value =~ /^[a-zA-Z]+$/)
	unless is_alpha
		@error = "Error, expected only alphabetical characters, got: #{value}"
	end
	is_alpha
end

#decimal?(value) ⇒ Boolean

Returns true if the value is an decimal, includes positive and negative

Parameters:

  • value (String)

    the value

Returns:

  • (Boolean)

207
208
209
210
211
212
213
# File 'lib/mousevc/validation.rb', line 207

def decimal?(value)
	is_decimal = coerce_bool (value =~ /[+-]?[\d_]+\.[\d_]+(e[+-]?[\d_]+)?\b|[+-]?[\d_]+e[+-]?[\d_]+\b/i)
	unless is_decimal
		@error = "Error, expected value to be a decimal, got: #{value}"
	end
	is_decimal
end

#differs?(value, other) ⇒ Boolean

Returns true if the value is not equal to the other

Parameters:

  • value (Any)

    the value

  • other (Any)

    the other value

Returns:

  • (Boolean)

56
57
58
59
60
61
62
# File 'lib/mousevc/validation.rb', line 56

def differs?(value, other)
	is_different = coerce_bool (value != other)
	unless is_different
		@error = "Error, expected value to differ from: #{other}, got: #{value}"
	end
	is_different
end

#email?(value) ⇒ Boolean

Note:

This method is more a validation that the value is probably an email. The best way to confirm an email is to require the user to enter it twice, compare them, and ultimately send them an email!

Returns true if the value is a valid email address

Parameters:

  • value (String)

    the value

Returns:

  • (Boolean)

265
266
267
268
269
270
271
# File 'lib/mousevc/validation.rb', line 265

def email?(value)
	is_email = coerce_bool (value =~ /[email protected]+/)
	unless is_email
		@error = "Error, expected value to be an email address, got: #{value}"
	end
	is_email
end

#exact_length?(value, length) ⇒ Boolean

Returns true if the value has exactly the specified length

Parameters:

  • value (String)

    the value

  • length (Integer)

    the length

Returns:

  • (Boolean)

101
102
103
104
105
106
107
# File 'lib/mousevc/validation.rb', line 101

def exact_length?(value, length)
	has_exact_length = coerce_bool (value.length == length)
	unless has_exact_length
		@error = "Error, expected value to have exactly #{length} characters, got: #{value.length}"
	end
	has_exact_length
end

#greater_than?(value, other) ⇒ Boolean

Note:

This method does note covert strings to numbers

Returns true if the value is greater than the other given value

Parameters:

  • value (Mixed)

    the value

  • other (Mixed)

    the other value

Returns:

  • (Boolean)

117
118
119
120
121
122
123
# File 'lib/mousevc/validation.rb', line 117

def greater_than?(value, other)
	is_greater = coerce_bool (value > other)
	unless is_greater
		@error = "Error, expected value to be greater than #{other}, got: #{value}"
	end
	is_greater
end

#greater_than_equal_to?(value, other) ⇒ Boolean

Note:

This method does note covert strings to numbers

Returns true if the value is greater than or equal to the other given value

Parameters:

  • value (Mixed)

    the value

  • other (Mixed)

    the other value

Returns:

  • (Boolean)

133
134
135
136
137
138
139
# File 'lib/mousevc/validation.rb', line 133

def greater_than_equal_to?(value, other)
	is_greater_eq = coerce_bool (value >= other)
	unless is_greater_eq
		@error = "Error, expected value to be greater than or equal to #{other}, got: #{value}"
	end
	is_greater_eq
end

#integer?(value) ⇒ Boolean

Returns true if the value is an integer, includes positive and negative

Parameters:

  • value (String)

    the value

Returns:

  • (Boolean)

193
194
195
196
197
198
199
# File 'lib/mousevc/validation.rb', line 193

def integer?(value)
	is_integer = coerce_bool (value =~ /^[-+]?\d+$/)
	unless is_integer
		@error = "Error, expected value to be an integer, got: #{value}"
	end
	is_integer
end

#ip?(value) ⇒ Boolean

Returns true if the value is a valid IP address i.e. between 0.0.0.0 and 255.255.255.255

Parameters:

  • value (String)

    the value

Returns:

  • (Boolean)

279
280
281
282
283
284
285
# File 'lib/mousevc/validation.rb', line 279

def ip?(value)
	is_ip = coerce_bool (value =~ /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/)
	unless is_ip
		@error = "Error, expected value to be an IP address, got: #{value}"
	end
	is_ip
end

#less_than?(value, other) ⇒ Boolean

Note:

This method does note covert strings to numbers

Returns true if the value is less than the other given value

Parameters:

  • value (Mixed)

    the value

  • other (Mixed)

    the other value

Returns:

  • (Boolean)

149
150
151
152
153
154
155
# File 'lib/mousevc/validation.rb', line 149

def less_than?(value, other)
	is_less = coerce_bool (value < other)
	unless is_less
		@error = "Error, expected value to be less than #{other}, got: #{value}"
	end
	is_less
end

#less_than_equal_to?(value, other) ⇒ Boolean

Note:

This method does note covert strings to numbers

Returns true if the value is less than or equal to the other given value

Parameters:

  • value (Mixed)

    the value

  • other (Mixed)

    the other value

Returns:

  • (Boolean)

165
166
167
168
169
170
171
# File 'lib/mousevc/validation.rb', line 165

def less_than_equal_to?(value, other)
	is_less_eq = coerce_bool (value <= other)
	unless is_less_eq
		@error = "Error, expected value to be less than or equal to #{other}, got: #{value}"
	end
	is_less_eq
end

#matches?(value, other) ⇒ Boolean

Returns true if the value is equal to the other

Parameters:

  • value (Any)

    the value

  • other (Any)

    the other value

Returns:

  • (Boolean)

41
42
43
44
45
46
47
# File 'lib/mousevc/validation.rb', line 41

def matches?(value, other)
	is_match = coerce_bool (value == other)
	unless is_match
		@error = "Error, expected value to match: #{other}, got: #{value}"
	end
	is_match
end

#max_length?(value, length) ⇒ Boolean

Returns true if the value has at most the specified length

Parameters:

  • value (String)

    the value

  • length (Integer)

    the length

Returns:

  • (Boolean)

86
87
88
89
90
91
92
# File 'lib/mousevc/validation.rb', line 86

def max_length?(value, length)
	has_max_length = coerce_bool (value.length <= length)
	unless has_max_length
		@error = "Error, expected value to have at most #{length} characters, got: #{value.length}"
	end
	has_max_length
end

#min_length?(value, length) ⇒ Boolean

Returns true if the value has at least the specified length

Parameters:

  • value (String)

    the value

  • length (Integer)

    the length

Returns:

  • (Boolean)

71
72
73
74
75
76
77
# File 'lib/mousevc/validation.rb', line 71

def min_length?(value, length)
	has_min_length = coerce_bool (value.length >= length)
	unless has_min_length
		@error = "Error, expected value to have at least #{length} characters, got: #{value.length}"
	end
	has_min_length
end

#natural?(value) ⇒ Boolean

Returns true if the value is a positive integer

Parameters:

  • value (String)

    the value

Returns:

  • (Boolean)

221
222
223
224
225
226
227
# File 'lib/mousevc/validation.rb', line 221

def natural?(value)
	is_natural = coerce_bool (value =~ /^\d+$/)
	unless is_natural
		@error = "Error, expected value to be a natural number, got: #{value}"
	end
	is_natural
end

#natural_no_zero?(value) ⇒ Boolean

Returns true if the value is a positive integer greater than zero

Parameters:

  • value (String)

    the value

Returns:

  • (Boolean)

235
236
237
238
239
240
241
# File 'lib/mousevc/validation.rb', line 235

def natural_no_zero?(value)
	is_natural_no_zero = coerce_bool (value =~ /^[1-9]+$/)
	unless is_natural_no_zero
		@error = "Error, expected value to be a natural number above 0, got: #{value}"
	end
	is_natural_no_zero
end

#numeric?(value) ⇒ Boolean

Returns true if the value is an integer or a decimal, includes positive and negative

Parameters:

  • value (String)

    the value

Returns:

  • (Boolean)

179
180
181
182
183
184
185
# File 'lib/mousevc/validation.rb', line 179

def numeric?(value)
	is_numeric = (integer?(value) || decimal?(value))
	unless is_numeric
		@error = "Error, expected value to be numeric, got: #{value}"
	end
	is_numeric
end

#password?(value, range = 8..15) ⇒ Boolean

Returns true if the value contains only letters, numbers and has a length between the min and max of the given range

Parameters:

  • value (String)

    the value

  • range (Range) (defaults to: 8..15)

    the min and max length of the password, defaults to 8..15

Returns:

  • (Boolean)

295
296
297
298
299
300
301
# File 'lib/mousevc/validation.rb', line 295

def password?(value, range=8..15)
	is_password = coerce_bool (value =~ /^(?!.*["'])(?=.*\d)(?=.*[a-z])\S{#{range.min},#{range.max}}$/)
	unless is_password
		@error = "Error, expected value to contain only lowercase letters, numbers, and have length between #{range.min} and #{range.max}, got: #{value}"
	end
	is_password
end

#password_caps?(value, range = 8..15) ⇒ Boolean

Returns true if the value contains only letters, numbers and has a length, has a length between the min and max of the given range, and contains at least one uppercase letter

Parameters:

  • value (String)

    the value

  • range (Range) (defaults to: 8..15)

    the min and max length of the password, defaults to 8..15

Returns:

  • (Boolean)

312
313
314
315
316
317
318
# File 'lib/mousevc/validation.rb', line 312

def password_caps?(value, range=8..15)
	is_password_caps = coerce_bool (value =~ /^(?!.*["'])(?=.*\d)(?=.*[A-Z])(?=.*[a-z])\S{#{range.min},#{range.max}}$/)
	unless is_password_caps
		@error = "Error, expected value to contain lowercase letters, numbers, have length between #{range.min} and #{range.max} and at least one uppercase letter, got: #{value}"
	end
	is_password_caps
end

#password_symbols?(value, range = 8..15) ⇒ Boolean

Note:

Excludes single and double quotes.

Returns true if the value contains only letters, numbers and has a length, has a length between the min and max of the given range, contains at least one uppercase letter, and contains at least one symbol

Parameters:

  • value (String)

    the value

  • range (Range) (defaults to: 8..15)

    the min and max length of the password, defaults to 8..15

Returns:

  • (Boolean)

332
333
334
335
336
337
338
# File 'lib/mousevc/validation.rb', line 332

def password_symbols?(value, range=8..15)
	is_password_symbols = coerce_bool (value =~ /^(?!.*["'])(?=.*[[email protected]#$%^&*()_+\-\\|{}<>\[\]:;?\/])(?=.*\d)(?=.*[A-Z])(?=.*[a-z])\S{#{range.min},#{range.max}}$/)
	unless is_password_symbols
		@error = @error = "Error, expected value to contain lowercase letters, numbers, have length between #{range.min} and #{range.max}, at least one uppercase letter and at least one symbol, got: #{value}"
	end
	is_password_symbols
end

#url?(value) ⇒ Boolean

Returns true if the value is a valid url and accounts for many edge cases

Parameters:

  • value (String)

    the value

Returns:

  • (Boolean)

249
250
251
252
253
254
255
# File 'lib/mousevc/validation.rb', line 249

def url?(value)
	is_url = coerce_bool (value =~ /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)[email protected])?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-{1})*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-{1})*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[\/?#]\S*)?$/i)
	unless is_url
		@error = "Error, expected value to be a url, got: #{value}"
	end
	is_url
end