Sha256: 2c1576234b5ef6e77b259ad0d81f69169829553bdf0e1636ea777766e6bdb414

Contents?: true

Size: 1.58 KB

Versions: 6

Compression:

Stored size: 1.58 KB

Contents

require 'json-schema'

module Marty

  private
  class PgEnumAttribute < JSON::Schema::Attribute
    def self.validate(curr_schema, data, frag, pro, validator, opt={})
      enum = nil
      begin
        enum = curr_schema.schema["pg_enum"].constantize
      rescue
        msg = "#{self.class.name} error: '#{data}' is not a pg_enum class"
        validation_error(pro, msg, frag, curr_schema, self, opt[:record_errors])
      end
      if !enum::VALUES.include?(data)
        msg = "#{self.class.name} error: '#{data}' not contained in #{enum}"
        validation_error(pro, msg, frag, curr_schema, self, opt[:record_errors])
      end
    end
  end

  class DateTimeFormatAttribute < JSON::Schema::Attribute
    def self.validate(curr_schema, data, frag, processor, validator, options={})
      begin
        DateTime.parse(data).in_time_zone(Rails.configuration.time_zone)
      rescue
        msg = "#{self.class.name} error: Can't parse '#{data}' into a DateTime"
        validation_error( processor,
                          msg,
                          frag,
                          curr_schema,
                          self,
                          options[:record_errors])
      end
    end
  end

  class JsonSchema < JSON::Schema::Draft4
    RAW_URI = "http://json-schema.org/marty-draft/schema#"

    def initialize
      super
      @attributes["pg_enum"] = PgEnumAttribute
      @attributes["datetime_format"] = DateTimeFormatAttribute
      @uri = JSON::Util::URI.parse(RAW_URI)
      @names = ["marty-draft", RAW_URI]
    end

    JSON::Validator.register_validator(self.new)
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
marty-1.0.33 lib/marty/json_schema.rb
marty-1.0.32 lib/marty/json_schema.rb
marty-1.0.31 lib/marty/json_schema.rb
marty-1.0.30 lib/marty/json_schema.rb
marty-1.0.29 lib/marty/json_schema.rb
marty-1.0.28 lib/marty/json_schema.rb