Class: ValidatorUtils::GeneralValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/ig-validator-utils.rb

Class Method Summary collapse

Class Method Details

.validate_address(value) ⇒ Object

pL matches a single unicode letter pN matches a single unicode number allow points, comma, apostrophe, hyphen, spaces



23
24
25
# File 'lib/ig-validator-utils.rb', line 23

def self.validate_address(value)
  value =~ /^[\p{L}\p{N} .,'-]+$/i
end

.validate_base_64(value) ⇒ Object

validates base64 encoding (includes newline constants ‘n’)



71
72
73
# File 'lib/ig-validator-utils.rb', line 71

def self.validate_base_64(value)
  value =~ /^[A-Za-z0-9+\/=\\]+={0,3}$/
end

.validate_boolean(value) ⇒ Object



31
32
33
# File 'lib/ig-validator-utils.rb', line 31

def self.validate_boolean(value)
  !!value == value
end

.validate_email(value) ⇒ Object



79
80
81
# File 'lib/ig-validator-utils.rb', line 79

def self.validate_email(value)
  value =~ /^[\w+\-.]+@[a-z\d\-.]+\.[a-z]+$/
end

.validate_hex(value) ⇒ Object



39
40
41
# File 'lib/ig-validator-utils.rb', line 39

def self.validate_hex(value)
  value =~ /^[a-f\d]{24}$/i
end

.validate_integer(value) ⇒ Object



27
28
29
# File 'lib/ig-validator-utils.rb', line 27

def self.validate_integer(value)
  Float(value) != nil rescue false
end

.validate_mobile(value) ⇒ Object

accepts only numbers with international codes in the format +12 12345678



84
85
86
87
88
# File 'lib/ig-validator-utils.rb', line 84

def self.validate_mobile(value)
  # strip out spaces first
  stripped = value.split.join
  stripped =~ /^(\+\d{1,3}[-]?)\d{8,12}$/
end

.validate_password(value) ⇒ Object

At least one upper case letter At least one lower case letter At least one digit Minimum 6 in length Maximum 12 in length



48
49
50
# File 'lib/ig-validator-utils.rb', line 48

def self.validate_password(value)
  value =~ /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{6,12}$/
end

.validate_public_ecdsa_key(value) ⇒ Object

for a 256-bit ECDSA curve, the uncompressed pubkey is 512 bits (256 bits of x, 256 bits of y, no sign bit). the compressed pubkey is 257 bits (256 bits of x, one bit of the sign of y). this equates to 32 bytes (ie: 256/8 = 32) + 1 (the sign) = 33



55
56
57
58
# File 'lib/ig-validator-utils.rb', line 55

def self.validate_public_ecdsa_key(value)
  decoded_key = Base64.decode64 value
  decoded_key.length == 33
end

.validate_string(value) ⇒ Object



3
4
5
# File 'lib/ig-validator-utils.rb', line 3

def self.validate_string(value)
  value.to_s != ''
end

.validate_string_strict(value) ⇒ Object

pL matches a single unicode letter also allows space, points, apostrophe and hyphen



16
17
18
# File 'lib/ig-validator-utils.rb', line 16

def self.validate_string_strict(value)
value =~ /^[\p{L} .'-]+$/i
end

.validate_unix_datetime(value) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/ig-validator-utils.rb', line 60

def self.validate_unix_datetime(value)
  begin
    now = Date.today.to_time
    time_to_validate = Time.at value
    return time_to_validate > now
  rescue
    return false
  end
end

.validate_uri(value) ⇒ Object



75
76
77
# File 'lib/ig-validator-utils.rb', line 75

def self.validate_uri(value)
  value =~ /^(http|https):\/\/[a-z0-9]+([\-\.][a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/
end

.validate_username_strict(value) ⇒ Object

pL matches a single unicode letter also allows ‘.’, ‘-’, ‘_’, ‘@’ takes into account usernames that are email addresses



10
11
12
# File 'lib/ig-validator-utils.rb', line 10

def self.validate_username_strict(value)
  value =~ /^[\p{L} .-@_]+$/i
end

.validate_uuid(value) ⇒ Object



35
36
37
# File 'lib/ig-validator-utils.rb', line 35

def self.validate_uuid(value)
  value =~ /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
end