Module: ValidationRules
- Defined in:
- lib/validation_rules.rb,
lib/validation_rules/version.rb,
lib/validation_rules/validation_rules.rb
Constant Summary collapse
- VERSION =
'1.0.5'
- EMAIL_REGEX =
/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/i
- ALPHA_REGEX =
/^[[:alpha:]]+$/
- ALPHA_DASH_REGEX =
/^[a-z\-_]+$/i
- ALPHA_DASH_DIGIT_REGEX =
/^[a-z\-_0-9]+$/i
- ALPHA_DASH_SLASH_REGEX =
/^[a-z\-_\/]+$/i
- ALPHA_NUMERIC_REGEX =
/^[[:alnum:]]+$/
- ALPHA_NUMERIC_DASH_REGEX =
/^[[:alnum:]\-_]+$/i
- NUMERIC_REGEX =
/^[0-9]+$/
- DATE_REGEX =
/^((?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])$/
- ISO8601_REGEX =
/^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])(T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?)?$/
- IP_REGEX =
/^(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))$/
- UUID_REGEX =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
Class Method Summary collapse
- .alpha(value) ⇒ Object
-
.alpha_dash(value) ⇒ Object
Validates for letters, underscores and dashes.
-
.alpha_dash_digit(value) ⇒ Object
Validates for letters, numbers, underscores and dashes.
-
.alpha_dash_slash(value) ⇒ Object
Validates for letters, numbers, forward-slashes, underscores and dashes.
- .alpha_numeric(value) ⇒ Object
-
.alpha_numeric_dash(value) ⇒ Object
Validates alphanumeric, dashes and underscores.
- .any_bool(value) ⇒ Object
- .between_dates(value, date1, date2) ⇒ Object
- .bool(value) ⇒ Object
- .bson_object_id(value) ⇒ Object
- .date(value) ⇒ Object
- .decimal(value, precision = 5, scale = 2) ⇒ Object
- .email(value) ⇒ Object
- .future_date(value) ⇒ Object
- .integer(value) ⇒ Object
- .ip_address(value) ⇒ Object
- .iso8601(value) ⇒ Object
- .json(value) ⇒ Object
- .length(value, length) ⇒ Object
- .matches(value, value1) ⇒ Object
- .max_length(value, length) ⇒ Object
- .min_length(value, length) ⇒ Object
- .money(value, decimals = 2) ⇒ Object
- .negative(value) ⇒ Object
- .numeric(value) ⇒ Object
- .numeric_max(value, max) ⇒ Object
- .numeric_min(value, min) ⇒ Object
- .past_date(value) ⇒ Object
- .positive(value) ⇒ Object
- .range(value, min, max) ⇒ Object
- .string(value) ⇒ Object
- .url(value) ⇒ Object
- .uuid(value) ⇒ Object
Class Method Details
.alpha(value) ⇒ Object
20 21 22 |
# File 'lib/validation_rules/validation_rules.rb', line 20 def self.alpha(value) value =~ ALPHA_REGEX end |
.alpha_dash(value) ⇒ Object
Validates for letters, underscores and dashes
25 26 27 |
# File 'lib/validation_rules/validation_rules.rb', line 25 def self.alpha_dash(value) value =~ ALPHA_DASH_REGEX end |
.alpha_dash_digit(value) ⇒ Object
Validates for letters, numbers, underscores and dashes
30 31 32 |
# File 'lib/validation_rules/validation_rules.rb', line 30 def self.alpha_dash_digit(value) value =~ ALPHA_DASH_DIGIT_REGEX end |
.alpha_dash_slash(value) ⇒ Object
Validates for letters, numbers, forward-slashes, underscores and dashes
35 36 37 |
# File 'lib/validation_rules/validation_rules.rb', line 35 def self.alpha_dash_slash(value) value =~ ALPHA_DASH_SLASH_REGEX end |
.alpha_numeric(value) ⇒ Object
39 40 41 |
# File 'lib/validation_rules/validation_rules.rb', line 39 def self.alpha_numeric(value) value =~ ALPHA_NUMERIC_REGEX end |
.alpha_numeric_dash(value) ⇒ Object
Validates alphanumeric, dashes and underscores
44 45 46 |
# File 'lib/validation_rules/validation_rules.rb', line 44 def self.alpha_numeric_dash(value) value =~ ALPHA_NUMERIC_DASH_REGEX end |
.any_bool(value) ⇒ Object
160 161 162 |
# File 'lib/validation_rules/validation_rules.rb', line 160 def self.any_bool(value) [true, false, 0, 1, "0", "1", "true", "false"].include?(value) end |
.between_dates(value, date1, date2) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/validation_rules/validation_rules.rb', line 83 def self.between_dates(value, date1, date2) value = Time.parse(value) if value.is_a? String date1 = Time.parse(date1) if date1.is_a? String date2 = Time.parse(date2) if date2.is_a? String value.between?(date1, date2) end |
.bool(value) ⇒ Object
156 157 158 |
# File 'lib/validation_rules/validation_rules.rb', line 156 def self.bool(value) !!value == value end |
.bson_object_id(value) ⇒ Object
65 66 67 |
# File 'lib/validation_rules/validation_rules.rb', line 65 def self.bson_object_id(value) ::BSON::ObjectId.legal? value end |
.date(value) ⇒ Object
48 49 50 |
# File 'lib/validation_rules/validation_rules.rb', line 48 def self.date(value) value =~ DATE_REGEX end |
.decimal(value, precision = 5, scale = 2) ⇒ Object
56 57 58 59 |
# File 'lib/validation_rules/validation_rules.rb', line 56 def self.decimal(value, precision = 5, scale = 2) before, after = precision - scale, scale value.to_s =~ /^[-+]?\d{0,#{before}}?(?:\.\d{0,#{after}})?$/ end |
.email(value) ⇒ Object
61 62 63 |
# File 'lib/validation_rules/validation_rules.rb', line 61 def self.email(value) value =~ EMAIL_REGEX end |
.future_date(value) ⇒ Object
73 74 75 76 |
# File 'lib/validation_rules/validation_rules.rb', line 73 def self.future_date(value) value = Time.parse(value) if value.is_a? String value.to_i >= Time.now.to_i end |
.integer(value) ⇒ Object
69 70 71 |
# File 'lib/validation_rules/validation_rules.rb', line 69 def self.integer(value) value.is_a? Integer end |
.ip_address(value) ⇒ Object
173 174 175 |
# File 'lib/validation_rules/validation_rules.rb', line 173 def self.ip_address(value) value =~ IP_REGEX end |
.iso8601(value) ⇒ Object
52 53 54 |
# File 'lib/validation_rules/validation_rules.rb', line 52 def self.iso8601(value) value =~ ISO8601_REGEX end |
.json(value) ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/validation_rules/validation_rules.rb', line 95 def self.json(value) return false unless value begin JSON.parse(value) rescue JSON::ParserError return false end return true end |
.length(value, length) ⇒ Object
110 111 112 |
# File 'lib/validation_rules/validation_rules.rb', line 110 def self.length(value, length) value.to_s.length == length end |
.matches(value, value1) ⇒ Object
114 115 116 |
# File 'lib/validation_rules/validation_rules.rb', line 114 def self.matches(value, value1) value == value1 end |
.max_length(value, length) ⇒ Object
118 119 120 |
# File 'lib/validation_rules/validation_rules.rb', line 118 def self.max_length(value, length) value.to_s.length <= length end |
.min_length(value, length) ⇒ Object
122 123 124 |
# File 'lib/validation_rules/validation_rules.rb', line 122 def self.min_length(value, length) value.to_s.length >= length end |
.money(value, decimals = 2) ⇒ Object
106 107 108 |
# File 'lib/validation_rules/validation_rules.rb', line 106 def self.money(value, decimals = 2) value.to_s =~ /^[-+]?([0-9]+)?(?:\.[0-9]{0,#{decimals}})?$/ end |
.negative(value) ⇒ Object
146 147 148 149 |
# File 'lib/validation_rules/validation_rules.rb', line 146 def self.negative(value) return false unless numeric(value) value.to_f < 0 end |
.numeric(value) ⇒ Object
126 127 128 129 |
# File 'lib/validation_rules/validation_rules.rb', line 126 def self.numeric(value) return true if value =~ /^\d+$/ true if Float(value) rescue false end |
.numeric_max(value, max) ⇒ Object
136 137 138 139 |
# File 'lib/validation_rules/validation_rules.rb', line 136 def self.numeric_max(value, max) return false unless numeric(value) value.to_f <= max.to_f end |
.numeric_min(value, min) ⇒ Object
131 132 133 134 |
# File 'lib/validation_rules/validation_rules.rb', line 131 def self.numeric_min(value, min) return false unless numeric(value) value.to_f >= min.to_f end |
.past_date(value) ⇒ Object
78 79 80 81 |
# File 'lib/validation_rules/validation_rules.rb', line 78 def self.past_date(value) value = Time.parse(value) if value.is_a? String value.to_i <= Time.now.to_i end |
.positive(value) ⇒ Object
141 142 143 144 |
# File 'lib/validation_rules/validation_rules.rb', line 141 def self.positive(value) return false unless numeric(value) value.to_f > 0 end |
.range(value, min, max) ⇒ Object
151 152 153 154 |
# File 'lib/validation_rules/validation_rules.rb', line 151 def self.range(value, min, max) return false unless numeric(value) value.to_f >= min.to_f and value.to_f <= max.to_f end |
.string(value) ⇒ Object
91 92 93 |
# File 'lib/validation_rules/validation_rules.rb', line 91 def self.string(value) value.is_a? String end |
.url(value) ⇒ Object
164 165 166 167 168 169 170 171 |
# File 'lib/validation_rules/validation_rules.rb', line 164 def self.url(value) begin uri = URI.parse(value) uri.kind_of? URI::HTTP rescue URI::InvalidURIError false end end |
.uuid(value) ⇒ Object
177 178 179 |
# File 'lib/validation_rules/validation_rules.rb', line 177 def self.uuid(value) value =~ UUID_REGEX end |