Class: ArchivesSpaceSchema

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

Instance Method Summary collapse

Constructor Details

#initializeArchivesSpaceSchema

Returns a new instance of ArchivesSpaceSchema.



161
162
163
164
165
166
167
168
169
# File 'common/archivesspace_json_schema.rb', line 161

def initialize
  super
  extend_schema_definition("http://json-schema.org/draft-03/schema#")
  @attributes["type"] = ArchivesSpaceTypeAttribute
  @attributes["subtype"] = ArchivesSpaceSubTypeAttribute
  @attributes["dynamic_enum"] = ArchivesSpaceDynamicEnumAttribute
  @attributes["properties"] = IfMissingAttribute
  @uri = URI.parse("http://www.archivesspace.org/archivesspace.json")
end

Instance Method Details

#already_failed?(fragments) ⇒ Boolean

Returns:

  • (Boolean)


172
173
174
175
176
# File 'common/archivesspace_json_schema.rb', line 172

def already_failed?(fragments)
  JSON::Validator.validation_errors.any? {|error|
    error.fragments == fragments
  }
end

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



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
# File 'common/archivesspace_json_schema.rb', line 179

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

  # Run any custom validations if we've made it this far with no errors
  if !already_failed?(fragments) && current_schema.schema.has_key?("validations")
    current_schema.schema["validations"].each do |level_and_name|
      level, name = level_and_name

      errors = JSONModel::custom_validations[name].call(data)

      errors.each do |error|
        error_string = nil

        if error.is_a? Symbol
          error_string = "Validation error code: #{error}"
        else
          field, msg = error
          prefix = level == :warning ? "Warning generated for" : "Validation failed for"
          error_string = "#{prefix} '#{field}': #{msg}"

        end

        err = JSON::Schema::ValidationError.new(error_string,
                                                fragments,
                                                "custom_validation",
                                                current_schema)

        JSON::Validator.validation_error(err)
      end
    end
  end
end