Module: JSONModel::Validations
- Extended by:
- JSONModel
- Defined in:
- common/validations.rb
Constant Summary
Constants included
from JSONModel
REFERENCE_KEY_REGEX
Class Method Summary
collapse
Methods included from JSONModel
JSONModel, JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, check_valid_refs, 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_publish_flags!, set_repository, strict_mode, strict_mode?, validate_schema, with_repository
Class Method Details
.check_agent_alternate_set(hash) ⇒ Object
320
321
322
323
324
325
326
327
328
329
330
331
|
# File 'common/validations.rb', line 320
def self.check_agent_alternate_set(hash)
errors = []
if (hash["set_component"].nil? || hash["set_component"].empty?) &&
(hash["descriptive_note"].nil? || hash["descriptive_note"].empty?) &&
(hash["file_uri"].nil? || hash["file_uri"].empty?)
errors << ["agent_sources", "Must specify one of Set Component, Descriptive Note or File URI"]
end
return errors
end
|
.check_agent_software_subrecords(hash) ⇒ Object
343
344
345
346
347
348
349
350
351
352
353
354
|
# File 'common/validations.rb', line 343
def self.check_agent_software_subrecords(hash)
errors = []
subrecords_disallowed = ["agent_record_identifiers", "agent_record_controls", "agent_other_agency_codes", "agent_conventions_declarations", "agent_maintenance_histories", "agent_sources", "agent_alternate_sets", "agent_resources"]
subrecords_disallowed.each do |sd|
unless hash[sd] == [] || hash[sd].nil?
errors << [sd, "subrecord not allowed for agent software"]
end
end
return errors
end
|
.check_agent_sources(hash) ⇒ Object
307
308
309
310
311
312
313
314
315
316
317
318
|
# File 'common/validations.rb', line 307
def self.check_agent_sources(hash)
errors = []
if (hash["source_entry"].nil? || hash["source_entry"].empty?) &&
(hash["descriptive_note"].nil? || hash["descriptive_note"].empty?) &&
(hash["file_uri"].nil? || hash["file_uri"].empty?)
errors << ["agent_sources", "Must specify one of Source Entry, Descriptive Note or File URI"]
end
return errors
end
|
.check_agent_subject_subrecord(hash) ⇒ Object
333
334
335
336
337
338
339
340
341
|
# File 'common/validations.rb', line 333
def self.check_agent_subject_subrecord(hash)
errors = []
if hash["subjects"].empty?
errors << ["subjects", "Must specify a primary subject"]
end
return errors
end
|
.check_archival_object(hash) ⇒ Object
609
610
611
612
613
614
615
616
617
618
|
# File 'common/validations.rb', line 609
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_assessment_monetary_value(hash) ⇒ Object
809
810
811
812
813
814
815
816
817
818
819
|
# File 'common/validations.rb', line 809
def self.check_assessment_monetary_value(hash)
errors = []
if monetary_value = hash['monetary_value']
unless monetary_value =~ /\A[0-9]+\z/ || monetary_value =~ /\A[0-9]+\.[0-9]{1,2}\z/
errors << ['monetary_value', "must be a number with no more than 2 decimal places"]
end
end
errors
end
|
.check_authority_id(hash) ⇒ Object
https://www.pivotaltracker.com/story/show/51373893
117
118
119
120
121
122
123
124
|
# File 'common/validations.rb', line 117
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
# File 'common/validations.rb', line 541
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
|
.check_container_location(hash) ⇒ Object
452
453
454
455
456
457
458
|
# File 'common/validations.rb', line 452
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
|
.check_container_profile(hash) ⇒ Object
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
|
# File 'common/validations.rb', line 516
def self.check_container_profile(hash)
errors = []
["depth", "width", "height"].each do |k|
if hash[k] !~ /^\s*(?=.*[0-9])\d*(?:\.\d{1,2})?\s*$/
errors << [k, "must be a number with no more than 2 decimal places"]
end
end
if !hash['stacking_limit'].nil? and hash['stacking_limit'] !~ /^\d+$/
errors << ['stacking_limit', 'must be a positive integer']
end
errors
end
|
.check_date(hash) ⇒ Object
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
# File 'common/validations.rb', line 177
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_date_field_query(hash) ⇒ Object
778
779
780
781
782
783
784
785
786
|
# File 'common/validations.rb', line 778
def self.check_date_field_query(hash)
errors = []
if (!hash.has_key?("value") || hash["value"].empty?) && hash["comparator"] != "empty"
errors << ["value", "Must specify either a value or use the 'empty' comparator"]
end
errors
end
|
.check_digital_object_component(hash) ⇒ Object
633
634
635
636
637
638
639
640
641
642
643
644
645
|
# File 'common/validations.rb', line 633
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_field_query(hash) ⇒ Object
761
762
763
764
765
766
767
768
769
|
# File 'common/validations.rb', line 761
def self.check_field_query(hash)
errors = []
if (!hash.has_key?("value") || hash["value"].empty?) && hash["comparator"] != "empty"
errors << ["value", "Must specify either a value or use the 'empty' comparator"]
end
errors
end
|
.check_identifier(hash) ⇒ Object
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
|
.check_instance(hash) ⇒ Object
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
|
# File 'common/validations.rb', line 468
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["digital_object"] && hash["instance_type"] != "digital_object"
errors << ["instance_type", "An instance with a digital object reference must be of type 'digital_object'"]
elsif hash["instance_type"]
errors << ["sub_container", "Can't be empty"] if hash["sub_container"].nil?
end
errors
end
|
.check_language(hash) ⇒ Object
374
375
376
377
378
379
380
381
382
383
384
|
# File 'common/validations.rb', line 374
def self.check_language(hash)
langs = hash['lang_materials'].map {|l| l['language_and_script']}.compact.reject {|e| e == [] }.flatten
errors = []
if langs == []
errors << :must_contain_at_least_one_language
end
errors
end
|
.check_location(hash) ⇒ Object
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
# File 'common/validations.rb', line 426
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_location_profile(hash) ⇒ Object
741
742
743
744
745
746
747
748
749
750
751
752
|
# File 'common/validations.rb', line 741
def self.check_location_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
|
.check_name(hash) ⇒ Object
126
127
128
129
130
|
# File 'common/validations.rb', line 126
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
599
600
601
602
603
604
605
606
607
|
# File 'common/validations.rb', line 599
def self.check_otherlevel(hash)
errors = []
if hash["level"] == "otherlevel"
errors << ["other_level", "missing required property"] if hash["other_level"].nil?
end
errors
end
|
.check_restriction_date(hash) ⇒ Object
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
|
# File 'common/validations.rb', line 703
def self.check_restriction_date(hash)
errors = []
if (rr = hash['rights_restriction'])
begin
begin_date = Date.strptime(rr['begin'], '%Y-%m-%d') if rr['begin']
rescue ArgumentError => e
errors << ["rights_restriction__begin", "must be in YYYY-MM-DD format"]
end
begin
end_date = Date.strptime(rr['end'], '%Y-%m-%d') if rr['end']
rescue ArgumentError => e
errors << ["rights_restriction__end", "must be in YYYY-MM-DD format"]
end
if begin_date && end_date && end_date < begin_date
errors << ["rights_restriction__end", "must not be before begin"]
end
end
errors
end
|
.check_rights_statement(hash) ⇒ Object
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
# File 'common/validations.rb', line 393
def self.check_rights_statement(hash)
errors = []
if hash["rights_type"] == "copyright"
errors << ["status", "missing required property"] if hash["status"].nil?
errors << ["jurisdiction", "missing required property"] if hash["jurisdiction"].nil?
errors << ["start_date", "missing required property"] if hash["start_date"].nil?
elsif hash["rights_type"] == "license"
errors << ["license_terms", "missing required property"] if hash["license_terms"].nil?
errors << ["start_date", "missing required property"] if hash["start_date"].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?
errors << ["start_date", "missing required property"] if hash["start_date"].nil?
elsif hash["rights_type"] == "other"
errors << ["other_rights_basis", "missing required property"] if hash["other_rights_basis"].nil?
errors << ["start_date", "missing required property"] if hash["start_date"].nil?
end
errors
end
|
.check_rights_statement_external_document(hash) ⇒ Object
794
795
796
797
798
799
800
|
# File 'common/validations.rb', line 794
def self.check_rights_statement_external_document(hash)
errors = []
errors << ['identifier_type', 'missing required property'] if hash['identifier_type'].nil?
errors
end
|
.check_source(hash) ⇒ Object
Specification:
https://www.pivotaltracker.com/story/show/41430143
See also: https://www.pivotaltracker.com/story/show/51373893
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'common/validations.rb', line 100
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_standard_date(date_standardized, errors, field_name = "date_standardized") ⇒ Object
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
|
# File 'common/validations.rb', line 860
def self.check_standard_date(date_standardized, errors, field_name = "date_standardized")
matches_y = (date_standardized =~ /^[\d]{1}$/) == 0
matches_y_mm = (date_standardized =~ /^[\d]{1}-[\d]{2}$/) == 0
matches_yy = (date_standardized =~ /^[\d]{2}$/) == 0
matches_yy_mm = (date_standardized =~ /^[\d]{2}-[\d]{2}$/) == 0
matches_yyy = (date_standardized =~ /^[\d]{3}$/) == 0
matches_yyy_mm = (date_standardized =~ /^[\d]{3}-[\d]{2}$/) == 0
matches_yyyy = (date_standardized =~ /^[\d]{4}$/) == 0
matches_yyyy_mm = (date_standardized =~ /^[\d]{4}-[\d]{2}$/) == 0
matches_yyyy_mm_dd = (date_standardized =~ /^[\d]{4}-[\d]{2}-[\d]{2}$/) == 0
matches_yyy_mm_dd = (date_standardized =~ /^[\d]{3}-[\d]{2}-[\d]{2}$/) == 0
matches_mm_yyyy = (date_standardized =~ /^[\d]{2}-[\d]{4}$/) == 0
matches_mm_dd_yyyy = (date_standardized =~ /^[\d]{4}-[\d]{2}-[\d]{2}$/) == 0
errors << [field_name, "must be in YYYY[YYY][YY][Y], YYYY[YYY][YY][Y]-MM, or YYYY-MM-DD format"] unless matches_yyyy || matches_yyyy_mm || matches_yyyy_mm_dd || matches_yyy || matches_yy || matches_y || matches_yyy_mm || matches_yy_mm || matches_y_mm || matches_mm_yyyy || matches_mm_dd_yyyy || matches_yyy_mm_dd
return errors
end
|
.check_structured_date_label(hash) ⇒ Object
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
# File 'common/validations.rb', line 216
def self.check_structured_date_label(hash)
errors = []
if !hash["structured_date_range"] && !hash["structured_date_single"]
errors << ["structured_date_label", "must_specify_either_a_single_or_ranged_date"]
end
if hash["structured_date_range"] && hash["structured_date_single"]
errors << ["structured_date_single", "cannot specify both a single and ranged date"]
end
if hash["structured_date_range"] && hash["date_type_structured"] == "single"
errors << ["structured_date_range", "Must specify single date for date type of single"]
end
if hash["structured_date_single"] && hash["date_type_structured"] == "range"
errors << ["structured_date_range", "Must specify range date for date type of range"]
end
return errors
end
|
.check_structured_date_range(hash) ⇒ Object
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
# File 'common/validations.rb', line 259
def self.check_structured_date_range(hash)
errors = []
has_begin_expr_date = !hash["begin_date_expression"].nil? &&
!hash["begin_date_expression"].empty?
has_end_expr_date = !hash["end_date_expression"].nil? &&
!hash["end_date_expression"].empty?
has_begin_std_date = !hash["begin_date_standardized"].nil? &&
!hash["begin_date_standardized"].empty?
has_end_std_date = !hash["end_date_standardized"].nil? &&
!hash["end_date_standardized"].empty?
errors << ["begin_date_expression", "is required"] if !has_begin_expr_date && (!has_begin_std_date && !has_end_std_date)
errors << ["end_date_expression", "requires begin date expression to be defined"] if !has_begin_expr_date && has_end_expr_date
errors << ["end_date_standardized", "requires begin_date_standardized to be defined"] if (!has_begin_std_date && has_end_std_date)
if has_begin_std_date
errors = check_standard_date(hash["begin_date_standardized"], errors, "begin_date_standardized")
end
if has_end_std_date
errors = check_standard_date(hash["end_date_standardized"], errors, "end_date_standardized")
end
if errors.length == 0 && hash["begin_date_standardized"] && hash["end_date_standardized"]
begin
if hash["begin_date_standardized"]
bt = parse_sloppy_date(hash["begin_date_standardized"])
end
if hash["end_date_standardized"]
et = parse_sloppy_date(hash["end_date_standardized"])
end
rescue => e
errors << ["begin_date_standardized", "Error attempting to parsing dates"]
end
errors << ["begin_date_standardized", "requires that end dates are after begin dates"] if bt && et && bt > et
end
return errors
end
|
.check_structured_date_single(hash) ⇒ Object
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
# File 'common/validations.rb', line 238
def self.check_structured_date_single(hash)
errors = []
if hash["date_role"].nil?
errors << ["date_role", "is required"]
end
has_expr_date = !hash["date_expression"].nil? &&
!hash["date_expression"].empty?
has_std_date = !hash["date_standardized"].nil?
errors << ["date_standardized", "or date expression is required"] unless has_expr_date || has_std_date
if has_std_date
errors = check_standard_date(hash["date_standardized"], errors)
end
return errors
end
|
.check_sub_container(hash) ⇒ Object
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
# File 'common/validations.rb', line 491
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
|
.check_survey_dates(hash) ⇒ Object
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
|
# File 'common/validations.rb', line 827
def self.check_survey_dates(hash)
errors = []
begin
begin_date = parse_sloppy_date(hash['survey_begin'])
rescue ArgumentError => e
errors << ["survey_begin", "not a valid date"]
end
begin
if hash['survey_end']
end_s = if begin_date && hash['survey_begin'] && hash['survey_begin'].start_with?(hash['survey_end'])
hash['survey_begin']
else
hash['survey_end']
end
end_date = parse_sloppy_date(end_s)
end
rescue ArgumentError
errors << ["survey_end", "not a valid date"]
end
if begin_date && end_date && end_date < begin_date
errors << ["survey_end", "must not be before begin"]
end
errors
end
|
.check_used_language(hash) ⇒ Object
356
357
358
359
360
361
362
363
364
|
# File 'common/validations.rb', line 356
def self.check_used_language(hash)
errors = []
if hash["language"].nil? && hash["notes"].empty?
errors << ["language", "Must specify either language or a note."]
end
return errors
end
|
.check_user_defined(hash) ⇒ Object
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
# File 'common/validations.rb', line 566
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.
155
156
157
158
159
160
161
162
163
164
|
# File 'common/validations.rb', line 155
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.
168
169
170
171
172
173
174
|
# File 'common/validations.rb', line 168
def self.parse_sloppy_date(s)
begin
Date.strptime(normalise_date(s), '%Y-%m-%d')
rescue
raise ArgumentError.new($!)
end
end
|