From b1e7286c497df1586bddb5746d08e69dbfb61949 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 18 Jul 2019 23:55:13 +0200 Subject: [PATCH] Adds config validation to configuration plugin (#9886) --- plugins/configuration.rb | 62 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/plugins/configuration.rb b/plugins/configuration.rb index 993e4ba4e6d..dd497576922 100644 --- a/plugins/configuration.rb +++ b/plugins/configuration.rb @@ -7,6 +7,11 @@ module Jekyll 'icon' => '/docs/configuration/customizing-devices/#icon', } + TYPES = [ + 'action', 'boolean', 'string', 'integer', 'float', 'time', 'template', + 'device_class', 'icon', 'map', 'list', 'date', 'datetime' + ] + def initialize(tag_name, text, tokens) super @component, @platform = text.strip.split('.', 2) @@ -48,7 +53,7 @@ module Jekyll end end - def render_config_vars(vars:, component:, platform:, classes: nil) + def render_config_vars(vars:, component:, platform:, converter:, classes: nil, parent_type: nil) result = Array.new result << "
" @@ -57,20 +62,56 @@ module Jekyll markup << "
#{key}
" markup << "
" markup << "

" + if attr.key? 'type' + + # Check if the type (or list of types) are valid + if attr['type'].kind_of? Array + attr['type'].each do |type| + raise ArgumentError, "Configuration type '#{type}' for key '#{key}' is not a valid type in the documentation."\ + " See: https://developers.home-assistant.io/docs/en/documentation_create_page.html#configuration" unless \ + TYPES.include? type + end + else + raise ArgumentError, "Configuration type '#{attr['type']}' for key '#{key}' is not a valid type in the documentation."\ + " See: https://developers.home-assistant.io/docs/en/documentation_create_page.html#configuration" unless \ + TYPES.include? attr['type'] + end + markup << "(" markup << "#{type_link(attr['type'], component: component)})" + else + # Type is missing, which is required (unless we are in a list or template) + raise ArgumentError, "Configuration key '#{key}' is missing a type definition" \ + unless ['list', 'template'].include? parent_type end + + if attr.key? 'required' + # Check if required is a valid value + raise ArgumentError, "Configuration key '#{key}' required field must be specified as: "\ + "true, false, inclusive or exclusive."\ + unless [true, false, 'inclusive', 'exclusive'].include? attr['required'] + markup << "(#{required_value(attr['required'])})" end + if attr.key? 'description' - markup << "#{attr['description']}" + markup << "#{converter.convert(attr['description'].to_s)}" + else + # Description is missing + raise ArgumentError, "Configuration key '#{key}' is missing a description." end markup << "

" - if attr.key? 'default' - markup << "

Default value: #{attr['default']}

" + + if attr.key? 'default' and not attr['default'].to_s.empty? + markup << "

\nDefault value: #{converter.convert(attr['default'].to_s)}

" + elsif attr['type'].to_s.include? 'boolean' + # Is the type is a boolean, a default key is required + raise ArgumentError, "Configuration key '#{key}' is a boolean type and"\ + " therefore, requires a default." end + markup << "
" # Check for nested configuration variables @@ -78,7 +119,8 @@ module Jekyll markup << "
" markup << render_config_vars( vars: attr['keys'], component: component, - platform: platform, classes: 'nested') + platform: platform, converter: converter, + classes: 'nested', parent_type: attr['type']) markup << "
" end @@ -100,12 +142,20 @@ module Jekyll component = Liquid::Template.parse(@component).render context platform = Liquid::Template.parse(@platform).render context + site = context.registers[:site] + converter = site.find_converter_instance(::Jekyll::Converters::Markdown) + vars = SafeYAML.load(contents) <<~MARKUP

Configuration Variables

- #{render_config_vars(vars: vars, component: component, platform: platform)} + #{render_config_vars( + vars: vars, + component: component, + platform: platform, + converter: converter + )}
MARKUP end