Class: LitleOnline::SchemaValidation

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

Class Method Summary collapse

Class Method Details

.validate_boolean(value, required, class_name, field_name) ⇒ Object



127
128
129
# File 'lib/XMLFields.rb', line 127

def self.validate_boolean(value, required, class_name, field_name)
  validate_enum(value, required, ['true','false','1','0'], class_name, field_name)
end

.validate_country(value, class_name, field_name) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/XMLFields.rb', line 88

def self.validate_country(value, class_name, field_name)
  if(value.nil?)
    return
  end
  list = ["USA","AF","AX","AL","DZ","AS","AD","AO","AI","AQ","AG","AR","AM","AW","AU","AT","AZ","BS","BH","BD","BB","BY","BE","BZ","BJ","BM","BT","BO","BQ","BA","BW","BV","BR","IO","BN","BG","BF","BI","KH","CM","CA","CV","KY","CF","TD","CL","CN","CX","CC","CO","KM","CG","CD","CK","CR","CI","HR","CU","CW","CY","CZ","DK","DJ","DM","DO","TL","EC","EG","SV","GQ","ER","EE","ET","FK","FO","FJ","FI","FR","GF","PF","TF","GA","GM","GE","DE","GH","GI","GR","GL","GD","GP","GU","GT","GG","GN","GW","GY","HT","HM","HN","HK","HU","IS","IN","ID","IR","IQ","IE","IM","IL","IT","JM","JP","JE","JO","KZ","KE","KI","KP","KR","KW","KG","LA","LV","LB","LS","LR","LY","LI","LT","LU","MO","MK","MG","MW","MY","MV","ML","MT","MH","MQ","MR","MU","YT","MX","FM","MD","MC","MN","MS","MA","MZ","MM","NA","NR","NP","NL","AN","NC","NZ","NI","NE","NG","NU","NF","MP","NO","OM","PK","PW","PS","PA","PG","PY","PE","PH","PN","PL","PT","PR","QA","RE","RO","RU","RW","BL","KN","LC","MF","VC","WS","SM","ST","SA","SN","SC","SL","SG","SX","SK","SI","SB","SO","ZA","GS","ES","LK","SH","PM","SD","SR","SJ","SZ","SE","CH","SY","TW","TJ","TZ","TH","TG","TK","TO","TT","TN","TR","TM","TC","TV","UG","UA","AE","GB","US","UM","UY","UZ","VU","VA","VE","VN","VG","VI","WF","EH","YE","ZM","ZW","RS","ME"]
  if(!list.include? value)
    raise "If " + class_name + " " + field_name + " is specified, it must be valid.  You specified " + value
  end
end

.validate_currency(value, required, class_name, field_name) ⇒ Object



123
124
125
# File 'lib/XMLFields.rb', line 123

def self.validate_currency(value, required, class_name, field_name)
  validate_enum(value, required, ['AUD','CAD','CHF','DKK','EUR','GBP','HKD','JPY','NOK','NZD','SEK','SGD','USD'], class_name, field_name)
end

.validate_date(value, required, class_name, field_name) ⇒ Object



131
132
133
# File 'lib/XMLFields.rb', line 131

def self.validate_date(value, required, class_name, field_name)
  validate_regex(value, required, /\A\d{4}-\d{2}-\d{2}\Z/, class_name, field_name)
end

.validate_enum(value, required, list, class_name, field_name) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/XMLFields.rb', line 108

def self.validate_enum(value, required, list, class_name, field_name)
  validate_required(value, required, class_name, field_name)
  if(value.nil?)
    return
  end
  if(!list.include?(value))
    str = '["'+ list.join('", "') + '"]'
    raise "If #{class_name} #{field_name} is specified, it must be in #{str}"
  end
end

.validate_length(value, required, min, max, class_name, field_name) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/XMLFields.rb', line 68

def self.validate_length(value, required, min, max, class_name, field_name)
  validate_required(value, required, class_name, field_name)
  if(value.nil?)
    return
  end
  if(value.length < min || value.length > max)
    raise "If " + class_name + " " + field_name + " is specified, it must be between " + min.to_s + " and " + max.to_s + " characters long"
  end
end

.validate_long(value, required, class_name, field_name) ⇒ Object



119
120
121
# File 'lib/XMLFields.rb', line 119

def self.validate_long(value, required, class_name, field_name)
  validate_size(value, required, -9223372036854775808, 9223372036854775807, class_name, field_name)
end

.validate_regex(value, required, regex, class_name, field_name) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/XMLFields.rb', line 98

def self.validate_regex(value, required,  regex, class_name, field_name)
  validate_required(value, required, class_name, field_name)
  if(value.nil?)
    return
  end
  if(!(regex =~ value))
    raise "If #{class_name} #{field_name} is specified, it must match the regular expression #{regex.inspect}"
  end
end

.validate_required(value, required, class_name, field_name) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/XMLFields.rb', line 60

def self.validate_required(value, required, class_name, field_name)
  if(required)
    if(value.nil?)
      raise "If #{class_name} is specified, it must have a #{field_name}"
    end
  end
end

.validate_size(value, required, min, max, class_name, field_name) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/XMLFields.rb', line 78

def self.validate_size(value, required, min, max, class_name, field_name)
  validate_required(value, required, class_name, field_name)
  if(value.nil?)
    return
  end
  if(value.to_i < min || value.to_i > max || !(/\A\-?\d+\Z/ =~ value))
    raise "If " + class_name + " " + field_name + " is specified, it must be between " + min.to_s + " and " + max.to_s
  end
end