diff --git a/README.rst b/README.rst index 99a3cdf6..003d6c99 100644 --- a/README.rst +++ b/README.rst @@ -1163,6 +1163,17 @@ ArrayValidator Check if the parameter is an array +DecimalValidator +-------------- + +Check if the parameter is a decimal number + +.. code:: ruby + + param :latitude, :decimal, :desc => "Geographic latitude", :required => true + param :longitude, :decimal, :desc => "Geographic longitude", :required => true + + Additional options ~~~~~~~~~~~~~~~~~ diff --git a/lib/apipie/extractor/collector.rb b/lib/apipie/extractor/collector.rb index 195e4179..84620223 100644 --- a/lib/apipie/extractor/collector.rb +++ b/lib/apipie/extractor/collector.rb @@ -75,6 +75,10 @@ def refine_params_description(params_desc, recorded_params) if param_desc[:type].first == :number && (key.to_s !~ /id$/ || !Apipie::Validator::NumberValidator.validate(value)) param_desc[:type].shift end + + if param_desc[:type].first == :decimal && (key.to_s !~ /id$/ || !Apipie::Validator::DecimalValidator.validate(value)) + param_desc[:type].shift + end end if value.is_a? Hash diff --git a/lib/apipie/validator.rb b/lib/apipie/validator.rb index 30ddbc18..dd90071a 100644 --- a/lib/apipie/validator.rb +++ b/lib/apipie/validator.rb @@ -398,6 +398,27 @@ def description end end + class DecimalValidator < BaseValidator + + def validate(value) + self.class.validate(value) + end + + def self.build(param_description, argument, options, block) + if argument == :decimal + self.new(param_description) + end + end + + def description + "Must be a decimal number." + end + + def self.validate(value) + value.to_s =~ /\A^[-+]?[0-9]+([,.][0-9]+)?\Z$/ + end + end + class NumberValidator < BaseValidator def validate(value)