Completed
Push — dev ( 6cb2fc...405831 )
by Fike
58s
created

AttributeValidator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
dl 0
loc 26
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B validate() 0 15 5
A initialize() 0 3 1
1
# frozen_string_literal: true
2
3
require_relative '../attribute_validator'
4
5
module AMA
6
  module Entity
7
    class Mapper
8
      module API
9
        module Wrapper
10
          # Attribute validator safety wrapper
11
          class AttributeValidator < API::AttributeValidator
12
            # @param [AMA::Entity::Mapper::API::AttributeValidator] validator
13
            def initialize(validator)
14
              @validator = validator
15
            end
16
17
            # @param [Object] value Attribute value
18
            # @param [AMA::Entity::Mapper::Type::Attribute] attr
19
            # @param [AMA::Entity::Mapper::Context] ctx
20
            # @return [Array<String>] List of violations
21
            def validate(value, attr, ctx)
22
              violations = @validator.validate(value, attr, ctx) do |v, a, c|
23
                API::Default::AttributeValidator::INSTANCE.validate(v, a, c)
24
              end
25
              violations = [violations] if violations.is_a?(String)
26
              violations.nil? ? [] : violations
27
            rescue StandardError => e
28
              raise_if_internal(e)
29
              message = "Error during #{attr} validation (value: #{value})"
30
              if e.is_a?(ArgumentError)
31
                message += '. Does provided validator have ' \
32
                  '(value, attribute, context) signature?'
33
              end
34
              compliance_error(message, context: ctx, parent: e)
35
            end
36
          end
37
        end
38
      end
39
    end
40
  end
41
end
42