Class: TypedParams::Validator

Inherits:
Mapper
  • Object
show all
Defined in:
lib/typed_params/validator.rb

Instance Method Summary collapse

Methods inherited from Mapper

call, #initialize

Constructor Details

This class inherits a constructor from TypedParams::Mapper

Instance Method Details

#call(params) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
# File 'lib/typed_params/validator.rb', line 7

def call(params)
  raise InvalidParameterError.new('is missing', path: schema.path, source: schema.source) if
    params.nil? && schema.required?

  depth_first_map(params) do |param|
    schema = param.schema
    type   = Types.for(param.value,
      try: schema.type.subtype? ? schema.type.to_sym : nil,
    )

    raise InvalidParameterError.new("type mismatch (received unknown expected #{schema.type.humanize})", path: param.path, source: schema.source) if
      type.nil?

    # Handle nils early on
    if Types.nil?(type)
      raise InvalidParameterError.new('cannot be null', path: param.path, source: schema.source) unless
        schema.optional? && TypedParams.config.ignore_nil_optionals ||
        schema.allow_nil?

      next
    end

    # Assert type
    raise InvalidParameterError.new("type mismatch (received #{type.humanize} expected #{schema.type.humanize})", path: param.path, source: schema.source) unless
      type == schema.type || type.subtype? && type.archetype == schema.type

    # Assertions for params without children
    if schema.children.nil?
      # Assert non-scalars only contain scalars (unless allowed)
      case
      when schema.hash?
        param.value.each do |key, value|
          next if
            Types.scalar?(value)

          raise InvalidParameterError.new('unpermitted type (expected object of scalar types)', path: Path.new(*param.path.keys, key), source: schema.source) unless
            schema.allow_non_scalars?
        end
      when schema.array?
        param.value.each_with_index do |value, index|
          next if
            Types.scalar?(value)

          raise InvalidParameterError.new('unpermitted type (expected array of scalar types)', path: Path.new(*param.path.keys, index), source: schema.source) unless
            schema.allow_non_scalars?
        end
      end

      # Handle blanks (and false-positive "blank" values)
      if param.value.blank?
        unless param.value == false
          raise InvalidParameterError.new('cannot be blank', path: param.path, source: schema.source) unless
            schema.allow_blank?

          next
        end
      end
    end

    # Assert validations
    schema.validations.each do |validation|
      validation.call(param.value)
    rescue ValidationError => e
      raise InvalidParameterError.new(e.message, path: param.path, source: schema.source)
    end
  end
end