From feed9b12f5a259ae1ba921309cb7d1f203bd254f Mon Sep 17 00:00:00 2001 From: ILikePies Date: Fri, 11 May 2012 11:07:24 +0200 Subject: [PATCH 1/3] Allow optional decimal fraction for seconds in date-time format validation. --- lib/json-schema/attributes/format.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/json-schema/attributes/format.rb b/lib/json-schema/attributes/format.rb index 6af347e7..d87d245e 100644 --- a/lib/json-schema/attributes/format.rb +++ b/lib/json-schema/attributes/format.rb @@ -4,12 +4,12 @@ class FormatAttribute < Attribute def self.validate(current_schema, data, fragments, validator, options = {}) case current_schema.schema['format'] - # Timestamp in restricted ISO-8601 YYYY-MM-DDThh:mm:ssZ + # Timestamp in restricted ISO-8601 YYYY-MM-DDThh:mm:ssZ with optional decimal fraction of the second when 'date-time' 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" + 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" validation_error(error_message, fragments, current_schema, self, options[:record_errors]) and return if !data.is_a?(String) - r = Regexp.new('^\d\d\d\d-\d\d-\d\dT(\d\d):(\d\d):(\d\d)Z$') + r = Regexp.new('^\d\d\d\d-\d\d-\d\dT(\d\d):(\d\d):(\d\d)(,|\.\d+)?Z$') if (m = r.match(data)) parts = data.split("T") begin From 89e1b363b19b71e26e812186bdba195d51c8d68c Mon Sep 17 00:00:00 2001 From: ILikePies Date: Fri, 11 May 2012 11:35:47 +0200 Subject: [PATCH 2/3] date-time format should accept times with fractional seconds part (with either comma or dot as separator) --- test/test_jsonschema_draft3.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/test_jsonschema_draft3.rb b/test/test_jsonschema_draft3.rb index f4af631f..b1086df3 100644 --- a/test/test_jsonschema_draft3.rb +++ b/test/test_jsonschema_draft3.rb @@ -883,6 +883,10 @@ def test_format_datetime data = {"a" => "2010-01-01T12:00:00Z"} assert(JSON::Validator.validate(schema,data)) + data = {"a" => "2010-01-01T12:00:00.1Z"} + assert(JSON::Validator.validate(schema,data)) + data = {"a" => "2010-01-01T12:00:00,1Z"} + assert(JSON::Validator.validate(schema,data)) data = {"a" => "2010-01-32T12:00:00Z"} assert(!JSON::Validator.validate(schema,data)) data = {"a" => "2010-13-01T12:00:00Z"} From 6e088580afd164363ab6104b12374b5658d12365 Mon Sep 17 00:00:00 2001 From: ILikePies Date: Fri, 11 May 2012 11:36:42 +0200 Subject: [PATCH 3/3] accept fractional second parts with either comma or dot (and do it right!) --- lib/json-schema/attributes/format.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/json-schema/attributes/format.rb b/lib/json-schema/attributes/format.rb index d87d245e..05e7e78f 100644 --- a/lib/json-schema/attributes/format.rb +++ b/lib/json-schema/attributes/format.rb @@ -9,7 +9,7 @@ def self.validate(current_schema, data, fragments, validator, options = {}) 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" validation_error(error_message, fragments, current_schema, self, options[:record_errors]) and return if !data.is_a?(String) - r = Regexp.new('^\d\d\d\d-\d\d-\d\dT(\d\d):(\d\d):(\d\d)(,|\.\d+)?Z$') + r = Regexp.new('^\d\d\d\d-\d\d-\d\dT(\d\d):(\d\d):(\d\d)([\.,]\d+)?Z$') if (m = r.match(data)) parts = data.split("T") begin