lib/validator.rb in kharon-0.1.0 vs lib/validator.rb in kharon-0.2.0
- old
+ new
@@ -1,6 +1,6 @@
-module Charon
+module Kharon
# The validator uses aquarium as an AOP DSL to provide "before" and "after" joint point to its main methods.
# @author Vincent Courtois <vincent.courtois@mycar-innovations.com>
class Validator
include Aquarium::DSL
@@ -30,11 +30,11 @@
# Checks if the given key is a numeric or not.
# @param [Object] key the key about which verify the type.
# @param [Hash] options a hash of options passed to this method (see documentation to know which options pass).
def numeric(key, options = {})
- match?(key, /\A([+-]?\d+)([,.](\d+))?\Z/) ? store(key, ->(item){item.sub(/,/, ".").to_f}, options) : raise_type_error(key, "Numeric")
+ match?(key, /\A([+-]?\d+)([,.](\d+))?\Z/) ? store(key, ->(item){item.to_s.sub(/,/, ".").to_f}, options) : raise_type_error(key, "Numeric")
end
# Checks if the given key is a not-empty string or not.
# @param [Object] key the key about which verify the type.
# @param [Hash] options a hash of options passed to this method (see documentation to know which options pass).
@@ -106,11 +106,11 @@
end
end
end
# After advice checking in numerics if limits are given, and if there are, if they are respected.
- after calls_to: [:integer, :numeric] do |joint_point, validator, *args|
+ before calls_to: [:integer, :numeric] do |joint_point, validator, *args|
unless !defined?(args[1]) or args[1].nil? or args[1].empty?
if(args[1].has_key?(:between))
validator.check_min_value(args[0], args[1][:between][0])
validator.check_max_value(args[0], args[1][:between][1])
else
@@ -118,10 +118,22 @@
validator.check_max_value(args[0], args[1][:max]) if(args[1].has_key?(:max))
end
end
end
+ after calls_to: [:numeric] do |joint_point, validator, *args|
+ unless !defined?(args[1]) or args[1].nil? or args[1].empty?
+ if(args[1].has_key?(:round) and args[1][:round].kind_of?(Integer))
+ validator.filtered[args[0]] = validator.filtered[args[0]].round(args[1][:round]) if validator.filtered.has_key?(args[0])
+ elsif(args[1].has_key?(:floor) and args[1][:floor] == true)
+ validator.filtered[args[0]] = validator.filtered[args[0]].floor if validator.filtered.has_key?(args[0])
+ elsif(args[1].has_key?(:ceil) and args[1][:ceil] == true)
+ validator.filtered[args[0]] = validator.filtered[args[0]].ceil if validator.filtered.has_key?(args[0])
+ end
+ end
+ end
+
# After advcie for all methods, checking the "in" and "equals" options.
after calls_to: self.instance_methods(false), exclude_methods: [:initialize, :new, :required, :dependency, :dependencies] do |joint_point, validator, *args|
unless !defined?(args[1]) or args[1].nil? or args[1].empty?
if(args[1].has_key?(:in))
validator.in_array?(args[0], args[1][:in])
@@ -144,10 +156,16 @@
unless !defined?(args[1]) or args[1].nil? or args[1].empty?
validator.contains?(args[0], validator.datas[args[0]], args[1][:contains]) if(args[1].has_key?(:contains))
end
end
+ after calls_to: [:text] do |joint_point, validator, *args|
+ unless !defined?(args[1]) or args[1].nil? or args[1].empty?
+ validator.match_regex?(args[0], validator.datas[args[0]], args[1][:regex]) if(args[1].has_key?(:regex))
+ end
+ end
+
# @!endgroup Advices
# Checks if a required key is present in provided datas.
# @param [Object] key the key of which check the presence.
# @raise [ArgumentError] if the key is not present.
@@ -216,9 +234,14 @@
# @param [Object] key the key associated with the value to check.
# @param [Array] required_keys the values that the initial Enumerable typed value should contain.
# @raise [ArgumentError] if the initial value has not each and every one of the given values.
def contains?(key, values, required_values)
raise ArgumentError.new("The key #{key} was supposed to contains values [#{required_values.join(", ")}]") if (values & required_values) != required_values
+ end
+
+ def match_regex?(key, value, regex)
+ regex = Regexp.new(regex) if regex.kind_of?(String)
+ raise ArgumentError.new("The key #{key} was supposed to match the regex #{regex} but its value was #{value}") unless regex.match(value)
end
private
# Check if the value associated with the given key matches the given regular expression.
\ No newline at end of file