Class: Hyrax::FlexibleSchemaValidators::SchemaValidator

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/flexible_schema_validators/schema_validator.rb

Overview

Handles JSON schema validation for flexible metadata profiles

Instance Method Summary collapse

Constructor Details

#initialize(schemer, profile, errors) ⇒ SchemaValidator

Returns a new instance of SchemaValidator.



7
8
9
10
11
# File 'app/services/hyrax/flexible_schema_validators/schema_validator.rb', line 7

def initialize(schemer, profile, errors)
  @schemer = schemer
  @profile = profile
  @errors = errors
end

Instance Method Details

#validate!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/hyrax/flexible_schema_validators/schema_validator.rb', line 13

def validate!
  @schemer.validate(@profile).to_a&.each do |error|
    pointer = error['data_pointer']
    type = error['type']

    if pointer.end_with?('/available_on') && error['data'].nil? && type == 'object'
      @errors << "Schema error at `#{pointer}`: `available_on` cannot be empty and must have a `class` or `context` sub-property."
    elsif type == 'required'
      missing_keys = error.dig('details', 'missing_keys')&.join("', '")
      @errors << "Schema error at `#{pointer}`: Missing required properties: '#{missing_keys}'."
    else
      @errors << "Schema error at `#{pointer}`: Invalid value `#{error['data'].inspect}` for type `#{type}`."
    end
  end
end