Class: Hasmenu::Validator

Inherits:
Object
  • Object
show all
Includes:
Printer
Defined in:
lib/hasmenu/validator.rb

Instance Method Summary collapse

Methods included from Printer

#print_build_for, #print_build_start, #print_format_for, #print_format_start, #print_header, #print_invalid_build, #print_invalid_format, #print_invalid_path, #print_invalid_report, #print_invalid_sequence, #print_invalid_version, #print_report, #print_warn_repeats

Constructor Details

#initialize(type, options) ⇒ Validator

Returns a new instance of Validator.



11
12
13
14
# File 'lib/hasmenu/validator.rb', line 11

def initialize(type, options)
  @type = type.chomp("/")
  @schemad = options[:schema] || File.join(Dir.pwd, ".meta", "schema")
end

Instance Method Details

#load_schemaObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hasmenu/validator.rb', line 16

def load_schema
  file = File.join(@schemad, "#{@type}.json")

  if File.file? file
    schema_data = JSON.parse File.read(file)
    @schema, error = JsonSchema.parse(schema_data)
    if error
      puts error
      return false
    end
    return true
  else
    print_invalid_path
    return false
  end
end

#perform(path) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/hasmenu/validator.rb', line 104

def perform(path)
  unless File.exist? path
    print_invalid_path
    return
  end

  return unless load_schema

  if File.file? path
    validate path
  elsif File.directory? path
    validate_all path
  else
    print_invalid_path
  end
end

#validate(path) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hasmenu/validator.rb', line 45

def validate(path)
  print_header path

  data = YAML.load_file(path)

  # check valid schema
  validated, errors = @schema.validate(data)
  puts errors unless validated

  # check unique columns
  type, subtype = @type.split("/")
  case type
    when "chains", "restaurants"
      validate_uniqueness(data.map { |x| x["uid"] })
    when "menu"
      case subtype
        when "chain"
          menus = data["chain"]["menus"] if data["chain"]
        when "restaurant"
          menus = data["restaurant"]["menus"] if data["restaurant"]
      end
  end

  if menus
    validate_uniqueness(menus.map { |m| m["uid"] })
    menus.each do |menu|
      sections = menu["sections"]
      validate_uniqueness(sections.map { |s| s["uid"] })

      items = sections.map { |s| s["items"].reject { |i| i["repeat"] } }.flatten
      validate_uniqueness(items.map { |i| i["uid"] })
      validate_uniqueness(items.map { |i| i["name"] })

      print_warn_repeats \
        if sections.map { |s| s["items"].select { |i| i["repeat"] } }.flatten.present?
    end
  end

  # check file naming conventions
  if menus
    menu = menus.first
    filename = File.basename(path, ".yml")
    # sequence
    if menu && menu.key?("active") && !menu["active"]
      print_invalid_sequence unless filename[0..2] == "xa-"
    else
      print_invalid_sequence unless filename[0..2] =~ /[0-9][0-9]-/
    end
    # version
    print_invalid_version unless filename[3..-1] == "#{menu['uid']}-v#{menu['version']}"
  end
end

#validate_all(path) ⇒ Object



98
99
100
101
102
# File 'lib/hasmenu/validator.rb', line 98

def validate_all(path)
  Dir.glob(File.join(path, "**", "*.yml")) do |file|
    validate file
  end
end

#validate_uniqueness(data) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hasmenu/validator.rb', line 33

def validate_uniqueness(data)
  schema_data = JSON.parse '{"type": "array", "items": {"type": "string"}, "uniqueItems": true}'
  schema = JsonSchema.parse!(schema_data)

  validated, errors = schema.validate(data)
  unless validated
    puts errors
    duplicates = data.group_by { |e| e }.select { |k, v| v.size > 1 }.map(&:first)
    puts JSON.dump duplicates
  end
end