Module: JSONModel::Validations
- Extended by:
- JSONModel
- Defined in:
- lib/aspace_client/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, parse_jsonmodel_ref, parse_reference, repository, repository_for, schema_src, set_repository, strict_mode, strict_mode?, with_repository
Class Method Details
.check_archival_object(hash) ⇒ Object
340
341
342
343
344
345
346
347
348
349
|
# File 'lib/aspace_client/validations.rb', line 340
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
|
.check_authority_id(hash) ⇒ Object
49
50
51
52
53
54
55
56
|
# File 'lib/aspace_client/validations.rb', line 49
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
|
.check_collection_management(hash) ⇒ Object
279
280
281
282
283
284
285
286
287
|
# File 'lib/aspace_client/validations.rb', line 279
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
errors
end
|
.check_container(hash) ⇒ Object
219
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
|
# File 'lib/aspace_client/validations.rb', line 219
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
|
.check_container_location(hash) ⇒ Object
203
204
205
206
207
208
209
|
# File 'lib/aspace_client/validations.rb', line 203
def self.check_container_location(hash)
errors = []
errors << ["end_date", "is required"] if hash["end_date"].nil? and hash["status"] == "previous"
errors
end
|
.check_date(hash) ⇒ Object
106
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
|
# File 'lib/aspace_client/validations.rb', line 106
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
|
.check_digital_object_component(hash) ⇒ Object
364
365
366
367
368
369
370
371
372
373
374
375
376
|
# File 'lib/aspace_client/validations.rb', line 364
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
|
.check_identifier(hash) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/aspace_client/validations.rb', line 8
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
|
.check_instance(hash) ⇒ Object
258
259
260
261
262
263
264
265
266
267
268
269
|
# File 'lib/aspace_client/validations.rb', line 258
def self.check_instance(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
|
.check_location(hash) ⇒ Object
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/aspace_client/validations.rb', line 177
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
|
.check_name(hash) ⇒ Object
58
59
60
61
62
|
# File 'lib/aspace_client/validations.rb', line 58
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
|
.check_otherlevel(hash) ⇒ Object
330
331
332
333
334
335
336
337
338
|
# File 'lib/aspace_client/validations.rb', line 330
def self.check_otherlevel(hash)
warnings = []
if hash["level"] == "otherlevel"
warnings << ["other_level", "is required"] if hash["other_level"].nil?
end
warnings
end
|
.check_rights_statement(hash) ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/aspace_client/validations.rb', line 153
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
|
.check_source(hash) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/aspace_client/validations.rb', line 32
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
|
.check_user_defined(hash) ⇒ Object
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
# File 'lib/aspace_client/validations.rb', line 297
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
|
.normalise_date(date) ⇒ Object
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.
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/aspace_client/validations.rb', line 84
def self.normalise_date(date)
negated = date.start_with?("-")
parts = date.gsub(/^-/, '').split(/-/)
padded = (parts + ['01', '01']).take(3)
(negated ? "-" : "") + padded.join("-")
end
|
.parse_sloppy_date(s) ⇒ Object
Returns a valid date or throws if the input is invalid.
97
98
99
100
101
102
103
|
# File 'lib/aspace_client/validations.rb', line 97
def self.parse_sloppy_date(s)
begin
Date.strptime(normalise_date(s), '%Y-%m-%d')
rescue
raise ArgumentError.new($!)
end
end
|