Navigation Menu

Skip to content

Commit

Permalink
Validate effective_date
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Feb 12, 2014
1 parent 7a001c6 commit 8a57580
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/droonga/catalog/base.rb
Expand Up @@ -15,6 +15,7 @@

require "digest/sha1"
require "zlib"
require "time"
require "droonga/message_processing_error"

module Droonga
Expand All @@ -41,6 +42,13 @@ def initialize(name, expected, actual, path)
end
end

class InvalidDate < ValidationError
def initialize(name, value, path)
super("\"#{name}\" must be a valid datetime. " +
"\"#{value}\" cannot be parsed as a datetime.", path)
end
end

class NegativeNumber < ValidationError
def initialize(name, actual, path)
super("\"#{name}\" must be a positive number, but #{actual}.", path)
Expand All @@ -60,6 +68,7 @@ def initialize(data, path)
@path = path
@base_path = File.dirname(path)

validate_effective_date
validate_zones
validate_farms
validate_datasets
Expand Down Expand Up @@ -189,6 +198,15 @@ def validate_parameter_type(expected, value, name)
end
end

def validate_valid_datetime(value, name)
validate_parameter_type(String, value, name)
begin
Time.parse(value)
rescue ArgumentError => error
raise InvalidDate.new(name, value, @path)
end
end

def validate_positive_numeric_parameter(value, name)
validate_parameter_type(Numeric, value, name)
if value < 0
Expand All @@ -210,6 +228,10 @@ def validate_one_or_larger_integer_parameter(value, name)
end
end

def validate_effective_date
validate_valid_datetime(@data["effective_date"], "effective_date")
end

def validate_zones
zones = @data["zones"]

Expand Down

0 comments on commit 8a57580

Please sign in to comment.