Class: MarkdownRecord::Validator

Inherits:
Object
  • Object
show all
Includes:
PathUtilities
Defined in:
lib/markdown_record/rendering/validator.rb

Instance Method Summary collapse

Methods included from PathUtilities

#base_content_root_name, #base_rendered_path, #base_rendered_root, #clean_path, #erb_locals_from_path, #fragment_attributes_from_path, #full_path_to_parts, #path_to_fragment_id, #remove_prefix, #rendered_path, #scoped_id_to_parts, #to_scoped_id

Constructor Details

#initializeValidator

Returns a new instance of Validator.



9
10
11
12
# File 'lib/markdown_record/rendering/validator.rb', line 9

def initialize
  @index = ::MarkdownRecord::Indexer.new.index
  @json = ::MarkdownRecord::JsonRenderer.new.render_models_for_subdirectory(subdirectory: "",:concat => true, :deep => true, :save => false, :render_content_fragment_json => true)
end

Instance Method Details

#validateObject



14
15
16
17
18
# File 'lib/markdown_record/rendering/validator.rb', line 14

def validate
  validate_filenames(@index)
  validate_models
  validate_fragments
end

#validate_filenames(val) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/markdown_record/rendering/validator.rb', line 20

def validate_filenames(val)
  if val.is_a?(Hash)
    temp_keys = val.keys.map { |v| remove_prefix(v) }
    
    dups = temp_keys.group_by{|e| clean_path(e)}.keep_if{|_, e| e.length > 1}
    if dups.any?
      raise ::MarkdownRecord::Errors::DuplicateFilenameError.new(dups.keys)
    else
      val.each do |k, v|
        validate_filenames(v)
      end
    end
  end
end

#validate_fragmentsObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/markdown_record/rendering/validator.rb', line 45

def validate_fragments
  frags = @json["markdown_record/content_fragment"]
  frags.each do |frag|
    parent_id = frag.dig("meta", "parent_id")
    if parent_id
      unless frags.any? { |f| f["id"] == parent_id}
        raise ::MarkdownRecord::Errors::MissingParentError.new(parent_id)
      end
    end
  end
end

#validate_modelsObject



35
36
37
38
39
40
41
42
43
# File 'lib/markdown_record/rendering/validator.rb', line 35

def validate_models
  @json.each do |klass, array|
    ids = array.map { |o| [o["id"], o["scope"]] }
    dups = ids.group_by{|e| e}.keep_if{|_, e| e.length > 1}
    if dups.any?
      raise ::MarkdownRecord::Errors::DuplicateIdError.new(klass, dups.keys.first)
    end
  end
end