FIX: Param should be displayed when the default value is invalid (#313)

What's the problem?
===================

TL;DR: When the user enters an incorrect default value, its
corresponding param-input will disappear

When creating a parameter from SQL, we perform cast_to_ruby, which means
that if the default value given by the user is invalid, the entire
parameter will not be added to the param_list of the query.
This behavior is very confusing, and users will not understand why an
incorrect initial value will cause the param-input to disappear.

What's the fix?
================

The cast_to_ruby process is canceled in create_from_sql, so that
param-input with incorrect default value will still be displayed.
We have a simple validation process on the front end, which is enough to
prompt whether some default inputs are incorrect.
This commit is contained in:
锦心 2024-08-21 12:03:11 +08:00 committed by GitHub
parent b6c45800b3
commit 760667d744
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 3 deletions

View File

@ -4,7 +4,7 @@ module ::DiscourseDataExplorer
class Parameter
attr_accessor :identifier, :type, :default, :nullable
def initialize(identifier, type, default, nullable)
def initialize(identifier, type, default, nullable, validate: true)
unless identifier
raise ValidationError.new("Parameter declaration error - identifier is missing")
end
@ -25,7 +25,7 @@ module ::DiscourseDataExplorer
@default = default
@nullable = nullable
begin
cast_to_ruby default if default.present?
cast_to_ruby default if default.present? && validate
rescue ValidationError
raise ValidationError.new(
"Parameter declaration error - the default value is not a valid #{type}",
@ -89,7 +89,7 @@ module ::DiscourseDataExplorer
type = type.strip
begin
ret_params << Parameter.new(ident, type, default, nullable)
ret_params << Parameter.new(ident, type, default, nullable, validate: opts[:strict])
rescue StandardError
raise if opts[:strict]
end

View File

@ -77,4 +77,22 @@ RSpec.describe DiscourseDataExplorer::Parameter do
end
end
end
describe ".create_from_sql" do
it "should not validate default value" do
TEST_SQL = <<~SQL
-- [params]
-- user_id :user_id = user_not_exists
-- post_id :post_id = /t/should-not-exist/33554432/1
-- topic_id :topic_id = /t/should-not-exist/2147483646
-- category_id :category_id = category_not_exists
-- group_id :group_id = group_not_exists
-- group_list :group_list = group_not_exists1,group_not_exists1
-- user_list :mul_users = user_not_exists1,user_not_exists2
SELECT 1
SQL
expect(described_class.create_from_sql(TEST_SQL).length).to eq(7)
end
end
end