-
-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from RST-J/number_enum_issue
Issue with Fixnum and Float in enum
- Loading branch information
Showing
9 changed files
with
291 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.