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

EntityValidator   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 11 1
1
# frozen_string_literal: true
2
3
require_relative '../entity_validator'
4
5
module AMA
6
  module Entity
7
    class Mapper
8
      module API
9
        module Default
10
          # Default entity validation
11
          class EntityValidator < API::EntityValidator
12
            INSTANCE = new
13
14
            # @param [Object] entity
15
            # @param [Mapper::Type::Concrete] type
16
            # @param [Mapper::Context] context
17
            # @return [Array<Array<Attribute, String, Segment>] List of
18
            #   violations, combined with attribute and segment
19
            def validate(entity, type, context)
20
              enumerator = type.enumerator.enumerate(entity, type, context)
21
              enumerator.flat_map do |attribute, value, segment = nil|
22
                next_context = segment.nil? ? context : context.advance(segment)
23
                validator = attribute.validator
24
                violations = validator.validate(value, attribute, next_context)
25
                violations.map do |violation|
26
                  [attribute, violation, segment]
27
                end
28
              end
29
            end
30
          end
31
        end
32
      end
33
    end
34
  end
35
end
36