Skip to content

Commit 4b3f741

Browse files
committed
- Add array syntax to validate
1 parent 4cd8940 commit 4b3f741

File tree

18 files changed

+216
-14
lines changed

18 files changed

+216
-14
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/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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ def label
2828
def usage_string
2929
required ? label : "[#{label}]"
3030
end
31+
32+
def validate
33+
return [] unless options['validate']
34+
35+
result = options['validate']
36+
result.is_a?(Array) ? result : [result]
37+
end
38+
39+
def validate? = validate.any?
3140
end
3241
end
3342
end

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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ def usage_string(extended: false)
1616
result << strings[:required] if required && extended
1717
result.join ' '
1818
end
19+
20+
def validate
21+
return [] unless options['validate']
22+
23+
result = options['validate']
24+
result.is_a?(Array) ? result : [result]
25+
end
26+
27+
def validate? = validate.any?
1928
end
2029
end
2130
end

lib/bashly/script/flag.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ def usage_string(extended: false)
4646
result << strings[:repeatable] if repeatable && extended
4747
result.join ' '
4848
end
49+
50+
def validate
51+
return [] unless options['validate']
52+
53+
result = options['validate']
54+
result.is_a?(Array) ? result : [result]
55+
end
56+
57+
def validate? = validate.any?
4958
end
5059
end
5160
end

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

lib/bashly/views/argument/validations.gtx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
if validate
1+
if validate?
22
= view_marker
33

44
if repeatable
55
> if [[ -v args['{{ name }}'] ]]; then
66
> values=''
77
> eval "values=(${args['{{ name }}']})"
88
> for value in "${values[@]}"; do
9-
> validation_output="$(validate_{{ validate }} "$value")"
9+
validate.each do |funcname|
10+
> validation_output="$(validate_{{ funcname }} "$value")"
1011
> if [[ -n "$validation_output" ]]; then
1112
> printf "{{ strings[:validation_error] }}\n" "{{ name.upcase }}" "$validation_output" >&2
1213
> exit 1
1314
> fi
15+
end
1416
> done
1517
> fi
1618
else
1719
> if [[ -v args['{{ name }}'] ]]; then
18-
> validation_output="$(validate_{{ validate }} "${args['{{ name }}']:-}")"
20+
validate.each do |funcname|
21+
> validation_output="$(validate_{{ funcname }} "${args['{{ name }}']:-}")"
1922
> if [[ -n "$validation_output" ]]; then
2023
> printf "{{ strings[:validation_error] }}\n" "{{ name.upcase }}" "$validation_output" >&2
2124
> exit 1
2225
> fi
26+
end
2327
> fi
2428
>
2529
end
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
if validate
1+
if validate?
22
= view_marker
33

44
> if [[ -v {{ name.upcase }} ]]; then
5-
> validation_output="$(validate_{{ validate }} "${{ name.upcase }}")"
5+
validate.each do |funcname|
6+
> validation_output="$(validate_{{ funcname }} "${{ name.upcase }}")"
67
> if [[ -n "${validation_output}" ]]; then
78
> printf "{{ strings[:environment_variable_validation_error] }}\n" "{{ usage_string }}" "$validation_output" >&2
89
> exit 1
910
> fi
11+
end
1012
> fi
1113
>
1214
end

0 commit comments

Comments
 (0)