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

Options Hash (params):

  • :minimum (Integer)

    minimum allowed id count

  • :maximum (Integer)

    maximum allowed id count


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

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.


8
9
10
# File 'lib/diaspora_federation/validators/rules/diaspora_id_list.rb', line 8

def params
  @params
end

Instance Method Details

#error_keySymbol

The error key for this rule


26
27
28
# File 'lib/diaspora_federation/validators/rules/diaspora_id_list.rb', line 26

def error_key
  :diaspora_id_list
end

#valid_value?(value) ⇒ Boolean


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

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