-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathvalidation.rb
More file actions
53 lines (42 loc) · 1.61 KB
/
validation.rb
File metadata and controls
53 lines (42 loc) · 1.61 KB
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
# frozen_string_literal: true
require 'yaml'
module Jekyll
class Validation < Liquid::Block # rubocop:disable Style/Documentation
def initialize(tag_name, markup, tokens)
super
@name = markup.strip
end
def render(context) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
@context = context
@site = context.registers[:site]
@page = context.environments.first['page']
contents = super
products = @page.fetch('products', [])
unless %w[gateway kic ai-gateway operator event-gateway metering-and-billing].any? { |p| products.include?(p) }
raise ArgumentError,
"Unsupported product for {% validation #{@name} %}"
end
config = YAML.load(contents)
drop = Drops::Validations::Base.make_for(yaml: config, id: @name)
output = context.stack do
context['config'] = drop
Liquid::Template.parse(File.read(drop.template_file)).render(context)
end
if config['indent']
# If the config has an indent key, we need to indent the output
# by that many spaces.
indent = ' ' * config['indent'].to_i
output = output.lines.map { |line| "#{indent}#{line}" }.join
end
output
rescue Psych::SyntaxError => e
message = <<~STRING
On `#{@page['path']}`, the following {% validation %} block contains a malformed yaml:
#{contents.strip.split("\n").each_with_index.map { |l, i| "#{i}: #{l}" }.join("\n")}
#{e.message}
STRING
raise ArgumentError, message
end
end
end
Liquid::Template.register_tag('validation', Jekyll::Validation)