Class: Validation::Rule::DiasporaIdList

Inherits:
Object
  • Object
show all
Defined in:
lib/diaspora_federation/validators/rules/diaspora_id_list.rb

Overview

Rule for validating the number of diaspora* IDs in a string. The evaluated string is split at “;”.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ DiasporaIdList

Creates a new rule for a diaspora* ID list validation

Parameters:

  • params (Hash) (defaults to: {})

Options Hash (params):

  • :minimum (Integer)

    minimum allowed id count

  • :maximum (Integer)

    maximum allowed id count



16
17
18
19
20
21
22
23
24
# File 'lib/diaspora_federation/validators/rules/diaspora_id_list.rb', line 16

def initialize(params={})
  %i[minimum maximum].each do |param|
    if params.include?(param) && !params[param].is_a?(Integer)
      raise ArgumentError, "The :#{param} needs to be an Integer"
    end
  end

  @params = params
end

Instance Attribute Details

#paramsHash (readonly)

This rule can have a minimum or maximum param.

Returns:

  • (Hash)

    params



10
11
12
# File 'lib/diaspora_federation/validators/rules/diaspora_id_list.rb', line 10

def params
  @params
end

Instance Method Details

#error_keySymbol

The error key for this rule

Returns:

  • (Symbol)

    error key



28
29
30
# File 'lib/diaspora_federation/validators/rules/diaspora_id_list.rb', line 28

def error_key
  :diaspora_id_list
end

#valid_value?(value) ⇒ Boolean

Returns:



32
33
34
35
36
37
38
39
40
41
# File 'lib/diaspora_federation/validators/rules/diaspora_id_list.rb', line 32

def valid_value?(value)
  ids = value.split(";")
  return false if params.include?(:maximum) && ids.count > params[:maximum]
  return false if params.include?(:minimum) && ids.count < params[:minimum]

  ids.each do |id|
    return false if DiasporaId::DIASPORA_ID.match(id).nil?
  end
  true
end