Sha256: 357dbaf14b7fae9b764aed4d87684a9e0db40ed6e217a37c88b008b3055aa413

Contents?: true

Size: 1.91 KB

Versions: 20

Compression:

Stored size: 1.91 KB

Contents

require 'test/unit'
require File.dirname(__FILE__) + '/../lib/json-schema'

class BitwiseAndAttribute < JSON::Schema::Attribute
  def self.validate(current_schema, data, fragments, validator, options = {})
    if data.is_a?(Integer) && data & current_schema.schema['bitwise-and'].to_i == 0
      message = "The property '#{build_fragment(fragments)}' did not evaluate  to true when bitwise-AND'd with  #{current_schema.schema['bitwise-or']}"
      raise JSON::Schema::ValidationError.new(message, fragments, current_schema)
    end
  end
end

class ExtendedSchema < JSON::Schema::Validator
  def initialize
    super
    extend_schema_definition("http://json-schema.org/draft-03/schema#")
    @attributes["bitwise-and"] = BitwiseAndAttribute
    @uri = URI.parse("http://test.com/test.json")
  end
  
  JSON::Validator.register_validator(self.new)
end

class JSONSchemaTestExtendedSchema < Test::Unit::TestCase
  def test_schema_from_file
    schema = {
      "$schema" => "http://json-schema.org/draft-03/schema#",
      "properties" => {
        "a" => {
          "bitwise-and" => 1
        },
        "b" => {
          "type" => "string"
        }
      }
    }
    
    data = {"a" => 0, "b" => "taco"}
    assert(JSON::Validator.validate(schema,data))
    data = {"a" => 1, "b" => "taco"}
    assert(JSON::Validator.validate(schema,data))
    data = {"a" => 1, "b" => 5}
    assert(!JSON::Validator.validate(schema,data))
    
    schema = {
      "$schema" => "http://test.com/test.json",
      "properties" => {
        "a" => {
          "bitwise-and" => 1
        },
        "b" => {
          "type" => "string"
        }
      }
    }
    
    data = {
      "a" => 0
    }
    
    data = {"a" => 1, "b" => "taco"}
    assert(JSON::Validator.validate(schema,data))
    data = {"a" => 0, "b" => "taco"}
    assert(!JSON::Validator.validate(schema,data))
    data = {"a" => 1, "b" => 5}
    assert(!JSON::Validator.validate(schema,data))
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
json-schema-1.0.3 test/test_extended_schema.rb
json-schema-1.0.2 test/test_extended_schema.rb
seomoz-json-schema-1.0.1 test/test_extended_schema.rb
json-schema-1.0.1 test/test_extended_schema.rb
json-schema-1.0.0 test/test_extended_schema.rb
json-schema-0.9.12 test/test_extended_schema.rb
json-schema-0.9.11 test/test_extended_schema.rb
json-schema-0.9.10 test/test_extended_schema.rb
json-schema-0.9.9 test/test_extended_schema.rb
json-schema-0.9.8 test/test_extended_schema.rb
json-schema-0.9.7 test/test_extended_schema.rb
json-schema-0.9.6 test/test_extended_schema.rb
json-schema-0.9.5 test/test_extended_schema.rb
json-schema-0.9.4 test/test_extended_schema.rb
json-schema-0.9.3 test/test_extended_schema.rb
json-schema-0.9.2 test/test_extended_schema.rb
json-schema-0.9.1 test/test_extended_schema.rb
json-schema-0.9.0 test/test_extended_schema.rb
json-schema-0.2.0 test/test_extended_schema.rb
json-schema-0.1.14 test/test_extended_schema.rb