Skip to content

Commit

Permalink
Merge pull request #219 from RST-J/number_enum_issue
Browse files Browse the repository at this point in the history
Issue with Fixnum and Float in enum
  • Loading branch information
RST-J committed Feb 23, 2015
2 parents d5080a8 + 7375be1 commit c32a6b2
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 133 deletions.
28 changes: 17 additions & 11 deletions lib/json-schema/util/array_set.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
require 'set'

# This is a hack that I don't want to ever use anywhere else or repeat EVER, but we need enums to be
# an Array to pass schema validation. But we also want fast lookup! And we can't use sets because of
# backport support... so...
# an Array to pass schema validation. But we also want fast lookup!

class ArraySet < Array
def include?(obj)
# On first invocation create a HASH (yeah, yeah) to act as our set given the array values
if !defined? @array_values
@array_values = {}
self.each {|x| @array_values[x] = 1}
end
@array_values.has_key? obj
end
end
def include?(obj)
if !defined? @values
@values = Set.new
self.each { |x| @values << convert_to_float_if_fixnum(x) }
end
@values.include?(convert_to_float_if_fixnum(obj))
end

private

def convert_to_float_if_fixnum(value)
value.is_a?(Fixnum) ? value.to_f : value
end
end
13 changes: 13 additions & 0 deletions lib/json-schema/validators/hyper-draft1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JSON
class Schema

class HyperDraft1 < Draft1
def initialize
super
@uri = Addressable::URI.parse("http://json-schema.org/draft-01/hyper-schema#")
end

JSON::Validator.register_validator(self.new)
end
end
end
13 changes: 13 additions & 0 deletions lib/json-schema/validators/hyper-draft2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module JSON
class Schema

class HyperDraft2 < Draft2
def initialize
super
@uri = Addressable::URI.parse("http://json-schema.org/draft-02/hyper-schema#")
end

JSON::Validator.register_validator(self.new)
end
end
end
1 change: 0 additions & 1 deletion lib/json-schema/validators/hyper-draft4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def initialize
end

JSON::Validator.register_validator(self.new)
JSON::Validator.register_default_validator(self.new)
end
end
end
133 changes: 133 additions & 0 deletions test/support/enum_validation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
module EnumValidation
module V1_V2
def test_enum_optional
schema = {
"properties" => {
"a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}], "optional" => true}
}
}

data = {}
assert_valid schema, data
end
end

module V3_V4
def test_enum_optional
schema = {
"properties" => {
"a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}]}
}
}

data = {}
assert_valid schema, data
end
end

module General
def test_enum_general
schema = {
"properties" => {
"a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}]}
}
}

data = { "a" => 1 }
assert_valid schema, data

data["a"] = 'boo'
assert_valid schema, data

data["a"] = [1,2,3]
assert_valid schema, data

data["a"] = {"a" => "b"}
assert_valid schema, data

data["a"] = 'taco'
refute_valid schema, data
end

def test_enum_number_integer_includes_float
schema = {
"properties" => {
"a" => {
"type" => "number",
"enum" => [0, 1, 2]
}
}
}

data = { "a" => 0 }
assert_valid schema, data

data["a"] = 0.0
assert_valid schema, data

data["a"] = 1
assert_valid schema, data

data["a"] = 1.0
assert_valid schema, data
end

def test_enum_number_float_includes_integer
schema = {
"properties" => {
"a" => {
"type" => "number",
"enum" => [0.0, 1.0, 2.0]
}
}
}

data = { "a" => 0.0 }
assert_valid schema, data

data["a"] = 0
assert_valid schema, data


data["a"] = 1.0
assert_valid schema, data

data["a"] = 1
assert_valid schema, data
end

def test_enum_integer_excludes_float
schema = {
"properties" => {
"a" => {
"type" => "integer",
"enum" => [0, 1, 2]
}
}
}

data = { "a" => 0 }
assert_valid schema, data

data["a"] = 0.0
refute_valid schema, data


data["a"] = 1
assert_valid schema, data

data["a"] = 1.0
refute_valid schema, data
end

def test_enum_with_schema_validation
schema = {
"properties" => {
"a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}]}
}
}
data = { "a" => 1 }
assert_valid(schema, data, :validate_schema => true)
end
end
end
38 changes: 3 additions & 35 deletions test/test_jsonschema_draft1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def exclusive_maximum

include ArrayValidation::ItemsTests

include EnumValidation::General
include EnumValidation::V1_V2

include NumberValidation::MinMaxTests

include ObjectValidation::AdditionalPropertiesTests
Expand Down Expand Up @@ -52,41 +55,6 @@ def test_optional
assert_valid schema, data
end

def test_enum
# Set up the default datatype
schema = {
"properties" => {
"a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}], "optional" => true}
}
}

data = {
"a" => nil
}

# Make sure all of the above are valid...
data["a"] = 1
assert_valid schema, data

data["a"] = 'boo'
assert_valid schema, data

data["a"] = [1,2,3]
assert_valid schema, data

data["a"] = {"a" => "b"}
assert_valid schema, data

# Test something that doesn't exist
data["a"] = 'taco'
refute_valid schema, data

# Try it without the key
data = {}
assert_valid schema, data
end


def test_max_decimal
# Set up the default datatype
schema = {
Expand Down
37 changes: 3 additions & 34 deletions test/test_jsonschema_draft2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def multiple_of
include ArrayValidation::ItemsTests
include ArrayValidation::UniqueItemsTests

include EnumValidation::General
include EnumValidation::V1_V2

include NumberValidation::MinMaxTests
include NumberValidation::MultipleOfTests

Expand Down Expand Up @@ -58,40 +61,6 @@ def test_optional
assert_valid schema, data
end

def test_enum
# Set up the default datatype
schema = {
"properties" => {
"a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}], "optional" => true}
}
}

data = {
"a" => nil
}

# Make sure all of the above are valid...
data["a"] = 1
assert_valid schema, data

data["a"] = 'boo'
assert_valid schema, data

data["a"] = [1,2,3]
assert_valid schema, data

data["a"] = {"a" => "b"}
assert_valid schema, data

# Test something that doesn't exist
data["a"] = 'taco'
refute_valid schema, data

# Try it without the key
data = {}
assert_valid schema, data
end

def test_disallow
# Set up the default datatype
schema = {
Expand Down
68 changes: 47 additions & 21 deletions test/test_jsonschema_draft3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def multiple_of
include ArrayValidation::AdditionalItemsTests
include ArrayValidation::UniqueItemsTests

include EnumValidation::General
include EnumValidation::V3_V4

include NumberValidation::MinMaxTests
include NumberValidation::MultipleOfTests

Expand Down Expand Up @@ -115,39 +118,62 @@ def test_strict_properties_required_props
assert(JSON::Validator.validate(schema,data,:strict => true))
end

def test_enum
# Set up the default datatype
def test_strict_properties_additional_props
schema = {
"$schema" => "http://json-schema.org/draft-03/schema#",
"properties" => {
"a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}]}
}
"a" => {"type" => "string"},
"b" => {"type" => "string"}
},
"additionalProperties" => {"type" => "integer"}
}

data = {
"a" => nil
data = {"a" => "a"}
assert(!JSON::Validator.validate(schema,data,:strict => true))

data = {"b" => "b"}
assert(!JSON::Validator.validate(schema,data,:strict => true))

data = {"a" => "a", "b" => "b"}
assert(JSON::Validator.validate(schema,data,:strict => true))

data = {"a" => "a", "b" => "b", "c" => "c"}
assert(!JSON::Validator.validate(schema,data,:strict => true))

data = {"a" => "a", "b" => "b", "c" => 3}
assert(JSON::Validator.validate(schema,data,:strict => true))
end

def test_strict_properties_pattern_props
schema = {
"$schema" => "http://json-schema.org/draft-03/schema#",
"properties" => {
"a" => {"type" => "string"},
"b" => {"type" => "string"}
},
"patternProperties" => {"\\d+ taco" => {"type" => "integer"}}
}

# Make sure all of the above are valid...
data["a"] = 1
assert_valid schema, data
data = {"a" => "a"}
assert(!JSON::Validator.validate(schema,data,:strict => true))

data["a"] = 'boo'
assert_valid schema, data
data = {"b" => "b"}
assert(!JSON::Validator.validate(schema,data,:strict => true))

data["a"] = [1,2,3]
assert_valid schema, data
data = {"a" => "a", "b" => "b"}
assert(JSON::Validator.validate(schema,data,:strict => true))

data["a"] = {"a" => "b"}
assert_valid schema, data
data = {"a" => "a", "b" => "b", "c" => "c"}
assert(!JSON::Validator.validate(schema,data,:strict => true))

# Test something that doesn't exist
data["a"] = 'taco'
refute_valid schema, data
data = {"a" => "a", "b" => "b", "c" => 3}
assert(!JSON::Validator.validate(schema,data,:strict => true))

# Try it without the key
data = {}
assert_valid schema, data
data = {"a" => "a", "b" => "b", "23 taco" => 3}
assert(JSON::Validator.validate(schema,data,:strict => true))

data = {"a" => "a", "b" => "b", "23 taco" => "cheese"}
assert(!JSON::Validator.validate(schema,data,:strict => true))
end

def test_disallow
Expand Down
Loading

0 comments on commit c32a6b2

Please sign in to comment.