Total Complexity | 6 |
Total Lines | 26 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | # frozen_string_literal: true |
||
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 |
||
42 |