Module: JSONModel::Validations
- Extended by:
- JSONModel
- Defined in:
- common/validations.rb
Class Method Summary
(collapse)
Methods included from JSONModel
JSONModel, JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, client_mode?, custom_validations, destroy_model, enum_default_value, enum_values, handle_error, init, load_schema, models, models, parse_jsonmodel_ref, parse_reference, repository, repository_for, schema_src, set_repository, strict_mode, strict_mode?, with_repository
Class Method Details
+ (Object) check_archival_object(hash)
400
401
402
403
404
405
406
407
408
409
|
# File 'common/validations.rb', line 400
def self.check_archival_object(hash)
errors = []
if (!hash.has_key?("dates") || hash["dates"].empty?) && (!hash.has_key?("title") || hash["title"].empty?)
errors << ["dates", "one or more required (or enter a Title)"]
errors << ["title", "must not be an empty string (or enter a Date)"]
end
errors
end
|
+ (Object) check_authority_id(hash)
https://www.pivotaltracker.com/story/show/51373893
50
51
52
53
54
55
56
57
|
# File 'common/validations.rb', line 50
def self.check_authority_id(hash)
warnings = []
if hash["source"].nil? && hash["authority_id"]
warnings << ["source", "is required if there is an authority id"]
end
warnings
end
|
+ (Object) check_collection_management(hash)
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
# File 'common/validations.rb', line 332
def self.check_collection_management(hash)
errors = []
if !hash["processing_total_extent"].nil? and hash["processing_total_extent_type"].nil?
errors << ["processing_total_extent_type", "is required if total extent is specified"]
end
[ "processing_hours_per_foot_estimate", "processing_total_extent", "processing_hours_total" ].each do |k|
if !hash[k].nil? and hash[k] !~ /^\-?\d{0,9}(\.\d{1,5})?$/
errors << [k, "must be a number with no more than nine digits and five decimal places"]
end
end
errors
end
|
+ (Object) check_container(hash)
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
# File 'common/validations.rb', line 220
def self.check_container(hash)
errors = []
got_current = false
required_container_fields = [["barcode_1"],
["type_1", "indicator_1"]]
if !required_container_fields.any? { |fieldset| fieldset.all? {|field| hash[field]} }
errors << [ :type_1, "either type_1 or barcode is required" ]
end
if !hash["container_extent_number"].nil? and hash["container_extent_number"] !~ /^\-?\d{0,9}(\.\d{1,5})?$/
errors << ["container_extent", "must be a number with no more than nine digits and five decimal places"]
elsif !hash["container_extent"].nil? and hash["container_extent_type"].nil?
errors << ["container_extent_type", "is required if container extent is specified "]
end
Array(hash["container_locations"]).each do |loc|
if loc["status"] == "current"
if got_current
errors << ["container_locations", "only one location can be current"]
break
else
got_current = true
end
end
end
errors
end
|
+ (Object) check_container_location(hash)
204
205
206
207
208
209
210
|
# File 'common/validations.rb', line 204
def self.check_container_location(hash)
errors = []
errors << ["end_date", "is required if status is previous"] if hash["end_date"].nil? and hash["status"] == "previous"
errors
end
|
+ (Object) check_container_profile(hash)
312
313
314
315
316
317
318
319
320
321
322
323
|
# File 'common/validations.rb', line 312
def self.check_container_profile(hash)
errors = []
["depth", "width", "height"].each do |k|
if !hash[k].nil? && hash[k] !~ /\A\d+(\.\d\d?)?\Z/
errors << [k, "must be a number with no more than 2 decimal places"]
end
end
errors
end
|
+ (Object) check_date(hash)
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'common/validations.rb', line 107
def self.check_date(hash)
errors = []
begin
begin_date = parse_sloppy_date(hash['begin']) if hash['begin']
rescue ArgumentError => e
errors << ["begin", "not a valid date"]
end
begin
if hash['end']
end_s = if begin_date && hash['begin'] && hash['begin'].start_with?(hash['end'])
hash['begin']
else
hash['end']
end
end_date = parse_sloppy_date(end_s)
end
rescue ArgumentError
errors << ["end", "not a valid date"]
end
if begin_date && end_date && end_date < begin_date
errors << ["end", "must not be before begin"]
end
if hash["expression"].nil? && hash["begin"].nil? && hash["end"].nil?
errors << ["expression", "is required unless a begin or end date is given"]
errors << ["begin", "is required unless an expression or an end date is given"]
errors << ["end", "is required unless an expression or a begin date is given"]
end
errors
end
|
+ (Object) check_digital_object_component(hash)
424
425
426
427
428
429
430
431
432
433
434
435
436
|
# File 'common/validations.rb', line 424
def self.check_digital_object_component(hash)
errors = []
fields = ["dates", "title", "label"]
if fields.all? {|field| !hash.has_key?(field) || hash[field].empty?}
fields.each do |field|
errors << [field, "you must provide a label, title or date"]
end
end
errors
end
|
+ (Object) check_identifier(hash)
9
10
11
12
13
14
15
16
17
18
19
|
# File 'common/validations.rb', line 9
def self.check_identifier(hash)
ids = (0...4).map {|i| hash["id_#{i}"]}
errors = []
if ids.reverse.drop_while {|elt| elt.to_s.empty?}.any?{|elt| elt.to_s.empty?}
errors << ["identifier", "must not contain blank entries"]
end
errors
end
|
+ (Object) check_instance(hash)
272
273
274
275
276
277
278
|
# File 'common/validations.rb', line 272
def self.check_instance(hash)
if hash['sub_container']
[]
else
check_instance_pre_managed_container(hash)
end
end
|
+ (Object) check_instance_pre_managed_container(hash)
259
260
261
262
263
264
265
266
267
268
269
270
|
# File 'common/validations.rb', line 259
def self.check_instance_pre_managed_container(hash)
errors = []
if hash["instance_type"] == "digital_object"
errors << ["digital_object", "Can't be empty"] if hash["digital_object"].nil?
elsif hash["instance_type"]
errors << ["container", "Can't be empty"] if hash["container"].nil?
end
errors
end
|
+ (Object) check_location(hash)
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
# File 'common/validations.rb', line 178
def self.check_location(hash)
errors = []
required_location_fields = [["barcode"],
["classification"],
["coordinate_1_indicator", "coordinate_1_label"]]
if !required_location_fields.any? { |fieldset| fieldset.all? {|field| hash[field]} }
errors << :location_fields_error
end
errors
end
|
+ (Object) check_name(hash)
59
60
61
62
63
|
# File 'common/validations.rb', line 59
def self.check_name(hash)
errors = []
errors << ["sort_name", "Property is required but was missing"] if hash["sort_name"].nil? and !hash["sort_name_auto_generate"]
errors
end
|
+ (Object) check_otherlevel(hash)
390
391
392
393
394
395
396
397
398
|
# File 'common/validations.rb', line 390
def self.check_otherlevel(hash)
warnings = []
if hash["level"] == "otherlevel"
warnings << ["other_level", "is required"] if hash["other_level"].nil?
end
warnings
end
|
+ (Object) check_rights_statement(hash)
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'common/validations.rb', line 154
def self.check_rights_statement(hash)
errors = []
if hash["rights_type"] == "intellectual_property"
errors << ["ip_status", "missing required property"] if hash["ip_status"].nil?
errors << ["jurisdiction", "missing required property"] if hash["jurisdiction"].nil?
elsif hash["rights_type"] == "license"
errors << ["license_identifier_terms", "missing required property"] if hash["license_identifier_terms"].nil?
elsif hash["rights_type"] == "statute"
errors << ["statute_citation", "missing required property"] if hash["statute_citation"].nil?
errors << ["jurisdiction", "missing required property"] if hash["jurisdiction"].nil?
end
errors
end
|
+ (Object) check_source(hash)
Specification:
https://www.pivotaltracker.com/story/show/41430143
See also: https://www.pivotaltracker.com/story/show/51373893
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'common/validations.rb', line 33
def self.check_source(hash)
errors = []
return errors if !hash['authorized']
if hash["source"].nil?
if hash["rules"].nil?
errors << ["rules", "is required when 'source' is blank"]
errors << ["source", "is required when 'rules' is blank"]
end
end
errors
end
|
+ (Object) check_sub_container(hash)
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
# File 'common/validations.rb', line 287
def self.check_sub_container(hash)
errors = []
if (!hash["type_2"].nil? && hash["indicator_2"].nil?) || (hash["type_2"].nil? && !hash["indicator_2"].nil?)
errors << ["type_2", "container 2 requires both a type and indicator"]
end
if (hash["type_2"].nil? && hash["indicator_2"].nil? && (!hash["type_3"].nil? || !hash["indicator_3"].nil?))
errors << ["type_2", "container 2 is required if container 3 is provided"]
end
if (!hash["type_3"].nil? && hash["indicator_3"].nil?) || (hash["type_3"].nil? && !hash["indicator_3"].nil?)
errors << ["type_3", "container 3 requires both a type and indicator"]
end
errors
end
|
+ (Object) check_user_defined(hash)
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
# File 'common/validations.rb', line 357
def self.check_user_defined(hash)
errors = []
["integer_1", "integer_2", "integer_3"].each do |k|
if !hash[k].nil? and hash[k] !~ /^\-?\d+$/
errors << [k, "must be an integer"]
end
end
["real_1", "real_2", "real_3"].each do |k|
if !hash[k].nil? and hash[k] !~ /^\-?\d{0,9}(\.\d{1,5})?$/
errors << [k, "must be a number with no more than nine digits and five decimal places"]
end
end
errors
end
|
+ (Object) normalise_date(date)
Take a date like YYYY or YYYY-MM and pad to YYYY-MM-DD
Note: this might not yield a valid date. The only goal is that something
valid on the way in remains valid on the way out.
85
86
87
88
89
90
91
92
93
94
|
# File 'common/validations.rb', line 85
def self.normalise_date(date)
negated = date.start_with?("-")
parts = date.gsub(/^-/, '').split(/-/)
padded = (parts + ['01', '01']).take(3)
(negated ? "-" : "") + padded.join("-")
end
|
+ (Object) parse_sloppy_date(s)
Returns a valid date or throws if the input is invalid.
98
99
100
101
102
103
104
|
# File 'common/validations.rb', line 98
def self.parse_sloppy_date(s)
begin
Date.strptime(normalise_date(s), '%Y-%m-%d')
rescue
raise ArgumentError.new($!)
end
end
|