Class: YolCommon::Validator
- Inherits:
-
Object
- Object
- YolCommon::Validator
- Defined in:
- lib/yol_common/validator.rb
Class Method Summary collapse
-
.asset_file_ext?(file_name) ⇒ Boolean
静态文件后缀.
-
.asset_file_name?(file_name) ⇒ Boolean
静态文件名.
-
.bank_card?(card) ⇒ Boolean
银行卡.
-
.date?(date) ⇒ Boolean
日期.
-
.domain_name?(name) ⇒ Boolean
域名.
-
.email?(email) ⇒ Boolean
邮箱.
-
.float?(float) ⇒ Boolean
float.
-
.folder_name?(str) ⇒ Boolean
方法说明.
-
.icon?(file_name) ⇒ Boolean
icon.
-
.image?(file_name) ⇒ Boolean
图片类型.
-
.image_asset_file_name?(file_name) ⇒ Boolean
图片文件名字.
-
.indentity_card?(card) ⇒ Boolean
验证身份证号码.
-
.integer?(integer) ⇒ Boolean
整数.
-
.is_mobile_agent?(agent) ⇒ Boolean
手机.
-
.json?(file_name) ⇒ Boolean
json.
-
.max_length?(target, length = 255) ⇒ Boolean
方法说明.
-
.min_length?(target, length = 10) ⇒ Boolean
方法说明.
-
.mobile?(mobile) ⇒ Boolean
手机.
-
.password?(pwd) ⇒ Boolean
密码.
-
.positive_integer?(str) ⇒ 返回数据类型
方法说明.
-
.price?(str) ⇒ Boolean
方法说明.
-
.under_size?(size_in_byte, limit_mb) ⇒ Boolean
方法说明.
-
.url?(url) ⇒ Boolean
url.
Class Method Details
.asset_file_ext?(file_name) ⇒ Boolean
静态文件后缀
43 44 45 46 47 48 |
# File 'lib/yol_common/validator.rb', line 43 def self.asset_file_ext?(file_name) name = file_name.to_s.downcase ext = File.extname(name) return false if ext.empty? %w{.eot .woff .woff2 .ttf .svg .js .css .htc .ico .json .otf}.include?(ext) end |
.asset_file_name?(file_name) ⇒ Boolean
静态文件名
35 36 37 38 39 40 |
# File 'lib/yol_common/validator.rb', line 35 def self.asset_file_name?(file_name) ext = File.extname(file_name) file_name = File.basename(file_name, ext) unless ext.empty? return true if file_name =~ /^[\w\.\-\_]+$/ false end |
.bank_card?(card) ⇒ Boolean
银行卡
154 155 156 157 |
# File 'lib/yol_common/validator.rb', line 154 def self.bank_card?(card) return true if card.to_s =~ /^\d{9,20}$/ return false end |
.date?(date) ⇒ Boolean
日期
97 98 99 100 101 102 103 104 |
# File 'lib/yol_common/validator.rb', line 97 def self.date?(date) begin Date.parse(date) return true rescue return false end end |
.domain_name?(name) ⇒ Boolean
域名
66 67 68 69 70 |
# File 'lib/yol_common/validator.rb', line 66 def self.domain_name?(name) return false if name.length < 1 || name.length > 63 return true if name.to_s =~ /^[a-z0-9-]+$/ false end |
.email?(email) ⇒ Boolean
邮箱
15 16 17 18 19 20 |
# File 'lib/yol_common/validator.rb', line 15 def self.email?(email) return false if email.length <= 3 || email.length >= 255 # email如包含\xBF则会报错:invalid byte sequence in UTF-8,在ruby2.1可以用scrub函数去掉错误编码的字符 return true if email.to_s =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i rescue nil false end |
.float?(float) ⇒ Boolean
float
117 118 119 120 121 122 123 124 |
# File 'lib/yol_common/validator.rb', line 117 def self.float?(float) begin Float(float) return true rescue return false end end |
.folder_name?(str) ⇒ Boolean
方法说明
171 172 173 174 |
# File 'lib/yol_common/validator.rb', line 171 def self.folder_name?(str) return true if str.to_s =~ /^[^\\\/:*?"<>|]{1,}$/ false end |
.icon?(file_name) ⇒ Boolean
icon
81 82 83 84 85 86 |
# File 'lib/yol_common/validator.rb', line 81 def self.icon?(file_name) name = file_name.to_s.downcase ext = File.extname(name) return false if ext.empty? %w{.ico}.include?(ext) end |
.image?(file_name) ⇒ Boolean
图片类型
73 74 75 76 77 78 |
# File 'lib/yol_common/validator.rb', line 73 def self.image?(file_name) name = file_name.to_s.downcase ext = File.extname(name) return false if ext.empty? %w{.jpg .jpeg .gif .png .bmp .webp .ico}.include?(ext) end |
.image_asset_file_name?(file_name) ⇒ Boolean
图片文件名字
51 52 53 54 55 56 |
# File 'lib/yol_common/validator.rb', line 51 def self.image_asset_file_name?(file_name) ext = File.extname(file_name) file_name = File.basename(file_name, ext) unless ext.empty? return true if file_name =~ /^[\w\-\_]+$/ false end |
.indentity_card?(card) ⇒ Boolean
验证身份证号码
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/yol_common/validator.rb', line 127 def self.indentity_card?(card) arr_int = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] arr_ch = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] case card.size when 15 chars = card.chars.to_a cardtemp_15 = 0 chars.insert(6,'1','9') 17.times do |i| cardtemp_15 += (chars[i].to_i * arr_int[i]) end chars << arr_ch[cardtemp_15 % 11] when 18 chars = card.chars.to_a else return false end cardtemp = 0 17.times do |i| cardtemp += chars[i].to_i * arr_int[i] end arr_ch[cardtemp % 11].eql?(chars.last) end |
.integer?(integer) ⇒ Boolean
整数
107 108 109 110 111 112 113 114 |
# File 'lib/yol_common/validator.rb', line 107 def self.integer?(integer) begin Integer(integer) return true rescue return false end end |
.is_mobile_agent?(agent) ⇒ Boolean
手机
9 10 11 12 |
# File 'lib/yol_common/validator.rb', line 9 def self.is_mobile_agent?(agent) return true if agent.to_s.downcase =~ MOBILE_REGEX false end |
.json?(file_name) ⇒ Boolean
json
89 90 91 92 93 94 |
# File 'lib/yol_common/validator.rb', line 89 def self.json?(file_name) name = file_name.to_s.downcase ext = File.extname(name) return false if ext.empty? ['.json'].include?(ext) end |
.max_length?(target, length = 255) ⇒ Boolean
方法说明
186 187 188 |
# File 'lib/yol_common/validator.rb', line 186 def self.max_length?(target, length = 255) target.to_s.length > length end |
.min_length?(target, length = 10) ⇒ Boolean
方法说明
191 192 193 |
# File 'lib/yol_common/validator.rb', line 191 def self.min_length?(target, length = 10) target.to_s.length < length end |
.mobile?(mobile) ⇒ Boolean
手机
23 24 25 26 |
# File 'lib/yol_common/validator.rb', line 23 def self.mobile?(mobile) return true if mobile.to_s =~ /^(13|14|15|16|17|18|19)\d{9}$/ false end |
.password?(pwd) ⇒ Boolean
密码
29 30 31 32 |
# File 'lib/yol_common/validator.rb', line 29 def self.password?(pwd) return true if pwd.length >=4 && pwd.length <= 40 false end |
.positive_integer?(str) ⇒ 返回数据类型
方法说明
180 181 182 183 |
# File 'lib/yol_common/validator.rb', line 180 def self.positive_integer?(str) return true if str.to_s =~ /^(0|[1-9]\d*)$/ false end |
.price?(str) ⇒ Boolean
方法说明
165 166 167 168 |
# File 'lib/yol_common/validator.rb', line 165 def self.price?(str) return true if str.to_s =~ /^[0-9]+([.]{1}[0-9]{1,2})?$/ false end |
.under_size?(size_in_byte, limit_mb) ⇒ Boolean
方法说明
160 161 162 |
# File 'lib/yol_common/validator.rb', line 160 def self.under_size?(size_in_byte, limit_mb) size_in_byte <= limit_mb * 1024 * 1024 end |
.url?(url) ⇒ Boolean
url
59 60 61 62 63 |
# File 'lib/yol_common/validator.rb', line 59 def self.url?(url) # return true if url.to_s =~ /https?:\/\/[\S]+/ return true if url.to_s.scan(URI.regexp).length > 0 false end |