Skip to content

Commit

Permalink
adding DecimalValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
kiddrew committed Feb 21, 2016
1 parent 6294c01 commit 81bc67e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
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 @@ -374,6 +374,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 81bc67e

Please sign in to comment.