lib/apipie/validator.rb in apipie-rails-0.0.11 vs lib/apipie/validator.rb in apipie-rails-0.0.12

- old
+ new

@@ -45,10 +45,14 @@ # validator description def description "TODO: validator description" end + def error + ParamInvalid.new(param_name, @error_value, description) + end + def to_s self.description end def to_json @@ -81,16 +85,12 @@ 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}" - end - def description - "Parameter has to be #{@type}." + "Must be #{@type}" end def expected_type if @type.ancestors.include? Hash 'hash' @@ -116,16 +116,12 @@ 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 - def description - "Parameter has to match regular expression /#{@regexp.source}/." + "Must match regular expression /#{@regexp.source}/." end end # arguments value must be one of given in array class ArrayValidator < BaseValidator @@ -141,16 +137,12 @@ 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 - def description - "Parameter has to be one of: #{@array.join(', ')}." + "Must be one of: #{@array.join(', ')}." end end class ProcValidator < BaseValidator @@ -166,11 +158,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}" + ParamInvalid.new(param_name, @error_value, @help) end def description "" end @@ -200,20 +192,16 @@ end end return true end - def error - "Has to be hash." - end - def description - "Has to be hash." + "Must be a Hash" end - def param(param_name, *args, &block) - param_description = Apipie::ParamDescription.new(param_name, *args, &block) + def param(param_name, validator, desc_or_options = nil, options = {}, &block) + param_description = Apipie::ParamDescription.new(param_name, validator, desc_or_options, options, &block) param_description.parent = self.param_description @hash_params_ordered << param_description @hash_params[param_name.to_sym] = param_description end @@ -251,16 +239,12 @@ if argument == :number self.new(param_description) end end - def error - "Parameter #{param_name} expecting to be a number, got: #{@error_value}" - end - def description - "Has to be a number." + "Must be a number." end def self.validate(value) value.to_s =~ /\A(0|[1-9]\d*)\Z$/ end @@ -276,15 +260,11 @@ if argument == :bool self.new(param_description) end end - def error - "Parameter #{param_name} expecting to be a boolean value, got: #{@error_value}" - end - def description - "Has to be a boolean" + "Must be 'true' or 'false'" end end end end