Class: ArchivesSpaceSchema

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

Instance Method Summary (collapse)

Constructor Details

- (ArchivesSpaceSchema) initialize

Returns a new instance of ArchivesSpaceSchema



158
159
160
161
162
163
164
165
166
# File 'common/archivesspace_json_schema.rb', line 158

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

- (Boolean) already_failed?(fragments)

Returns:

  • (Boolean)


169
170
171
172
173
# File 'common/archivesspace_json_schema.rb', line 169

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

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



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

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