Class: Lore::Validation::Parameter_Validator
- Defined in:
- lib/lore/validation/parameter_validator.rb
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
-
.validate(klass, value_hash) ⇒ Object
To be used inside Table_Accessor with Validator.invalid_params(this, @attribute_values) or e.g.
- .validate_constraints(table_constraints, table_value_hash) ⇒ Object
- .validate_types(type_codes, table_value_hash, required) ⇒ Object
- .validate_update(klass, value_hash) ⇒ Object
Class Method Details
.validate(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)
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 |
# File 'lib/lore/validation/parameter_validator.rb', line 26 def self.validate(klass, value_hash) Lore.logger.debug { "Validating attributes for #{klass.to_s}: " } Lore.logger.debug { "#{value_hash.inspect}" } invalid_params = Hash.new attribute_settings = klass.__attributes__ constraints = attribute_settings.constraints required = attribute_settings.required attribute_settings.types.each_pair { |table, fields| begin validate_types(fields, value_hash[table], required[table]) rescue Lore::Exceptions::Invalid_Types => ip invalid_params[table] = ip end } attribute_settings.constraints.each_pair { |table, fields| begin validate_constraints(fields, value_hash[table]) rescue Lore::Exceptions::Unmet_Constraints => ip invalid_params[table] = ip end } if invalid_params.length == 0 then return true end raise Lore::Exceptions::Validation_Failure.new(klass, invalid_params) end |
.validate_constraints(table_constraints, table_value_hash) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/lore/validation/parameter_validator.rb', line 133 def self.validate_constraints(table_constraints, table_value_hash) unmet_constraints = {} table_constraints.each_pair { |attrib, rules| value = table_value_hash[attrib.to_s] if table_value_hash rules.each_pair { |rule, rule_value| Lore.logger.debug { "Found constraint for #{attrib}: #{rule.inspect} " } Lore.logger.debug { "Rule is: #{rule_value.inspect} " } if rule == :minlength && value.to_s.length < rule_value then unmet_constraints[attrib] = :minlength Lore.logger.debug { "Field #{attrib} failed :minlength" } end if rule == :maxlength && value.to_s.length > rule_value then unmet_constraints[attrib] = :maxlength Lore.logger.debug { "Field #{attrib} failed :maxlength" } end if rule == :format && rule_value.match(value).nil? then unmet_constraints[attrib] = :format Lore.logger.debug { "Field #{attrib} failed :format" } end } } if unmet_constraints.length > 0 then raise Lore::Exceptions::Unmet_Constraints.new(unmet_constraints) end return true end |
.validate_types(type_codes, table_value_hash, required) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/lore/validation/parameter_validator.rb', line 96 def self.validate_types(type_codes, table_value_hash, required) invalid_types = {} value = false type_validator = Type_Validator.new() # DERRN-DERRN-DERRN!! What if there is no value hash? type_codes.each_pair { |field,type| field = field.to_sym is_required = (required && required[field]) || false value = table_value_hash[field] if table_value_hash # Replace whitespaces and array delimiters to check for real value length value_nil = (value.nil? || value.to_s.gsub(/\s/,'').gsub(/[{}]/,'').length == 0) Lore.logger.debug { "Validate #{field}, required? #{is_required}, " << "value: #{value.inspect}, value nil? #{value_nil}" } # Is value missing? if (is_required && value_nil) then invalid_types[field] = :missing Lore.logger.debug { "Field #{field} is :missing" } # Otherwise: Is value of valid type? elsif !value_nil && !type_validator.typecheck(type, value, is_required) then invalid_types[field] = type Lore.logger.debug { "Field #{field} has invalid type: " << "expected: #{type}, value: #{value}" } end } if invalid_types.keys.length > 0 then raise Lore::Exceptions::Invalid_Types.new(invalid_types) end return true end |
.validate_update(klass, value_hash) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/lore/validation/parameter_validator.rb', line 56 def self.validate_update(klass, value_hash) Lore.logger.debug { "Validating attributes for updating #{klass.to_s}: " } Lore.logger.debug { "#{value_hash.inspect}" } invalid_params = Hash.new attribute_settings = klass.__attributes__ constraints = attribute_settings.constraints required = attribute_settings.required value_hash.each_pair { |table, attributes| types = attribute_settings.types[table] if !types then raise ::Exception.new("No types given for #{klass.to_s} (#{table})") end types.delete_if { |attribute,value| !attributes[attribute] } begin validate_types(types, attributes, required[table]) rescue Lore::Exceptions::Invalid_Types => ip invalid_params[table] = ip end } value_hash.each_pair { |table, attributes| constraints = attribute_settings.constraints[table] if constraints then constraints.delete_if { |attribute,constraint| !attributes[attribute] } begin validate_constraints(constraints, attributes) rescue Lore::Exceptions::Unmet_Constraints => ip invalid_params[table] = ip end else Lore.logger.info { "No constraints for #{klass.to_s}?" } end } if invalid_params.length == 0 then return true end raise Lore::Exceptions::Validation_Failure.new(klass, invalid_params) end |