lib/restapi/validator.rb in restapi-0.0.2 vs lib/restapi/validator.rb in restapi-0.0.3
- old
+ new
@@ -4,16 +4,20 @@
# to create new validator, inherit from Restapi::Validator::Base
# and implement class method build and instance method validate
class BaseValidator
- attr_accessor :param_name
+ attr_accessor :param_description
+ def initialize(param_description)
+ @param_description = param_description
+ end
+
# find the right validator for given options
- def self.find(argument, options, block)
+ def self.find(param_description, argument, options, block)
self.subclasses.each do |validator_type|
- validator = validator_type.build(argument, options, block)
+ validator = validator_type.build(param_description, argument, options, block)
return validator if validator
end
return nil
end
@@ -26,10 +30,14 @@
@error_value = value
false
end
end
+ def param_name
+ @param_description.name
+ end
+
# validator description
def description
"TODO: validator description"
end
@@ -44,11 +52,12 @@
end
# validate arguments type
class TypeValidator < BaseValidator
- def initialize(argument)
+ def initialize(param_description, argument)
+ super(param_description)
@type = argument
@type = Integer if @type == Fixnum
end
def validate(value)
@@ -58,12 +67,12 @@
rescue ArgumentError
return false
end
end
- def self.build(argument, options, block)
- self.new(argument) if argument.is_a?(Class) && argument != Hash
+ def self.build(param_description, argument, options, block)
+ self.new(param_description, argument) if argument.is_a?(Class) && (argument != Hash || block.nil?)
end
def error
"Parameter #{@param_name} expecting to be #{@type.name}, got: #{@error_value.class.name}"
end
@@ -74,20 +83,21 @@
end
# validate arguments value with regular expression
class RegexpValidator < BaseValidator
- def initialize(argument)
+ def initialize(param_description, argument)
+ super(param_description)
@regexp = argument
end
def validate(value)
value =~ @regexp
end
- def self.build(argument, options, proc)
- self.new(argument) if argument.is_a? Regexp
+ def self.build(param_description, argument, options, proc)
+ self.new(param_description, argument) if argument.is_a? Regexp
end
def error
"Parameter #{@param_name} expecting to match /#{@regexp.source}/, got '#{@error_value}'"
end
@@ -98,30 +108,21 @@
end
# arguments value must be one of given in array
class ArrayValidator < BaseValidator
- def initialize(argument)
+ def initialize(param_description, argument)
+ super(param_description)
@array = argument
end
def validate(value)
-
- @array.find do |expected|
- expected_class = expected.class
- expected_class = Integer if expected_class == Fixnum
- begin
- converted_value = Kernel.send(expected_class.to_s, value)
- rescue ArgumentError
- false
- end
- converted_value === expected
- end
+ @array.include?(value)
end
- def self.build(argument, options, proc)
- self.new(argument) if argument.is_a?(Array)
+ def self.build(param_description, argument, options, proc)
+ self.new(param_description, argument) if argument.is_a?(Array)
end
def error
"Parameter #{@param_name} has bad value (#{@error_value.inspect}). Expecting one of: #{@array.join(',')}."
end
@@ -131,20 +132,21 @@
end
end
class ProcValidator < BaseValidator
- def initialize(argument)
+ def initialize(param_description, argument)
+ super(param_description)
@proc = argument
end
def validate(value)
(@help = @proc.call(value)) === true
end
- def self.build(argument, options, proc)
- self.new(argument) if argument.is_a?(Proc) && argument.arity == 1
+ def self.build(param_description, argument, options, proc)
+ self.new(param_description, argument) if argument.is_a?(Proc) && argument.arity == 1
end
def error
"Parameter #{@param_name} has bad value (\"#{@error_value}\"). #{@help}"
end
@@ -154,16 +156,20 @@
end
end
class HashValidator < BaseValidator
- def self.build(argument, options, block)
- self.new(block) if block.is_a?(Proc) && block.arity <= 0 && argument == Hash
+ attr_reader :hash_params_ordered
+
+ def self.build(param_description, argument, options, block)
+ self.new(param_description, block) if block.is_a?(Proc) && block.arity <= 0 && argument == Hash
end
- def initialize(argument)
+ def initialize(param_description, argument)
+ super(param_description)
@proc = argument
+ @hash_params_ordered = []
@hash_params = {}
self.instance_exec(&@proc)
end
@@ -183,11 +189,14 @@
def description
"TODO"
end
def param(param_name, *args, &block)
- @hash_params[param_name.to_sym] = Restapi::ParamDescription.new(param_name, *args, &block)
+ param_description = Restapi::ParamDescription.new(param_name, *args, &block)
+ param_description.parent = self.param_description
+ @hash_params_ordered << param_description
+ @hash_params[param_name.to_sym] = param_description
end
end
end
-end
\ No newline at end of file
+end