module ADIWG::MdjsonSchemas::Utils
Utility methods for accessing and loading schemas
Public Class Methods
examples_dir()
click to toggle source
Return the path to examples directory.
@return [String] The path to examples directory
# 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)
click to toggle source
load json files
@param [String] filename The path and file name to load
# File lib/adiwg/mdjson_schemas/utils.rb, line 12 def self.load_json(filename) JSON.load File.new(filename) end
load_schemas(strict = false)
click to toggle source
Pre-load all of the json-schemas for mdJSON validation
@param [Boolean] strict If true, will disallow additional properties @return [nil]
# 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)
click to toggle source
Load one schema and make it strict, i.e. additionalProperties=false and all properties required
@param [String] schema The filename of the schema to load @return [Hash] The schema as Ruby hash
# 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_dir()
click to toggle source
Return the path to schema directory.
@return [String] The path to schema directory
# File lib/adiwg/mdjson_schemas/utils.rb, line 26 def self.schema_dir File.join(File.dirname(File.expand_path(__FILE__)), '../../../schema/') end
schema_path()
click to toggle source
Return the path to main schema.json file.
@return [String] The path to the schema.json file
# 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