lib/restapi/validator.rb in restapi-0.0.3 vs lib/restapi/validator.rb in restapi-0.0.4

- old
+ new

@@ -10,13 +10,18 @@ def initialize(param_description) @param_description = param_description end + def self.inherited(subclass) + @validators ||= [] + @validators.insert 0, subclass + end + # find the right validator for given options def self.find(param_description, argument, options, block) - self.subclasses.each do |validator_type| + @validators.each do |validator_type| validator = validator_type.build(param_description, argument, options, block) return validator if validator end return nil end @@ -47,41 +52,55 @@ def to_json self.description end + # what type is expected, mostly string + # this information is used in cli client + # thor supported types — :string, :hash, :array, :numeric, or :boolean + def expected_type + 'string' + end + end # validate arguments type class TypeValidator < BaseValidator def initialize(param_description, argument) super(param_description) @type = argument - @type = Integer if @type == Fixnum end def validate(value) return false if value.nil? - begin - Kernel.send(@type.to_s, value) - rescue ArgumentError - return false - end + value.is_a? @type end def self.build(param_description, argument, options, block) - self.new(param_description, argument) if argument.is_a?(Class) && (argument != Hash || block.nil?) + if argument.is_a?(Class) && (argument != Hash || block.nil?) + self.new(param_description, argument) + end end def error - "Parameter #{@param_name} expecting to be #{@type.name}, got: #{@error_value.class.name}" + "Parameter #{param_name} expecting to be #{@type.name}, got: #{@error_value.class.name}" end def description "Parameter has to be #{@type}." end + + def expected_type + if @type.ancestors.include? Hash + 'hash' + elsif @type.ancestors.include? Numeric + 'numeric' + else + 'string' + end + end end # validate arguments value with regular expression class RegexpValidator < BaseValidator @@ -97,11 +116,11 @@ 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}'" + "Parameter #{param_name} expecting to match /#{@regexp.source}/, got '#{@error_value}'" end def description "Parameter has to match regular expression /#{@regexp.source}/." end @@ -122,11 +141,11 @@ 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(',')}." + "Parameter #{param_name} has bad value (#{@error_value.inspect}). Expecting one of: #{@array.join(',')}." end def description "Parameter has to be one of: #{@array.join(', ')}." end @@ -146,11 +165,11 @@ 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}" + "Parameter #{param_name} has bad value (\"#{@error_value}\"). #{@help}" end def description "" end @@ -181,21 +200,25 @@ end return true end def error - "TODO" + "Has to be hash." end def description - "TODO" + "Has to be hash." end def param(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 + + def expected_type + 'hash' end end end end