Skip to content

Commit

Permalink
Merge pull request #43 from easolhq/support-limiting-options-for-product
Browse files Browse the repository at this point in the history
Support limiting options for product
  • Loading branch information
Dávid Lantos authored Mar 14, 2023
2 parents 3d2a718 + e5ab567 commit 7e6f65f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ Style/BlockDelimiters:
EnforcedStyle: semantic
Exclude:
- "spec/**/*.rb"
IgnoredMethods:
AllowedMethods:
- attribute
- html
- json
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ bin/rspec

### How to test locally in a theme repo

#### Direct call

This approach is generally quicker than installing the gem (see below). Please use the built gem for final testing.

Within a theme repo, call the executable of the gem directly, for example:

```
cd alchemist_theme
../canvas/bin/canvas lint
```

#### Installing the gem

1. Clone the [easolhq/canvas](https://github.com/easolhq/canvas) repo locally.
2. Build the gem inside the canvas directory to create a `.gem` file.

Expand Down
28 changes: 25 additions & 3 deletions lib/canvas/validators/schema_attributes/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ class SchemaAttribute
# Attribute validations specific to product-type variables.
class Product < Base
ALLOWED_DEFAULT_VALUES = %w[random].freeze
ALLOWED_RESTRICTIONS = %w[experiences accommodations extras].freeze

def validate
super &&
ensure_default_values_are_valid
ensure_default_values_are_valid &&
ensure_only_values_are_valid
end

private
Expand All @@ -23,6 +25,10 @@ def permitted_values_for_default_key
end
end

def optional_keys
super.merge("only" => Array)
end

def ensure_default_values_are_valid
return true unless attribute.key?("default")

Expand All @@ -33,11 +39,27 @@ def ensure_default_values_are_valid
end
end

def ensure_only_values_are_valid
return true unless attribute.key?("only")

if attribute["only"].empty?
@errors << %["only" cannot be empty]
return false
end

unsupported_entries = attribute["only"] - ALLOWED_RESTRICTIONS
if unsupported_entries.any?
@errors << %["only" for product-type variables must be one of: #{ALLOWED_RESTRICTIONS.join(', ')}]
return false
end

true
end

def default_value_is_valid?(value)
value = value.downcase
if !ALLOWED_DEFAULT_VALUES.include?(value)
@errors << "\"default\" for product-type variables must be "\
"one of: #{ALLOWED_DEFAULT_VALUES.join(', ')}"
@errors << %["default" for product-type variables must be one of: #{ALLOWED_DEFAULT_VALUES.join(', ')}]
false
else
true
Expand Down
2 changes: 1 addition & 1 deletion lib/canvas/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Canvas
VERSION = "3.2.1"
VERSION = "3.3.0"
end
46 changes: 43 additions & 3 deletions spec/lib/canvas/validators/schema_attributes/product_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# frozen_string_literal: true

describe Canvas::Validator::SchemaAttribute::Product do
subject(:validator) {
Canvas::Validator::SchemaAttribute::Product.new(attribute)
}
subject(:validator) { described_class.new(attribute) }

describe "#validate" do
describe "validating an optional 'default' key" do
Expand All @@ -26,6 +24,48 @@
end
end

context "when `only` is provided" do
it "is valid when using an array" do
all_allowed_values = %w[experiences accommodations extras]
validator = described_class.new({
"name" => "my_product",
"type" => "product",
"only" => all_allowed_values,
})
expect(validator.validate).to eq(true)
end

it "is invalid when empty as it would exclude everything" do
validator = described_class.new({
"name" => "my_product",
"type" => "product",
"only" => [],
})
expect(validator.validate).to eq(false)
expect(validator.errors).to include(%["only" cannot be empty])
end

it "is invalid when using unsupported option" do
validator = described_class.new({
"name" => "my_product",
"type" => "product",
"only" => ["unsupported"],
})
expect(validator.validate).to eq(false)
expect(validator.errors).to include(%["only" for product-type variables must be one of: experiences, accommodations, extras])
end

it "is invalid when not an array" do
validator = described_class.new({
"name" => "my_product",
"type" => "product",
"only" => "not an array",
})
expect(validator.validate).to eq(false)
expect(validator.errors).to include(%["only" is a String, expected Array])
end
end

context "when `random` default value is provided" do
let(:default_value) { "Random" }

Expand Down

0 comments on commit 7e6f65f

Please sign in to comment.