module JSONModel::Validations

Public Class Methods

check_archival_object(hash) click to toggle source
# 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) click to toggle source

www.pivotaltracker.com/story/show/51373893

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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']
      # 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_digital_object_component(hash) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/aspace_client/validations.rb, line 177
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_name(hash) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

Specification: www.pivotaltracker.com/story/show/41430143 See also: www.pivotaltracker.com/story/show/51373893

# File lib/aspace_client/validations.rb, line 32
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_user_defined(hash) click to toggle source
# 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) click to toggle source

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.

# File lib/aspace_client/validations.rb, line 84
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) click to toggle source

Returns a valid date or throws if the input is invalid.

# 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