Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation to disallow required args before optional #533

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/bashly/config_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ def assert_command(key, value)
assert value['args'].last['repeatable'],
"#{key}.args cannot contain a repeatable arg unless it is the last one"
end

required_order = value['args'].map { |x| x['required'] ? true : false }
refute required_order.include_sequence?(false, true),
"#{key}.args cannot contain required arg after optional arg"
end

if value['expose']
Expand Down
6 changes: 6 additions & 0 deletions lib/bashly/extensions/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ def indent(offset)
def nonuniq
tally.select { |_key, count| count > 1 }.keys
end

def include_sequence?(*elements)
return false if elements.empty?

each_cons(elements.size).any?(elements)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#<Bashly::ConfigurationError: root.args cannot contain required arg after optional arg>
10 changes: 10 additions & 0 deletions spec/bashly/extensions/array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,14 @@
expect(subject.nonuniq).to eq %w[it works]
end
end

describe '#include_sequence?' do
it 'returns true when the array includes the sequence' do
expect([1, 2, 3, 4].include_sequence?(2, 3)).to be true
end

it 'returns true when the array does not include the sequence' do
expect([1, 2, 3, 4].include_sequence?(3, 2)).to be false
end
end
end
7 changes: 7 additions & 0 deletions spec/fixtures/script/validations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@
repeatable: true
required: true

:command_args_required_after_optional:
name: invalid
args:
- name: user
- name: password
required: true

:command_catch_all_commands:
name: invalid
catch_all:
Expand Down