11module Bashly
22 class ConfigValidator
3+ include ValidationHelpers
4+
35 attr_reader :data
46
57 def initialize ( data )
@@ -10,63 +12,8 @@ def validate
1012 assert_command "root" , data
1113 end
1214
13- def deprecations
14- @deprecations ||= [ ]
15- end
16-
1715 private
1816
19- def assert ( valid , message )
20- raise ConfigurationError , message unless valid
21- end
22-
23- def refute ( invalid , message )
24- assert !invalid , message
25- end
26-
27- def deprecate ( key , **options )
28- deprecations . push Deprecation . new ( key , **options )
29- end
30-
31- def assert_string ( key , value )
32- assert value . is_a? ( String ) , "#{ key } must be a string"
33- end
34-
35- def assert_optional_string ( key , value )
36- assert_string key , value if value
37- end
38-
39- def assert_boolean ( key , value )
40- assert [ true , false , nil ] . include? ( value ) , "#{ key } must be a boolean"
41- end
42-
43- def assert_array ( key , value , of : nil )
44- return unless value
45- assert value . is_a? ( Array ) , "#{ key } must be an array"
46- if of
47- value . each_with_index do |val , i |
48- send "assert_#{ of } " . to_sym , "#{ key } [#{ i } ]" , val
49- end
50- end
51- end
52-
53- def assert_hash ( key , value , whitelist = nil )
54- assert value . is_a? ( Hash ) , "#{ key } must be a hash"
55-
56- if whitelist
57- invalid_keys = value . keys . map ( &:to_sym ) - whitelist
58- assert invalid_keys . empty? , "#{ key } contains invalid options: #{ invalid_keys . join ( ', ' ) } "
59- end
60- end
61-
62- def assert_string_or_array ( key , value )
63- return unless value
64- assert [ Array , String ] . include? ( value . class ) ,
65- "#{ key } must be a string or an array"
66-
67- assert_array key , value , of : :string if value . is_a? Array
68- end
69-
7017 def assert_version ( key , value )
7118 return unless value
7219 assert [ String , Integer , Float ] . include? ( value . class ) ,
@@ -177,6 +124,12 @@ def assert_command(key, value)
177124 assert_array "#{ key } .environment_variables" , value [ 'environment_variables' ] , of : :env_var
178125 assert_array "#{ key } .examples" , value [ 'examples' ] , of : :string
179126
127+ assert_uniq "#{ key } .commands" , value [ 'commands' ] , 'name'
128+ assert_uniq "#{ key } .commands" , value [ 'commands' ] , 'alias'
129+ assert_uniq "#{ key } .flags" , value [ 'flags' ] , 'long'
130+ assert_uniq "#{ key } .flags" , value [ 'flags' ] , 'short'
131+ assert_uniq "#{ key } .args" , value [ 'args' ] , 'name'
132+
180133 if value [ 'catch_all' ] and value [ 'args' ]
181134 repeatable_arg = value [ 'args' ] . select { |a | a [ 'repeatable' ] } . first &.dig 'name'
182135 refute repeatable_arg , "#{ key } .catch_all makes no sense with repeatable arg (#{ repeatable_arg } )"
0 commit comments