11module Bashly
22 class ConfigValidator
3+ include ValidationHelpers
4+
35 attr_reader :data
46
57 def initialize ( data )
@@ -10,69 +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_uniq ( key , value , array_key )
63- return unless value
64- list = value . map { |c | c [ array_key ] } . compact . flatten
65- assert list . uniq? , "#{ key } cannot have elements with similar #{ array_key } values"
66- end
67-
68- def assert_string_or_array ( key , value )
69- return unless value
70- assert [ Array , String ] . include? ( value . class ) ,
71- "#{ key } must be a string or an array"
72-
73- assert_array key , value , of : :string if value . is_a? Array
74- end
75-
7617 def assert_version ( key , value )
7718 return unless value
7819 assert [ String , Integer , Float ] . include? ( value . class ) ,
0 commit comments