Class: JekyllOpenSdgPlugins::ValidateSiteConfig

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/jekyll-open-sdg-plugins/validate_site_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
# File 'lib/jekyll-open-sdg-plugins/validate_site_config.rb', line 11

def generate(site)

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

  # Perform validation if the "validate_site_config" flag is true.
  if site.config.has_key?('validate_site_config') && site.config['validate_site_config']
    unless schemer.valid?(site.config)
      opensdg_notice('Site configuration invalid:')
      errors = schemer.validate(site.config).to_a
      errors.each { |error| opensdg_validation_error(error) }
      opensdg_notice "The site configuration is not valid. See feedback above."
      raise "Invalid site configuration"
    end
  end

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

end