Skip to content

Commit 174cc94

Browse files
committed
extract basic assertions from ConfigValidator to ValidationHelpers
1 parent 740f89e commit 174cc94

File tree

2 files changed

+71
-61
lines changed

2 files changed

+71
-61
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
module Bashly
2+
# This is a `ConfigValidator` concern responsible for providing basic
3+
# assertion methods.
4+
module ValidationHelpers
5+
6+
def deprecations
7+
@deprecations ||= []
8+
end
9+
10+
protected
11+
12+
def assert(valid, message)
13+
raise ConfigurationError, message unless valid
14+
end
15+
16+
def refute(invalid, message)
17+
assert !invalid, message
18+
end
19+
20+
def deprecate(key, **options)
21+
deprecations.push Deprecation.new(key, **options)
22+
end
23+
24+
def assert_string(key, value)
25+
assert value.is_a?(String), "#{key} must be a string"
26+
end
27+
28+
def assert_optional_string(key, value)
29+
assert_string key, value if value
30+
end
31+
32+
def assert_boolean(key, value)
33+
assert [true, false, nil].include?(value), "#{key} must be a boolean"
34+
end
35+
36+
def assert_array(key, value, of: nil)
37+
return unless value
38+
assert value.is_a?(Array), "#{key} must be an array"
39+
if of
40+
value.each_with_index do |val, i|
41+
send "assert_#{of}".to_sym, "#{key}[#{i}]", val
42+
end
43+
end
44+
end
45+
46+
def assert_hash(key, value, whitelist = nil)
47+
assert value.is_a?(Hash), "#{key} must be a hash"
48+
49+
if whitelist
50+
invalid_keys = value.keys.map(&:to_sym) - whitelist
51+
assert invalid_keys.empty?, "#{key} contains invalid options: #{invalid_keys.join(', ')}"
52+
end
53+
end
54+
55+
def assert_uniq(key, value, array_key)
56+
return unless value
57+
list = value.map { |c| c[array_key] }.compact.flatten
58+
assert list.uniq?, "#{key} cannot have elements with similar #{array_key} values"
59+
end
60+
61+
def assert_string_or_array(key, value)
62+
return unless value
63+
assert [Array, String].include?(value.class),
64+
"#{key} must be a string or an array"
65+
66+
assert_array key, value, of: :string if value.is_a? Array
67+
end
68+
end
69+
end

lib/bashly/config_validator.rb

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module 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

Comments
 (0)