Module: Proteus::Validators::ValidationDSL
- Included in:
- BaseValidator
- Defined in:
- lib/proteus/validators/validation_dsl.rb
Instance Method Summary collapse
- #collect(key) ⇒ Object
- #current_data ⇒ Object
- #each ⇒ Object
- #each_key ⇒ Object
- #ensure_data_type(type) ⇒ Object
- #ensure_keys(*keys) ⇒ Object
- #ensure_presence(key, optional: false) ⇒ Object
- #ensure_unique_values ⇒ Object
- #ensure_uniqueness_across(*keys) ⇒ Object
- #ensure_value(key, options = {}) ⇒ Object
- #in_case(key, options = {}) ⇒ Object
- #init ⇒ Object
- #peek ⇒ Object
- #transform_to_paths(data, current_prefix = "", include_prefixes: false) ⇒ Object
- #within(key, optional: false) ⇒ Object
Instance Method Details
#collect(key) ⇒ Object
102 103 104 |
# File 'lib/proteus/validators/validation_dsl.rb', line 102 def collect(key) current_data.collect { |item| item[key.to_s] } end |
#current_data ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/proteus/validators/validation_dsl.rb', line 8 def current_data data = @data.dig(*@key_stack) if data.nil? raise ValidationError.new(message_suffix: "Key hierarchy #{keys_to_hierarchy(*@key_stack)} not found or empty.") end data end |
#each ⇒ Object
168 169 170 171 172 173 174 |
# File 'lib/proteus/validators/validation_dsl.rb', line 168 def each current_data.each_with_index do |item, index| within index do yield end end end |
#each_key ⇒ Object
154 155 156 157 158 159 160 |
# File 'lib/proteus/validators/validation_dsl.rb', line 154 def each_key current_data.each do |key, value| within key do yield end end end |
#ensure_data_type(type) ⇒ Object
42 43 44 45 46 |
# File 'lib/proteus/validators/validation_dsl.rb', line 42 def ensure_data_type(type) unless current_data.is_a? type raise ValidationError.new(message_suffix: "Data in hierarchy #{keys_to_hierarchy(*@key_stack)} has to be of type #{type}.") end end |
#ensure_keys(*keys) ⇒ Object
162 163 164 165 166 |
# File 'lib/proteus/validators/validation_dsl.rb', line 162 def ensure_keys(*keys) keys.each do |key| within key end end |
#ensure_presence(key, optional: false) ⇒ Object
176 177 178 |
# File 'lib/proteus/validators/validation_dsl.rb', line 176 def ensure_presence(key, optional: false) within(key, optional: optional) end |
#ensure_unique_values ⇒ Object
36 37 38 39 40 |
# File 'lib/proteus/validators/validation_dsl.rb', line 36 def ensure_unique_values unless current_data.length == current_data.uniq.length raise ValidationError.new(message_suffix: "Values in hierarchy #{keys_to_hierarchy(*@key_stack)} have to be unique.") end end |
#ensure_uniqueness_across(*keys) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/proteus/validators/validation_dsl.rb', line 106 def ensure_uniqueness_across(*keys) keys.each do |key| @current_paths = [] transform_to_paths(current_data) @current_paths = @current_paths.grep(/^:#{key}:/) unless @current_paths.length == @current_paths.uniq.length raise ValidationError.new(message_suffix: "Values in hierarchy #{keys_to_hierarchy(*@key_stack)} => * => #{key} have to be unique.") end end end |
#ensure_value(key, options = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/proteus/validators/validation_dsl.rb', line 48 def ensure_value(key, = {}) if current_data.key?(key.to_s) if current_data[key.to_s].is_a? Array if .key?(:in) current_data[key.to_s].each_with_index do |item, index| unless [:in].include?(item) raise ValidationError.new(message_suffix: "Data in hierarchy #{keys_to_hierarchy(*@key_stack, key, index)} invalid. Value #{current_data[key.to_s][index]} is not allowed. Should be one of [#{[:in].join(', ')}].") end end end if .key?(:matches) current_data[key.to_s].each_with_index do |item, index| unless [:matches].match?(item) raise ValidationError.new(message_suffix: "Data in hierarchy #{keys_to_hierarchy(*@key_stack, key, index)} invalid. Value of must match #{[:matches].inspect}.") end end end else if .key?(:in_range) unless [:in_range].include?(current_data[key.to_s]) raise ValidationError.new(message_suffix: "Data in hierarchy #{keys_to_hierarchy(*@key_stack)} invalid. Value #{current_data[key.to_s]} is out of range (Valid range: #{[:in_range]}).") end end if .key?(:in) unless [:in].include?(current_data[key.to_s]) raise ValidationError.new(message_suffix: "Data in hierarchy #{keys_to_hierarchy(*@key_stack)} invalid. Value of #{key.to_s} must be one of [#{[:in].join(', ')}]") end end if .key?(:matches) unless [:matches].match?(current_data[key.to_s]) raise ValidationError.new(message_suffix: "Data in hierarchy #{keys_to_hierarchy(*@key_stack)} invalid. Value of #{current_data[key.to_s]} must match #{[:matches].inspect}.") end end if .key?(:length) min = [:length][:min] || 0 max = [:length][:max] || 18446744073709551616 # 2**64 unless current_data[key.to_s].size >= min && current_data[key.to_s].size <= max raise ValidationError.new(message_suffix: "Data in hierarchy #{keys_to_hierarchy(*@key_stack)} invalid. Length of #{current_data[key.to_s]} must be in range [#{min}, #{max}].") end end end else unless .key?(:optional) raise ValidationError.new(message_suffix: "Data in hierarchy #{keys_to_hierarchy(*@key_stack)} invalid. Key #{key} not found.") end end end |
#in_case(key, options = {}) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/proteus/validators/validation_dsl.rb', line 138 def in_case(key, = {}) if current_data.key?(key.to_s) if .key?(:has_value) if [:has_value].is_a? Array if [:has_value].include?(current_data[key.to_s]) yield end else if current_data[key.to_s] == [:has_value] yield end end end end end |
#init ⇒ Object
4 5 6 |
# File 'lib/proteus/validators/validation_dsl.rb', line 4 def init @key_stack ||= [] end |
#peek ⇒ Object
16 17 18 |
# File 'lib/proteus/validators/validation_dsl.rb', line 16 def peek @data.dig(*@key_stack) end |
#transform_to_paths(data, current_prefix = "", include_prefixes: false) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/proteus/validators/validation_dsl.rb', line 118 def transform_to_paths(data, current_prefix = "", include_prefixes: false) if data.is_a?(Hash) data.each do |key, value| transform_to_paths(value, "#{current_prefix}:#{key}") end elsif data.is_a?(Array) if data.any? data.each do |value| transform_to_paths(value, "#{current_prefix}") end end if current_prefix.length > 0 && include_prefixes @current_paths << current_prefix end else @current_paths << "#{current_prefix}:#{data}" end end |
#within(key, optional: false) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/proteus/validators/validation_dsl.rb', line 20 def within(key, optional: false) init key.is_a?(Numeric) ? @key_stack.push(key) : @key_stack.push(key.to_s) if !peek && optional @key_stack.pop return end current_data yield if block_given? @key_stack.pop end |