Sha256: 56075c65c00057aa5ede61eee94df64c09f1adff191affa6c01eeb145408da47

Contents?: true

Size: 1.66 KB

Versions: 7

Compression:

Stored size: 1.66 KB

Contents

require 'json-schema/attribute'

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"
          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])
            end
          else
            validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
json-schema-2.5.2 lib/json-schema/attributes/formats/date_time.rb
json-schema-openc-fork-0.0.2 lib/json-schema/attributes/formats/date_time.rb
json-schema-2.5.1 lib/json-schema/attributes/formats/date_time.rb
json-schema-openc-fork-0.0.1 lib/json-schema/attributes/formats/date_time.rb
json-schema-2.5.0 lib/json-schema/attributes/formats/date_time.rb
json-schema-2.4.1 lib/json-schema/attributes/formats/date_time.rb
json-schema-2.4.0 lib/json-schema/attributes/formats/date_time.rb