Class: Dragnet::Validators::Fields::ReposValidator

Inherits:
FieldValidator show all
Defined in:
lib/dragnet/validators/fields/repos_validator.rb

Overview

Validates that the repos attribute in an MTR is valid. This means:

  • It is either a Hash or an Array of Hashes.

  • The attributes inside each of the Hashes are also valid.

Instance Method Summary collapse

Instance Method Details

#validate(key, value) ⇒ Array<Dragnet::Repo>?

Validates the MTR's repos field.

Parameters:

  • key (String)

    The name of the key (usually 'repos')

  • value (Object)

    The value associated to the attribute.

Returns:

  • (Array<Dragnet::Repo>, nil)

    If value is a valid Hash or a valid Array of Hashes an Array of Dragnet::Repo objects is returned. If value is nil, nil is returned.

Raises:

See Also:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dragnet/validators/fields/repos_validator.rb', line 23

def validate(key, value)
  return unless value

  validate_type(key, value, Hash, Array)

  if value.is_a?(Array)
    return if value.empty?

    validate_array_types(key, value, Hash)
  else
    # This is needed because trying to apply the splat operator over a
    # Hash will result in an Array of Arrays (one for each of the Hash's
    # key pairs).
    value = [value]
  end

  create_repos(value)
end