Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store static regexps in constants for re-use #156

Merged
merged 2 commits into from
Oct 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/json-schema/attributes/formats/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
module JSON
class Schema
class DateFormat < FormatAttribute
REGEXP = /\A\d{4}-\d{2}-\d{2}\z/

def self.validate(current_schema, data, fragments, processor, validator, options = {})
if data.is_a?(String)
error_message = "The property '#{build_fragment(fragments)}' must be a date in the format of YYYY-MM-DD"
r = Regexp.new('^\d\d\d\d-\d\d-\d\d$')
if (r.match(data))
if REGEXP.match(data)
begin
Date.parse(data)
rescue Exception
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
return
end
else
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
return
end
end
end
Expand Down
9 changes: 5 additions & 4 deletions lib/json-schema/attributes/formats/date_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,31 @@
module JSON
class Schema
class DateTimeFormat < FormatAttribute
REGEXP = /\A\d{4}-\d{2}-\d{2}T(\d{2}):(\d{2}):(\d{2})([\.,]\d+)?(Z|[+-](\d{2})(:?\d{2})?)?\z/

def self.validate(current_schema, data, fragments, processor, validator, options = {})
# Timestamp in restricted ISO-8601 YYYY-MM-DDThh:mm:ssZ with optional decimal fraction of the second
if data.is_a?(String)
error_message = "The property '#{build_fragment(fragments)}' must be a date/time in the ISO-8601 format of YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DDThh:mm:ss.ssZ"
r = Regexp.new('^\d\d\d\d-\d\d-\d\dT(\d\d):(\d\d):(\d\d)([\.,]\d+)?(Z|[+-](\d\d)(:?\d\d)?)?$')
if (m = r.match(data))
if (m = REGEXP.match(data))
parts = data.split("T")

begin
Date.parse(parts[0])
rescue Exception
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
return
end

begin
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[1].to_i > 23
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[2].to_i > 59
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[3].to_i > 59
rescue Exception
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
return
end
else
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
return
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/json-schema/attributes/formats/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
module JSON
class Schema
class TimeFormat < FormatAttribute
REGEXP = /\A(\d{2}):(\d{2}):(\d{2})\z/

def self.validate(current_schema, data, fragments, processor, validator, options = {})
if data.is_a?(String)
error_message = "The property '#{build_fragment(fragments)}' must be a time in the format of hh:mm:ss"
r = Regexp.new('^(\d\d):(\d\d):(\d\d)$')
if (m = r.match(data))
if (m = REGEXP.match(data))
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[1].to_i > 23
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[2].to_i > 59
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[3].to_i > 59
else
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
return
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/test_jsonschema_draft1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,10 @@ def test_format_time
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
data = {"a" => "12:00:00b"}
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
data = {"a" => "12:00:00"}
assert(JSON::Validator.validate(schema,data,:version => :draft1))
data = {"a" => "12:00:00\nabc"}
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
end


Expand All @@ -660,6 +664,8 @@ def test_format_date
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
data = {"a" => "2010-01-01n"}
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
data = {"a" => "2010-01-01\nabc"}
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
end

def test_format_datetime
Expand All @@ -684,6 +690,8 @@ def test_format_datetime
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
data = {"a" => "2010-01-0112:00:00Z"}
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
data = {"a" => "2010-01-01T12:00:00Z\nabc"}
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
end

def test_format_unknown
Expand Down
6 changes: 6 additions & 0 deletions test/test_jsonschema_draft2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,8 @@ def test_format_time
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
data = {"a" => "12:00:00b"}
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
data = {"a" => "12:00:00\nabc"}
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
end


Expand All @@ -732,6 +734,8 @@ def test_format_date
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
data = {"a" => "2010-01-01n"}
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
data = {"a" => "2010-01-01\nabc"}
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
end

def test_format_datetime
Expand All @@ -756,6 +760,8 @@ def test_format_datetime
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
data = {"a" => "2010-01-0112:00:00Z"}
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
data = {"a" => "2010-01-01T12:00:00Z\nabc"}
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
end

def test_format_unknown
Expand Down
6 changes: 6 additions & 0 deletions test/test_jsonschema_draft3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,8 @@ def test_format_time
assert(!JSON::Validator.validate(schema,data))
data = {"a" => "12:00:00b"}
assert(!JSON::Validator.validate(schema,data))
data = {"a" => "12:00:00\nabc"}
assert(!JSON::Validator.validate(schema,data))
end


Expand All @@ -1033,6 +1035,8 @@ def test_format_date
assert(!JSON::Validator.validate(schema,data))
data = {"a" => "2010-01-01n"}
assert(!JSON::Validator.validate(schema,data))
data = {"a" => "2010-01-01\nabc"}
assert(!JSON::Validator.validate(schema,data))
end

def test_format_datetime
Expand Down Expand Up @@ -1062,6 +1066,8 @@ def test_format_datetime
assert(!JSON::Validator.validate(schema,data))
data = {"a" => "2010-01-0112:00:00Z"}
assert(!JSON::Validator.validate(schema,data))
data = {"a" => "2010-01-01T12:00:00.1Z\nabc"}
assert(!JSON::Validator.validate(schema,data))

# test with a specific timezone
data = {"a" => "2010-01-01T12:00:00+01"}
Expand Down