Skip to content

Commit 5ec1e53

Browse files
committed
Restrict values to known settings
To avoid accidental typos and misuse in the user input
1 parent 7c36d16 commit 5ec1e53

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

lib/canvas/validators/schema_attributes/product.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class SchemaAttribute
77
# Attribute validations specific to product-type variables.
88
class Product < Base
99
ALLOWED_DEFAULT_VALUES = %w[random].freeze
10+
ALLOWED_RESTRICTIONS = %w[experiences accommodations extras].freeze
1011

1112
def validate
1213
super &&
@@ -43,10 +44,16 @@ def ensure_only_values_are_valid
4344

4445
if attribute["only"].empty?
4546
@errors << %["only" cannot be empty]
46-
false
47-
else
48-
true
47+
return false
4948
end
49+
50+
usupported_entries = attribute["only"] - ALLOWED_RESTRICTIONS
51+
if usupported_entries.any?
52+
@errors << %["only" contains unsupported entries: #{usupported_entries}]
53+
return false
54+
end
55+
56+
true
5057
end
5158

5259
def default_value_is_valid?(value)

spec/lib/canvas/validators/schema_attributes/product_spec.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626

2727
context "when `only` is provided" do
2828
it "is valid when using an array" do
29+
all_allowed_values = %w[experiences accommodations extras]
2930
validator = described_class.new({
3031
"name" => "my_product",
3132
"type" => "product",
32-
"only" => ["Experience", "Product"],
33+
"only" => all_allowed_values,
3334
})
3435
expect(validator.validate).to eq(true)
3536
end
@@ -44,6 +45,16 @@
4445
expect(validator.errors).to include(%["only" cannot be empty])
4546
end
4647

48+
it "is invalid when using unsupported option" do
49+
validator = described_class.new({
50+
"name" => "my_product",
51+
"type" => "product",
52+
"only" => ["unsupported"],
53+
})
54+
expect(validator.validate).to eq(false)
55+
expect(validator.errors).to include(%["only" contains unsupported entries: ["unsupported"]])
56+
end
57+
4758
it "is invalid when not an array" do
4859
validator = described_class.new({
4960
"name" => "my_product",

0 commit comments

Comments
 (0)