Skip to content

Commit

Permalink
new validator - DecimalValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
kiddrew authored and iNecas committed Apr 30, 2018
1 parent f409351 commit e9075fc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~

Expand Down
4 changes: 4 additions & 0 deletions lib/apipie/extractor/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions lib/apipie/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e9075fc

Please sign in to comment.