-
Notifications
You must be signed in to change notification settings - Fork 62
/
validate.rb
executable file
·104 lines (82 loc) · 2.78 KB
/
validate.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
97
98
99
100
101
102
103
104
#!/usr/bin/env ruby
require 'yaml'
require 'kwalify'
require 'active_support/time'
schema = Kwalify::Yaml.load_file('schema.yml')
validator = Kwalify::Validator.new(schema)
# adapted from lib/confcal.rb included in community websites
def tz_lookup time_zone
return @tzc[time_zone] if defined? @tzc
## Build timezone abbriviation dictionary
@tzc ||= {}
ActiveSupport::TimeZone.all.each do | zone|
daylight_abbr = zone.parse('Aug 1').strftime('%Z')
standard_abbr = zone.parse('Dec 1').strftime('%Z')
# It's important to give priority to "standard" time,
# as there are some clashes
# (Sadly, that's just the way it is)
@tzc[standard_abbr] = zone.name unless @tzc[standard_abbr]
@tzc[daylight_abbr] = zone.name unless @tzc[daylight_abbr]
end
tz_lookup time_zone
end
valid = true
Dir.glob('20*/*.yml').sort.each do |file|
begin
yaml_file = YAML.load_file(file)
doc = yaml_file
errors = validator.validate(doc)
if errors && !errors.empty?
valid = false
errors.each do |err|
puts "Error: #{file}: [#{err.path}] #{err.message}"
end
end
if doc['talks']
doc['talks'].each do |talk|
begin
if talk['start']
error_type = 'time'
date_type = 'start'
DateTime.parse(talk['start'])
# also check TZ
tz_abbrev = talk['start'][/[a-zA-Z+0-9:]+$/]
error_type = 'timezone'
raise "wrong timezone" if tz_lookup(tz_abbrev).nil?
end
if talk['end']
error_type = 'time'
date_type = 'end'
DateTime.parse(talk['end'])
# also check TZ
tz_abbrev = talk['end'][/[a-zA-Z+0-9:]+$/]
error_type = 'timezone'
raise "wrong timezone" if tz_lookup(tz_abbrev).nil?
end
rescue StandardError
valid = false
puts "Error: #{file}: Invalid #{date_type} #{error_type}: " \
"'#{talk[date_type]}' in '#{talk['title']}'"
end
end
end
rescue StandardError => err
valid = false
puts err.problem
friendly = case err.problem
when /mapping values/
'Strings containing colons must be surrounded in quotes'
when /did not find expected comment/
'Single-line strings must not start with a pipe |, ' \
'unless quoted'
when /did not find expected key/
"Items are not in a group — perhaps 'talks:' is missing " \
'above the talks?'
else
"#{err.problem} #{err.context}"
end
puts "Error: #{err.file}: line #{err.line}, col #{err.column}: #{friendly}"
end
end
puts 'Congratulations! All events are valid!' if valid
exit valid