Sha256: 641a3419b33d3be24515814f27f103bc433133084c9bc27e4a1d2d867506c086

Contents?: true

Size: 1.2 KB

Versions: 66

Compression:

Stored size: 1.2 KB

Contents

require 'json-schema'

module Marty

  private
  class PgEnumAttribute < JSON::Schema::Attribute
    def self.validate(curr_schema, data, frag, pro, validator, opt={})
      values = nil
      path = '#/' + frag.join('/')
      begin
        cs = curr_schema.schema["pg_enum"]
        enum = cs.constantize
        values = enum::VALUES
      rescue => e
        msg = "The property '#{path}': '#{cs}' is not a pg_enum class"
        validation_error(pro, msg, frag, curr_schema, self, opt[:record_errors])
      end
      if values && !values.include?(data)
        msg = "The property '#{path}' value '#{data}' not contained in #{enum}"
        validation_error(pro, msg, frag, curr_schema, self, opt[: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
      @formats["date-time"]  = JSON::Schema::DateTimeFormat
      @formats["date"]       = JSON::Schema::DateFormat
      @uri                   = JSON::Util::URI.parse(RAW_URI)
      @names                 = ["marty-draft", RAW_URI]
    end

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

end

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
marty-2.4.1 lib/marty/json_schema.rb
marty-2.4.0 lib/marty/json_schema.rb
marty-2.3.15 lib/marty/json_schema.rb
marty-2.3.14 lib/marty/json_schema.rb
marty-2.3.13 lib/marty/json_schema.rb
marty-2.3.12 lib/marty/json_schema.rb
marty-2.3.11 lib/marty/json_schema.rb
marty-2.3.10 lib/marty/json_schema.rb
marty-2.3.9 lib/marty/json_schema.rb
marty-2.3.8 lib/marty/json_schema.rb
marty-2.3.7 lib/marty/json_schema.rb
marty-2.3.5 lib/marty/json_schema.rb
marty-2.3.4 lib/marty/json_schema.rb
marty-2.1.5 lib/marty/json_schema.rb
marty-2.3.2 lib/marty/json_schema.rb
marty-2.3.1 lib/marty/json_schema.rb
marty-2.3.0 lib/marty/json_schema.rb
marty-2.1.4 lib/marty/json_schema.rb
marty-2.1.3 lib/marty/json_schema.rb
marty-2.1.2 lib/marty/json_schema.rb