Class: Lore::Validation::Parameter_Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/lore/validation/parameter_validator.rb

Overview

:nodoc:

Constant Summary collapse

PG_BOOL =
16
PG_SMALLINT =
21
PG_INT =
23
PG_TEXT =
25
PG_VARCHAR =
1043
PG_TIMESTAMP_TIMEZONE =
1184

Class Method Summary collapse

Class Method Details

.invalid_params(klass, value_hash) ⇒ Object

To be used inside Table_Accessor with Validator.invalid_params(this, @attribute_values) or e.g. in a dispatcher with Validator.invalid_params(Some_Klass, parameter_hash)



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
# File 'lib/lore/validation/parameter_validator.rb', line 25

def self.invalid_params(klass, value_hash)

  invalid_params = Hash.new
  explicit_attributes = klass.get_explicit_attributes
  constraints = klass.get_constraints

  klass.get_attribute_types.each_pair { |table, fields|
    begin
      validate_types(fields, value_hash[table], explicit_attributes[table]) 
    rescue Exception::Invalid_Types => ip
      invalid_params[table] = ip
    end
  }
  
  klass.get_constraints.each_pair { |table, fields|
    begin
      validate_constraints(fields, value_hash[table])
    rescue Exception::Unmet_Constraints => ip
      invalid_params[table] = ip
    end
  }
  if invalid_params.length == 0 then return true end
    
  raise Lore::Exception::Invalid_Klass_Parameters.new(klass, invalid_params)

end

.validate_constraints(table_constraints, table_value_hash) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lore/validation/parameter_validator.rb', line 79

def self.validate_constraints(table_constraints, table_value_hash)
  unmet_constraints = {}
  table_constraints.each_pair { |attrib, rules|
    value = table_value_hash[attrib.to_s]
    rules.each_pair { |rule, rule_value|
      if rule == :minlength && value.to_s.length < rule_value then
        unmet_constraints[attrib] = :minlength
      end
      if rule == :maxlength && value.to_s.length > rule_value then
        unmet_constraints[attrib] = :maxlength
      end
      if rule == :format && rule_value.match(value).nil? then
        unmet_constraints[attrib] = :format
      end
    }
  }
  if unmet_constraints.length > 0 then
    raise Lore::Exception::Unmet_Constraints.new(unmet_constraints)
  end
  return true
end

.validate_types(type_codes, table_value_hash, table_explicit_attributes) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/lore/validation/parameter_validator.rb', line 52

def self.validate_types(type_codes, table_value_hash, table_explicit_attributes)
  invalid_types = {} 
  value = false
  type_validator = Type_Validator.new()
  type_codes.each_pair { |field, type|
    
    nil_allowed = table_explicit_attributes.nil? ||
                  !table_explicit_attributes.include?(field)

    value = table_value_hash[field]
    # Replace whitespaces and array delimiters to check for real value length
    value_nil = (value.nil? || value.to_s.gsub(/\s/,'').gsub(/[{}]/,'').length == 0)
    # Is value missing? 
    if (!nil_allowed && value_nil) then 
      invalid_types[field] = :missing
    # If so: Is value of valid type? 
    elsif !type_validator.typecheck(type, value, nil_allowed) then
      invalid_types[field] = type
    end
    
  }
  if invalid_types.keys.length > 0 then
      raise Lore::Exception::Invalid_Types.new(invalid_types)
  end
  return true
end