Module: ADIWG::MdjsonSchemas::Utils

Defined in:
lib/adiwg/mdjson_schemas/utils.rb

Overview

Utility methods for accessing and loading schemas

Class Method Summary collapse

Class Method Details

.examples_dirString

Return the path to examples directory.

Returns:

  • (String)

    The path to examples directory



33
34
35
# File 'lib/adiwg/mdjson_schemas/utils.rb', line 33

def self.examples_dir
  File.join(File.dirname(File.expand_path(__FILE__)), '../../../examples/')
end

.load_json(filename) ⇒ Object

load json files

Parameters:

  • filename (String)

    The path and file name to load



12
13
14
# File 'lib/adiwg/mdjson_schemas/utils.rb', line 12

def self.load_json(filename)
  JSON.load File.new(filename)
end

.load_schemas(strict = false) ⇒ nil

Pre-load all of the json-schemas for mdJSON validation

Parameters:

  • strict (Boolean) (defaults to: false)

    If true, will disallow additional properties

Returns:

  • (nil)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/adiwg/mdjson_schemas/utils.rb', line 41

def self.load_schemas(strict = false)
  Dir.glob(schema_dir + '*.json') do |schema|
    loaded = Utils.load_json(schema)
    name = File.basename(schema)

    if strict
      loaded['additionalProperties'] = false
      unless loaded['definitions'].nil?
        loaded['definitions'].each do |_key, val|
          val['additionalProperties'] = false
        end
      end
    end

    jschema = JSON::Schema.new(loaded, Addressable::URI.parse(name))

    JSON::Validator.add_schema(jschema)
  end
end

.load_strict(schema) ⇒ Hash

Load one schema and make it strict, i.e. additionalProperties=false and all properties required

Parameters:

  • schema (String)

    The filename of the schema to load

Returns:

  • (Hash)

    The schema as Ruby hash



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/adiwg/mdjson_schemas/utils.rb', line 66

def self.load_strict(schema)
  loaded = load_json(schema_dir + schema)
  loaded['additionalProperties'] = false
  loaded['required'] = loaded['properties'].keys unless loaded['properties'].nil?

  unless loaded['definitions'].nil?
    loaded['definitions'].each do |_key, val|
      val['additionalProperties'] = false
      val['required'] = val['properties'].keys unless val['properties'].nil?
    end
  end

  loaded
end

.schema_dirString

Return the path to schema directory.

Returns:

  • (String)

    The path to schema directory



26
27
28
# File 'lib/adiwg/mdjson_schemas/utils.rb', line 26

def self.schema_dir
  File.join(File.dirname(File.expand_path(__FILE__)), '../../../schema/')
end

.schema_pathString

Return the path to main schema.json file.

Returns:

  • (String)

    The path to the schema.json file



19
20
21
# File 'lib/adiwg/mdjson_schemas/utils.rb', line 19

def self.schema_path
  File.join(File.dirname(File.expand_path(__FILE__)), '../../../schema/schema.json')
end