Class: JekyllOpenSdgPlugins::ValidateIndicatorConfig

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/jekyll-open-sdg-plugins/validate_indicator_config.rb

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jekyll-open-sdg-plugins/validate_indicator_config.rb', line 11

def generate(site)

  schema_path = File.join(File.dirname(__FILE__), 'schema-indicator-config.json')
  json_from_file = File.read(schema_path)
  schema = JSON.parse(json_from_file)
  schemer = JSONSchemer.schema(schema)

  # Perform validation if the "validate_indicator_config" flag is true.
  if site.config.has_key?('validate_indicator_config') && site.config['validate_indicator_config']
    # We don't care too much what language we use, just get the first one.
    language = 'en'
    if site.config.has_key?('languages')
      language = site.config['languages'][0]
    end
     = {}
     = site.data[language]['meta']
    # Loop through the indicators (using metadata as a list).
    validation_failed = false
    .each do |inid, meta|
      unless schemer.valid?(meta)
        validation_failed = true
        opensdg_notice('Indicator ' + inid + ' configuration invalid:')
        errors = schemer.validate(meta).to_a
        errors.each { |error| opensdg_validation_error(error) }
      end
    end
    if validation_failed
      opensdg_notice "Some indicator configuration was not valid. See feedback above."
      raise "Invalid indicator configuration"
    end
  end

  # Regardless place the schema in site data so it can be used in Jekyll templates.
  site.data['schema-indicator-config'] = schema

end