Class: IfMissingAttribute

Inherits:
JSON::Schema::PropertiesAttribute
  • Object
show all
Defined in:
common/archivesspace_json_schema.rb

Overview

Add a new ‘ifmissing’ attribute which emits either an error or warning depending on its value.

Class Method Summary collapse

Class Method Details

.validate(current_schema, data, fragments, validator, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'common/archivesspace_json_schema.rb', line 7

def self.validate(current_schema, data, fragments, validator, options = {})
  super

  if data.is_a?(Hash)
    current_schema.schema['properties'].each do |property, property_schema|
      if (property_schema['ifmissing'] && !data.has_key?(property))
        message = nil

        if property_schema['ifmissing'] == 'error'
          message = "ERROR: The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
        elsif property_schema['ifmissing'] == 'warn'
          message = "WARNING: The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
        end

        if message
          validation_error(message, fragments, current_schema, self, options[:record_errors])
        end
      end
    end
  end
end