Skip to content

Commit b224228

Browse files
authored
Merge pull request #663 from bashly-framework/add/validations-as-array
Add array syntax support to `validate`
2 parents 4cd8940 + 1c5d3df commit b224228

File tree

26 files changed

+271
-28
lines changed

26 files changed

+271
-28
lines changed

examples/validations/src/bashly.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ commands:
1515
# Bashly will look for a function named `validate_integer` in your
1616
# script, you can use any name as long as it has a matching function.
1717
validate: integer
18+
1819
- name: second
1920
help: Second number
20-
validate: integer
21+
22+
# Multiple validations can be provided as an array.
23+
validate: [not_empty, integer]
2124

2225
flags:
2326
- long: --save

examples/validations/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ bashly generate
1212

1313
./validate calc 1 2 --save README.md
1414
./validate calc A
15+
./validate calc 1 ''
1516
./validate calc 1 B
1617
./validate calc 1 2 --save no-such-file.txt
1718

lib/bashly.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module Script
2828
module Introspection
2929
autoloads 'bashly/script/introspection', %i[
3030
Arguments Commands Dependencies EnvironmentVariables Examples Flags
31-
Variables Visibility
31+
Validate Variables Visibility
3232
]
3333
end
3434
end

lib/bashly/config_validator.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def assert_arg(key, value)
9292
assert_string "#{key}.name", value['name']
9393
assert_optional_string "#{key}.help", value['help']
9494
assert_string_or_array "#{key}.default", value['default']
95-
assert_optional_string "#{key}.validate", value['validate']
95+
assert_string_or_array "#{key}.validate", value['validate']
9696
assert_boolean "#{key}.required", value['required']
9797
assert_boolean "#{key}.repeatable", value['repeatable']
9898
assert_boolean "#{key}.unique", value['unique']
@@ -123,7 +123,7 @@ def assert_flag(key, value)
123123
assert_optional_string "#{key}.help", value['help']
124124
assert_optional_string "#{key}.arg", value['arg']
125125
assert_string_or_array "#{key}.default", value['default']
126-
assert_optional_string "#{key}.validate", value['validate']
126+
assert_string_or_array "#{key}.validate", value['validate']
127127

128128
assert_boolean "#{key}.private", value['private']
129129
assert_boolean "#{key}.repeatable", value['repeatable']
@@ -169,7 +169,7 @@ def assert_env_var(key, value)
169169
assert_boolean "#{key}.required", value['required']
170170
assert_boolean "#{key}.private", value['private']
171171
assert_array "#{key}.allowed", value['allowed'], of: :string
172-
assert_optional_string "#{key}.validate", value['validate']
172+
assert_string_or_array "#{key}.validate", value['validate']
173173

174174
refute value['required'] && value['default'], "#{key} cannot have both nub`required` and nub`default`"
175175
end

lib/bashly/script/argument.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
module Bashly
44
module Script
55
class Argument < Base
6+
include Introspection::Validate
7+
68
class << self
79
def option_keys
810
@option_keys ||= %i[

lib/bashly/script/command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def user_lib
158158

159159
# Returns a mixed array of Argument and Flag objects that have validations
160160
def validatables
161-
@validatables ||= args.select(&:validate) + flags.select(&:validate)
161+
@validatables ||= args.select(&:validate?) + flags.select(&:validate?)
162162
end
163163

164164
private

lib/bashly/script/environment_variable.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Bashly
22
module Script
33
class EnvironmentVariable < Base
44
include Introspection::Visibility
5+
include Introspection::Validate
56

67
class << self
78
def option_keys

lib/bashly/script/flag.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Script
55
class Flag < Base
66
include Completions::Flag
77
include Introspection::Visibility
8+
include Introspection::Validate
89

910
class << self
1011
def option_keys

lib/bashly/script/introspection/environment_variables.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def required_environment_variables
2828

2929
# Returns an array of all the environment_variables with a validation
3030
def validated_environment_variables
31-
environment_variables.select(&:validate)
31+
environment_variables.select(&:validate?)
3232
end
3333

3434
# Returns only public environment variables, or both public and private
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Bashly
2+
module Script
3+
module Introspection
4+
module Validate
5+
# Returns an array of validations
6+
def validate
7+
return [] unless options['validate']
8+
9+
result = options['validate']
10+
result.is_a?(Array) ? result : [result]
11+
end
12+
13+
# Returns true if there are any validations defined
14+
def validate? = validate.any?
15+
end
16+
end
17+
end
18+
end

0 commit comments

Comments
 (0)