Module: JSONModel::Validations
- Defined in:
- common/validations.rb
Class Method Summary collapse
-
.check_agent_alternate_set(hash) ⇒ Object
-
.check_agent_software_subrecords(hash) ⇒ Object
-
.check_agent_sources(hash) ⇒ Object
-
.check_agent_subject_subrecord(hash) ⇒ Object
-
.check_archival_object(hash) ⇒ Object
-
.check_assessment_monetary_value(hash) ⇒ Object
-
.check_authority_id(hash) ⇒ Object
https://www.pivotaltracker.com/story/show/51373893.
-
.check_collection_management(hash) ⇒ Object
-
.check_container_location(hash) ⇒ Object
-
.check_container_profile(hash) ⇒ Object
-
.check_date(hash) ⇒ Object
-
.check_date_field_query(hash) ⇒ Object
-
.check_digital_object_component(hash) ⇒ Object
-
.check_field_query(hash) ⇒ Object
-
.check_identifier(hash) ⇒ Object
-
.check_instance(hash) ⇒ Object
-
.check_language(hash) ⇒ Object
-
.check_location(hash) ⇒ Object
-
.check_location_profile(hash) ⇒ Object
-
.check_name(hash) ⇒ Object
-
.check_otherlevel(hash) ⇒ Object
-
.check_restriction_date(hash) ⇒ Object
-
.check_rights_statement(hash) ⇒ Object
-
.check_rights_statement_external_document(hash) ⇒ Object
-
.check_source(hash) ⇒ Object
Specification: https://www.pivotaltracker.com/story/show/41430143 See also: https://www.pivotaltracker.com/story/show/51373893.
-
.check_standard_date(date_standardized, errors, field_name = "date_standardized") ⇒ Object
-
.check_structured_date_label(hash) ⇒ Object
-
.check_structured_date_range(hash) ⇒ Object
-
.check_structured_date_single(hash) ⇒ Object
-
.check_sub_container(hash) ⇒ Object
-
.check_survey_dates(hash) ⇒ Object
-
.check_used_language(hash) ⇒ Object
-
.check_user_defined(hash) ⇒ Object
-
.normalise_date(date) ⇒ Object
Take a date like YYYY or YYYY-MM and pad to YYYY-MM-DD.
-
.parse_sloppy_date(s) ⇒ Object
Returns a valid date or throws if the input is invalid.
Class Method Details
.check_agent_alternate_set(hash) ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'common/validations.rb', line 319 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
342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'common/validations.rb', line 342 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
306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'common/validations.rb', line 306 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
332 333 334 335 336 337 338 339 340 |
# File 'common/validations.rb', line 332 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
608 609 610 611 612 613 614 615 616 617 |
# File 'common/validations.rb', line 608 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
808 809 810 811 812 813 814 815 816 817 818 |
# File 'common/validations.rb', line 808 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
116 117 118 119 120 121 122 123 |
# File 'common/validations.rb', line 116 def self.(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
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 |
# File 'common/validations.rb', line 540 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
451 452 453 454 455 456 457 |
# File 'common/validations.rb', line 451 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
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'common/validations.rb', line 515 def self.check_container_profile(hash) errors = [] # Ensure depth, width and height have no more than 2 decimal places ["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 # Ensure stacking limit is a positive integer if it has value 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
176 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 |
# File 'common/validations.rb', line 176 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'] # If padding our end date with months/days would cause it to fall before # the start date (e.g. if the start date was '2000-05' and the end date # just '2000'), use the start date in place of 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
777 778 779 780 781 782 783 784 785 |
# File 'common/validations.rb', line 777 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
632 633 634 635 636 637 638 639 640 641 642 643 644 |
# File 'common/validations.rb', line 632 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
760 761 762 763 764 765 766 767 768 |
# File 'common/validations.rb', line 760 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
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'common/validations.rb', line 467 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
373 374 375 376 377 378 379 380 381 382 383 |
# File 'common/validations.rb', line 373 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
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'common/validations.rb', line 425 def self.check_location(hash) errors = [] # When creating a location, a minimum of one of the following is required: # * Barcode # * Classification # * Coordinate 1 Label AND Coordinate 1 Indicator 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
740 741 742 743 744 745 746 747 748 749 750 751 |
# File 'common/validations.rb', line 740 def self.check_location_profile(hash) errors = [] # Ensure depth, width and height have no more than 2 decimal places ["depth", "width", "height"].each do |k| if !hash[k].nil? && 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 errors end |
.check_name(hash) ⇒ Object
125 126 127 128 129 |
# File 'common/validations.rb', line 125 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
598 599 600 601 602 603 604 605 606 |
# File 'common/validations.rb', line 598 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
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 |
# File 'common/validations.rb', line 702 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
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'common/validations.rb', line 392 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
793 794 795 796 797 798 799 |
# File 'common/validations.rb', line 793 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
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'common/validations.rb', line 99 def self.check_source(hash) errors = [] # non-authorized forms don't need source or rules 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
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 |
# File 'common/validations.rb', line 859 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
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'common/validations.rb', line 215 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
258 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 |
# File 'common/validations.rb', line 258 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
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'common/validations.rb', line 237 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
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'common/validations.rb', line 490 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
826 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 |
# File 'common/validations.rb', line 826 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'] # If padding our end date with months/days would cause it to fall before # the start date (e.g. if the start date was '2000-05' and the end date # just '2000'), use the start date in place of 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
355 356 357 358 359 360 361 362 363 |
# File 'common/validations.rb', line 355 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
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
# File 'common/validations.rb', line 565 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.
154 155 156 157 158 159 160 161 162 163 |
# File 'common/validations.rb', line 154 def self.normalise_date(date) negated = date.start_with?("-") parts = date.gsub(/^-/, '').split(/-/) # Pad out to the right length 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.
167 168 169 170 171 172 173 |
# File 'common/validations.rb', line 167 def self.parse_sloppy_date(s) begin Date.strptime(normalise_date(s), '%Y-%m-%d') rescue raise ArgumentError.new($!) end end |