Class: Hasmenu::Validator
- Inherits:
-
Object
- Object
- Hasmenu::Validator
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_schema ⇒ Object
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
|
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)
path
data = YAML.load_file(path)
validated, errors = @schema.validate(data)
puts errors unless validated
type, subtype = @type.split("/")
case type
when "chains", "restaurants"
validate_uniqueness(data.map { |x| x["uid"] })
when "menu"
case subtype
when "chain"
= data["chain"]["menus"] if data["chain"]
when "restaurant"
= data["restaurant"]["menus"] if data["restaurant"]
end
end
if
validate_uniqueness(.map { |m| m["uid"] })
.each do ||
sections = ["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
if
= .first
filename = File.basename(path, ".yml")
if && .key?("active") && !["active"]
print_invalid_sequence unless filename[0..2] == "xa-"
else
print_invalid_sequence unless filename[0..2] =~ /[0-9][0-9]-/
end
print_invalid_version unless filename[3..-1] == "#{['uid']}-v#{['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
|