Skip to content

Commit

Permalink
FIX: Param should be displayed when the default value is invalid (#313)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Lhcfl authored Aug 21, 2024
1 parent b6c4580 commit 760667d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/discourse_data_explorer/parameter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,7 +25,7 @@ def initialize(identifier, type, default, nullable)
@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}",
Expand Down Expand Up @@ -89,7 +89,7 @@ def self.create_from_sql(sql, opts = {})
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
Expand Down
18 changes: 18 additions & 0 deletions spec/lib/parameter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,22 @@ def param(identifier, type, default, nullable)
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

0 comments on commit 760667d

Please sign in to comment.