Skip to content

Commit 868b2d7

Browse files
authored
Merge pull request #533 from DannyBen/add/arg-order-validation
Add validation to disallow required args before optional
2 parents 80319be + 9a692ca commit 868b2d7

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

lib/bashly/config_validator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ def assert_command(key, value)
231231
assert value['args'].last['repeatable'],
232232
"#{key}.args cannot contain a repeatable arg unless it is the last one"
233233
end
234+
235+
required_order = value['args'].map { |x| x['required'] ? true : false }
236+
refute required_order.include_sequence?(false, true),
237+
"#{key}.args cannot contain required arg after optional arg"
234238
end
235239

236240
if value['expose']

lib/bashly/extensions/array.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ def indent(offset)
1313
def nonuniq
1414
tally.select { |_key, count| count > 1 }.keys
1515
end
16+
17+
def include_sequence?(*elements)
18+
return false if elements.empty?
19+
20+
each_cons(elements.size).any?(elements)
21+
end
1622
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::ConfigurationError: root.args cannot contain required arg after optional arg>

spec/bashly/extensions/array_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,14 @@
5454
expect(subject.nonuniq).to eq %w[it works]
5555
end
5656
end
57+
58+
describe '#include_sequence?' do
59+
it 'returns true when the array includes the sequence' do
60+
expect([1, 2, 3, 4].include_sequence?(2, 3)).to be true
61+
end
62+
63+
it 'returns true when the array does not include the sequence' do
64+
expect([1, 2, 3, 4].include_sequence?(3, 2)).to be false
65+
end
66+
end
5767
end

spec/fixtures/script/validations.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@
6969
repeatable: true
7070
required: true
7171

72+
:command_args_required_after_optional:
73+
name: invalid
74+
args:
75+
- name: user
76+
- name: password
77+
required: true
78+
7279
:command_catch_all_commands:
7380
name: invalid
7481
catch_all:

0 commit comments

Comments
 (0)