diff --git a/.travis.yml b/.travis.yml index dd848724..ed24bc09 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,8 @@ rvm: - "2.1" - "jruby" - "rbx" +install: + - bundle install --retry=3 matrix: include: - rvm: "2.1" diff --git a/Gemfile b/Gemfile index 4341e6a1..5e2797e3 100644 --- a/Gemfile +++ b/Gemfile @@ -2,8 +2,9 @@ source "https://rubygems.org" gemspec -gem "json", :platform => :ruby_18 +gem "json", :platforms => :mri_18 group :development do gem "rake" + gem "minitest", '~> 5.0' end diff --git a/lib/json-schema/attributes/additionalproperties.rb b/lib/json-schema/attributes/additionalproperties.rb index 4cf96c12..8278f6b7 100644 --- a/lib/json-schema/attributes/additionalproperties.rb +++ b/lib/json-schema/attributes/additionalproperties.rb @@ -33,34 +33,33 @@ def self.validate(current_schema, data, fragments, processor, validator, options end def self.remove_valid_properties(extra_properties, current_schema, validator) + if current_schema.schema['properties'] + extra_properties = extra_properties - current_schema.schema['properties'].keys + end - if current_schema.schema['properties'] - extra_properties = extra_properties - current_schema.schema['properties'].keys - end - - if current_schema.schema['patternProperties'] - current_schema.schema['patternProperties'].each_key do |key| - r = Regexp.new(key) - extras_clone = extra_properties.clone - extras_clone.each do |prop| - if r.match(prop) - extra_properties = extra_properties - [prop] - end + if current_schema.schema['patternProperties'] + current_schema.schema['patternProperties'].each_key do |key| + r = Regexp.new(key) + extras_clone = extra_properties.clone + extras_clone.each do |prop| + if r.match(prop) + extra_properties = extra_properties - [prop] end end end + end - if schemas= current_schema.schema['extends'] - schemas = [schemas] if !schemas.is_a?(Array) - schemas.each do |schema_value| - temp_uri,extended_schema= JSON::Schema::ExtendsAttribute.get_extended_uri_and_schema(schema_value, current_schema, validator) - if extended_schema - extra_properties= remove_valid_properties(extra_properties, extended_schema, validator) - end + if schemas = current_schema.schema['extends'] + schemas = [schemas] if !schemas.is_a?(Array) + schemas.each do |schema_value| + _, extended_schema = JSON::Schema::ExtendsAttribute.get_extended_uri_and_schema(schema_value, current_schema, validator) + if extended_schema + extra_properties = remove_valid_properties(extra_properties, extended_schema, validator) end end + end - extra_properties + extra_properties end end diff --git a/lib/json-schema/attributes/type.rb b/lib/json-schema/attributes/type.rb index 786dcf06..815a69b7 100644 --- a/lib/json-schema/attributes/type.rb +++ b/lib/json-schema/attributes/type.rb @@ -74,11 +74,9 @@ def self.validate(current_schema, data, fragments, processor, validator, options # Lookup Schema type of given class instance def self.type_of_data(data) - type, klass = TYPE_CLASS_MAPPINGS.map { |k,v| [k,v] }.sort_by { |i| - k,v = i + type, _ = TYPE_CLASS_MAPPINGS.map { |k,v| [k,v] }.sort_by { |(_, v)| -Array(v).map { |klass| klass.ancestors.size }.max - }.find { |i| - k,v = i + }.find { |(_, v)| Array(v).any? { |klass| data.kind_of?(klass) } } type diff --git a/test/support/array_validation.rb b/test/support/array_validation.rb new file mode 100644 index 00000000..3d6877f9 --- /dev/null +++ b/test/support/array_validation.rb @@ -0,0 +1,108 @@ +module ArrayValidation + module ItemsTests + def test_items_single_schema + schema = { 'items' => { 'type' => 'string' } } + + assert_valid schema, [] + assert_valid schema, ['a'] + assert_valid schema, ['a', 'b'] + + refute_valid schema, [1] + refute_valid schema, ['a', 1] + + # other types are disregarded + assert_valid schema, {'a' => 'foo'} + end + + def test_items_multiple_schemas + schema = { + 'items' => [ + { 'type' => 'string' }, + { 'type' => 'integer' } + ] + } + + assert_valid schema, ['b', 1] + assert_valid schema, ['b', 1, nil] + refute_valid schema, [1, 'b'] + refute_valid schema, [] + end + + def test_minitems + schema = { 'minItems' => 1 } + + assert_valid schema, [1] + assert_valid schema, [1, 2] + refute_valid schema, [] + + # other types are disregarded + assert_valid schema, 5 + end + + def test_maxitems + schema = { 'maxItems' => 1 } + + assert_valid schema, [] + assert_valid schema, [1] + refute_valid schema, [1, 2] + + # other types are disregarded + assert_valid schema, 5 + end + end + + module AdditionalItemsTests + def test_additional_items_false + schema = { + 'items' => [ + { 'type' => 'integer' }, + { 'type' => 'string' } + ], + 'additionalItems' => false + } + + assert_valid schema, [1, 'string'] + refute_valid schema, [1, 'string', 2] + refute_valid schema, ['string', 1] + end + + def test_additional_items_schema + schema = { + 'items' => [ + { 'type' => 'integer' }, + { 'type' => 'string' } + ], + 'additionalItems' => { 'type' => 'integer' } + } + + assert_valid schema, [1, 'string'] + assert_valid schema, [1, 'string', 2] + refute_valid schema, [1, 'string', 'string'] + end + end + + module UniqueItemsTests + def test_unique_items + schema = { 'uniqueItems' => true } + + assert_valid schema, [nil, 5] + refute_valid schema, [nil, nil] + + assert_valid schema, [true, false] + refute_valid schema, [true, true] + + assert_valid schema, [4, 4.1] + refute_valid schema, [4, 4] + + assert_valid schema, ['a', 'ab'] + refute_valid schema, ['a', 'a'] + + assert_valid schema, [[1], [2]] + refute_valid schema, [[1], [1]] + + assert_valid schema, [{'b' => 1}, {'c' => 2}] + assert_valid schema, [{'b' => 1}, {'c' => 1}] + refute_valid schema, [{'b' => 1}, {'b' => 1}] + end + end +end diff --git a/test/support/number_validation.rb b/test/support/number_validation.rb new file mode 100644 index 00000000..c4d767e3 --- /dev/null +++ b/test/support/number_validation.rb @@ -0,0 +1,93 @@ +module NumberValidation + module MinMaxTests + def test_minimum + schema = { + 'properties' => { + 'a' => { 'minimum' => 5 } + } + } + + assert_valid schema, {'a' => 5} + assert_valid schema, {'a' => 6} + + refute_valid schema, {'a' => 4} + refute_valid schema, {'a' => 4.99999} + + # other types are disregarded + assert_valid schema, {'a' => 'str'} + end + + def test_exclusive_minimum + schema = { + 'properties' => { + 'a' => { 'minimum' => 5 }.merge(exclusive_minimum) + } + } + + assert_valid schema, {'a' => 6} + assert_valid schema, {'a' => 5.0001} + refute_valid schema, {'a' => 5} + end + + def test_maximum + schema = { + 'properties' => { + 'a' => { 'maximum' => 5 } + } + } + + assert_valid schema, {'a' => 4} + assert_valid schema, {'a' => 5} + + refute_valid schema, {'a' => 6} + refute_valid schema, {'a' => 5.0001} + end + + def test_exclusive_maximum + schema = { + 'properties' => { + 'a' => { 'maximum' => 5 }.merge(exclusive_maximum) + } + } + + assert_valid schema, {'a' => 4} + assert_valid schema, {'a' => 4.99999} + refute_valid schema, {'a' => 5} + end + end + + # draft3 introduced `divisibleBy`, renamed to `multipleOf` in draft4. + # Favor the newer name, but the behavior should be identical. + module MultipleOfTests + def multiple_of + 'multipleOf' + end + + def test_multiple_of + schema = { + 'properties' => { + 'a' => { multiple_of => 1.1 } + } + } + + assert_valid schema, {'a' => 0} + + assert_valid schema, {'a' => 2.2} + refute_valid schema, {'a' => 3.4} + + # other types are disregarded + assert_valid schema, {'a' => 'hi'} + end + + def test_multiple_of_zero + schema = { + 'properties' => { + 'a' => { multiple_of => 0 } + } + } + + refute_valid schema, {'a' => 5} + refute_valid schema, {'a' => 0} + end + end +end diff --git a/test/support/object_validation.rb b/test/support/object_validation.rb new file mode 100644 index 00000000..c5e15033 --- /dev/null +++ b/test/support/object_validation.rb @@ -0,0 +1,68 @@ +module ObjectValidation + module AdditionalPropertiesTests + def test_additional_properties_false + schema = { + 'properties' => { + 'a' => { 'type' => 'integer' } + }, + 'additionalProperties' => false + } + + assert_valid schema, {'a' => 1} + refute_valid schema, {'a' => 1, 'b' => 2} + end + + def test_additional_properties_schema + schema = { + 'properties' => { + 'a' => { 'type' => 'integer' } + }, + 'additionalProperties' => { 'type' => 'string' } + } + + assert_valid schema, {'a' => 1} + assert_valid schema, {'a' => 1, 'b' => 'hi'} + refute_valid schema, {'a' => 1, 'b' => 2} + end + end + + module PatternPropertiesTests + def test_pattern_properties + schema = { + 'patternProperties' => { + "\\d+ taco" => { 'type' => 'integer' } + } + } + + assert_valid schema, {'1 taco' => 1, '20 taco' => 20} + assert_valid schema, {'foo' => true, '1 taco' => 1} + refute_valid schema, {'1 taco' => 'yum'} + end + + def test_pattern_properties_additional_properties_false + schema = { + 'patternProperties' => { + "\\d+ taco" => { 'type' => 'integer' } + }, + 'additionalProperties' => false + } + + assert_valid schema, {'1 taco' => 1} + refute_valid schema, {'1 taco' => 'yum'} + refute_valid schema, {'1 taco' => 1, 'foo' => true} + end + + def test_pattern_properties_additional_properties_schema + schema = { + 'patternProperties' => { + "\\d+ taco" => { 'type' => 'integer' } + }, + 'additionalProperties' => { 'type' => 'string' } + } + + assert_valid schema, {'1 taco' => 1} + assert_valid schema, {'1 taco' => 1, 'foo' => 'bar'} + refute_valid schema, {'1 taco' => 1, 'foo' => 2} + end + end +end diff --git a/test/support/string_validation.rb b/test/support/string_validation.rb new file mode 100644 index 00000000..b8d911cb --- /dev/null +++ b/test/support/string_validation.rb @@ -0,0 +1,154 @@ +module StringValidation + module ValueTests + def test_minlength + schema = { + 'properties' => { + 'a' => { 'minLength' => 1 } + } + } + + assert_valid schema, {'a' => 't'} + refute_valid schema, {'a' => ''} + + # other types are disregarded + assert_valid schema, {'a' => 5} + end + + def test_maxlength + schema = { + 'properties' => { + 'a' => { 'maxLength' => 2 } + } + } + + assert_valid schema, {'a' => 'tt'} + assert_valid schema, {'a' => ''} + refute_valid schema, {'a' => 'ttt'} + + # other types are disregarded + assert_valid schema, {'a' => 5} + end + + def test_pattern + schema = { + 'properties' => { + 'a' => { 'pattern' => "\\d+ taco" } + } + } + + assert_valid schema, {'a' => '156 taco bell'} + refute_valid schema, {'a' => 'x taco'} + + # other types are disregarded + assert_valid schema, {'a' => 5} + end + end + + module FormatTests + # Draft1..3 use the format name `ip-address`; draft4 changed it to `ipv4`. + def ipv4_format + 'ip-address' + end + + def test_format_unknown + schema = { + 'properties' => { + 'a' => { 'format' => 'unknown' } + } + } + + assert_valid schema, {'a' => 'absolutely anything!'} + assert_valid schema, {'a' => ''} + end + + def test_format_union + schema = { + 'properties' => { + 'a' => { + 'type' => ['string', 'null'], + 'format' => 'date-time' + } + } + } + + assert_valid schema, {'a' => nil} + refute_valid schema, {'a' => 'wrong'} + end + + def test_format_ipv4 + schema = { + 'properties' => { + 'a' => { 'format' => ipv4_format } + } + } + + assert_valid schema, {"a" => "1.1.1.1"} + refute_valid schema, {"a" => "1.1.1"} + refute_valid schema, {"a" => "1.1.1.300"} + refute_valid schema, {"a" => "1.1.1"} + refute_valid schema, {"a" => "1.1.1.1b"} + + # other types are disregarded + assert_valid schema, {'a' => 5} + end + + def test_format_ipv6 + schema = { + 'properties' => { + 'a' => { 'format' => 'ipv6' } + } + } + + assert_valid schema, {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff"} + assert_valid schema, {"a" => "1111:0:8888:0:0:0:eeee:ffff"} + assert_valid schema, {"a" => "1111:2222:8888::eeee:ffff"} + assert_valid schema, {"a" => "::1"} + + refute_valid schema, {"a" => "1111:2222:8888:99999:aaaa:cccc:eeee:ffff"} + refute_valid schema, {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:gggg"} + refute_valid schema, {"a" => "1111:2222::9999::cccc:eeee:ffff"} + refute_valid schema, {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"} + refute_valid schema, {"a" => "42"} + refute_valid schema, {"a" => "b"} + end + end + + # Draft1..3 explicitly support `date`, `time` formats in addition to + # the `date-time` format. + module DateAndTimeFormatTests + def test_format_time + schema = { + 'properties' => { + 'a' => { 'format' => 'time' } + } + } + + assert_valid schema, {"a" => "12:00:00"} + refute_valid schema, {"a" => "12:00"} + refute_valid schema, {"a" => "12:00:60"} + refute_valid schema, {"a" => "12:60:00"} + refute_valid schema, {"a" => "24:00:00"} + refute_valid schema, {"a" => "0:00:00"} + refute_valid schema, {"a" => "-12:00:00"} + refute_valid schema, {"a" => "12:00:00b"} + assert_valid schema, {"a" => "12:00:00"} + refute_valid schema, {"a" => "12:00:00\nabc"} + end + + def test_format_date + schema = { + 'properties' => { + 'a' => { 'format' => 'date' } + } + } + + assert_valid schema, {"a" => "2010-01-01"} + refute_valid schema, {"a" => "2010-01-32"} + refute_valid schema, {"a" => "n2010-01-01"} + refute_valid schema, {"a" => "2010-1-01"} + refute_valid schema, {"a" => "2010-01-1"} + refute_valid schema, {"a" => "2010-01-01n"} + refute_valid schema, {"a" => "2010-01-01\nabc"} + end + end +end diff --git a/test/support/type_validation.rb b/test/support/type_validation.rb new file mode 100644 index 00000000..20965b7d --- /dev/null +++ b/test/support/type_validation.rb @@ -0,0 +1,81 @@ +module TypeValidation + # The draft4 schema refers to the JSON types as 'simple types'; + # see draft4#/definitions/simpleTypes + module SimpleTypeTests + TYPES = { + 'integer' => 5, + 'number' => 5.0, + 'string' => 'str', + 'boolean' => true, + 'object' => {}, + 'array' => [], + 'null' => nil + } + + TYPES.each do |name, value| + other_values = TYPES.values.reject { |v| v == value } + + define_method(:"test_#{name}_type_property") do + schema = { + 'properties' => { 'a' => { 'type' => name } } + } + assert_valid schema, {'a' => value} + + other_values.each do |other_value| + refute_valid schema, {'a' => other_value} + end + end + + define_method(:"test_#{name}_type_value") do + schema = { 'type' => name } + assert_valid schema, value + + other_values.each do |other_value| + schema = { 'type' => name } + refute_valid schema, other_value + end + end + end + + def test_type_union + schema = { 'type' => ['integer', 'string'] } + assert_valid schema, 5 + assert_valid schema, 'str' + refute_valid schema, nil + refute_valid schema, [5, 'str'] + end + end + + # The draft1..3 schemas support an additional type, `any`. + module AnyTypeTests + def test_any_type + schema = { 'type' => 'any' } + + SimpleTypeTests::TYPES.values.each do |value| + assert_valid schema, value + end + end + end + + # The draft1..3 schemas support schemas as values for `type`. + module SchemaUnionTypeTests + def test_union_type_with_schemas + schema = { + 'properties' => { + 'a' => { + 'type' => [ + {'type' => 'string'}, + {'type' => 'object', 'properties' => { 'b' => { 'type' => 'integer' }}} + ] + } + } + } + + assert_valid schema, {'a' => 'test'} + refute_valid schema, {'a' => 5} + + assert_valid schema, {'a' => {'b' => 5}} + refute_valid schema, {'a' => {'b' => 'taco'}} + end + end +end diff --git a/test/test_all_of_ref_schema.rb b/test/test_all_of_ref_schema.rb index 9d2dd1d9..8ab6bf0d 100644 --- a/test/test_all_of_ref_schema.rb +++ b/test/test_all_of_ref_schema.rb @@ -1,24 +1,23 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) + +class AllOfRefSchemaTest < Minitest::Test + def schema + schema_fixture_path('all_of_ref_schema.json') + end + + def data + data_fixture_path('all_of_ref_data.json') + end -class AllOfRefSchemaTest < Test::Unit::TestCase def test_all_of_ref_schema_fails - schema = File.join(File.dirname(__FILE__),"schemas/all_of_ref_schema.json") - data = File.join(File.dirname(__FILE__),"data/all_of_ref_data.json") - errors = JSON::Validator.fully_validate(schema,data, :errors_as_objects => true) - assert(!errors.empty?, "should have failed to validate") + refute_valid schema, data end def test_all_of_ref_schema_succeeds - schema = File.join(File.dirname(__FILE__),"schemas/all_of_ref_schema.json") - data = %({"name": 42}) - errors = JSON::Validator.fully_validate(schema,data, :errors_as_objects => true) - assert(errors.empty?, "should have validated") + assert_valid schema, %({"name": 42}) end def test_all_of_ref_subschema_errors - schema = File.join(File.dirname(__FILE__), 'schemas/all_of_ref_schema.json') - data = File.join(File.dirname(__FILE__), 'data/all_of_ref_data.json') errors = JSON::Validator.fully_validate(schema, data, :errors_as_objects => true) nested_errors = errors[0][:errors] assert_equal([:allof_0], nested_errors.keys, 'should have nested errors for each allOf subschema') @@ -26,8 +25,6 @@ def test_all_of_ref_subschema_errors end def test_all_of_ref_message - schema = File.join(File.dirname(__FILE__), 'schemas/all_of_ref_schema.json') - data = File.join(File.dirname(__FILE__), 'data/all_of_ref_data.json') errors = JSON::Validator.fully_validate(schema, data) expected_message = """The property '#/' of type Hash did not match all of the required schemas. The schema specific errors were: diff --git a/test/test_any_of_ref_schema.rb b/test/test_any_of_ref_schema.rb index 8205083c..475222de 100644 --- a/test/test_any_of_ref_schema.rb +++ b/test/test_any_of_ref_schema.rb @@ -1,16 +1,15 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) + +class AnyOfRefSchemaTest < Minitest::Test + def schema + schema_fixture_path('any_of_ref_schema.json') + end -class AnyOfRefSchemaTest < Test::Unit::TestCase def test_any_of_ref_schema - schema = File.join(File.dirname(__FILE__),"schemas/any_of_ref_schema.json") - data = File.join(File.dirname(__FILE__),"data/any_of_ref_data.json") - errors = JSON::Validator.fully_validate(schema,data, :errors_as_objects => true) - assert(errors.empty?, errors.map{|e| e[:message] }.join("\n")) + assert_valid schema, data_fixture_path('any_of_ref_data.json') end def test_any_of_ref_subschema_errors - schema = File.join(File.dirname(__FILE__),'schemas/any_of_ref_schema.json') data = %({"names": ["jack"]}) errors = JSON::Validator.fully_validate(schema, data, :errors_as_objects => true) nested_errors = errors[0][:errors] @@ -21,7 +20,6 @@ def test_any_of_ref_subschema_errors end def test_any_of_ref_message - schema = File.join(File.dirname(__FILE__),'schemas/any_of_ref_schema.json') data = %({"names": ["jack"]}) errors = JSON::Validator.fully_validate(schema, data) expected_message = """The property '#/names/0' of type String did not match one or more of the required schemas. The schema specific errors were: diff --git a/test/test_bad_schema_ref.rb b/test/test_bad_schema_ref.rb index cffe7527..a8b07d00 100644 --- a/test/test_bad_schema_ref.rb +++ b/test/test_bad_schema_ref.rb @@ -1,10 +1,9 @@ -require 'test/unit' +require File.expand_path('../test_helper', __FILE__) require 'webmock' require 'socket' -require File.dirname(__FILE__) + '/../lib/json-schema' -class BadSchemaRefTest < Test::Unit::TestCase +class BadSchemaRefTest < Minitest::Test def setup WebMock.allow_net_connect! end @@ -15,27 +14,26 @@ def teardown def test_bad_uri_ref schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "type" => "array", - "items" => { "$ref" => "../google.json"} + "$schema" => "http://json-schema.org/draft-04/schema#", + "type" => "array", + "items" => { "$ref" => "../google.json"} } data = [1,2,3] - assert_raise(URI::BadURIError) do + assert_raises(URI::BadURIError) do JSON::Validator.validate(schema,data) end end def test_bad_host_ref schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "type" => "array", - "items" => { "$ref" => "http://ppcheesecheseunicornnuuuurrrrr.com/json.schema"} + "$schema" => "http://json-schema.org/draft-04/schema#", + "type" => "array", + "items" => { "$ref" => "http://ppcheesecheseunicornnuuuurrrrr.com/json.schema"} } data = [1,2,3] - - assert_raise(SocketError, OpenURI::HTTPError) do + assert_raises(SocketError, OpenURI::HTTPError) do JSON::Validator.validate(schema,data) end end diff --git a/test/test_common_test_suite.rb b/test/test_common_test_suite.rb index 4262fc65..e57b30ab 100644 --- a/test/test_common_test_suite.rb +++ b/test/test_common_test_suite.rb @@ -1,8 +1,7 @@ -require 'test/unit' +require File.expand_path('../test_helper', __FILE__) require 'webmock' -require File.expand_path('../../lib/json-schema', __FILE__) -class CommonTestSuiteTest < Test::Unit::TestCase +class CommonTestSuiteTest < Minitest::Test TEST_DIR = File.expand_path('../test-suite/tests', __FILE__) # These are test files which we know fail spectacularly, either because we @@ -11,12 +10,19 @@ class CommonTestSuiteTest < Test::Unit::TestCase # you can replace `:all` with an array containing the names of individual # tests to skip. IGNORED_TESTS = Hash.new { |h,k| h[k] = [] }.merge({ - "draft3/optional/format.json" => :all, - "draft3/optional/jsregex.json" => [ - "ECMA 262 regex dialect recognition/ECMA 262 has no support for lookbehind", - "ECMA 262 regex dialect recognition/[^] is a valid regex", + "draft3/optional/jsregex.json" => :all, + "draft3/optional/format.json" => [ + "validation of regular expressions", + "validation of e-mail addresses", + "validation of URIs", + "validation of host names", + "validation of CSS colors" ], - "draft4/optional/format.json" => :all + "draft4/optional/format.json" => [ + "validation of URIs", + "validation of e-mail addresses", + "validation of host names" + ] }) include WebMock::API @@ -33,6 +39,7 @@ def setup def teardown WebMock.disable! + WebMock.reset! end Dir["#{TEST_DIR}/*"].each do |suite| @@ -47,21 +54,19 @@ def teardown v = nil test["tests"].each do |t| - err_id = "#{rel_file}: #{base_description}/#{t['description']}" - - unless IGNORED_TESTS[rel_file] == :all or - IGNORED_TESTS[rel_file].include? "#{base_description}/#{t['description']}" - define_method("test_#{err_id}") do - assert_nothing_raised("Exception raised running #{err_id}") do - v = JSON::Validator.fully_validate(schema, - t["data"], - :validate_schema => true, - :version => version - ) - end + next if IGNORED_TESTS[rel_file] == :all + next if IGNORED_TESTS[rel_file].any? { |test| + base_description == test || "#{base_description}/#{t['description']}" == test + } - assert_equal t["valid"], v.empty?, "Common test suite case failed: #{err_id}\n#{v}" - end + err_id = "#{rel_file}: #{base_description}/#{t['description']}" + define_method("test_#{err_id}") do + errors = JSON::Validator.fully_validate(schema, + t["data"], + :validate_schema => true, + :version => version + ) + assert_equal t["valid"], errors.empty?, "Common test suite case failed: #{err_id}\n#{v}" end end end diff --git a/test/test_custom_format.rb b/test/test_custom_format.rb index f01a89c5..e7ab3cc6 100644 --- a/test/test_custom_format.rb +++ b/test/test_custom_format.rb @@ -1,8 +1,7 @@ # encoding: utf-8 -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) -class JSONSchemaCustomFormatTest < Test::Unit::TestCase +class JSONSchemaCustomFormatTest < Minitest::Test def setup @all_versions = ['draft1', 'draft2', 'draft3', 'draft4'] @format_proc = lambda { |value| raise JSON::Schema::CustomFormatError.new("must be 42") unless value == "42" } diff --git a/test/test_definition.rb b/test/test_definition.rb index 1c9941a3..42541204 100644 --- a/test/test_definition.rb +++ b/test/test_definition.rb @@ -1,23 +1,15 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) -class RelativeDefinitionTest < Test::Unit::TestCase +class RelativeDefinitionTest < Minitest::Test def test_definition_schema - schema = File.join(File.dirname(__FILE__),"schemas/definition_schema.json") - data = {"a" => 5} - errors = JSON::Validator.fully_validate(schema,data, :errors_as_objects => true) - assert(errors.empty?, errors.map{|e| e[:message] }.join("\n")) + assert_valid schema_fixture_path('definition_schema.json'), {"a" => 5} end def test_relative_definition - schema = File.join(File.dirname(__FILE__),"schemas/relative_definition_schema.json") - - data = {"a" => "foo"} - assert(!JSON::Validator.validate(schema,data)) - - data = {"a" => 5} - assert(JSON::Validator.validate(schema,data)) + schema = schema_fixture_path('relative_definition_schema.json') + assert_valid schema, {"a" => 5} + refute_valid schema, {"a" => "foo"} end end diff --git a/test/test_extended_schema.rb b/test/test_extended_schema.rb index cb57db2f..9b8e4c15 100644 --- a/test/test_extended_schema.rb +++ b/test/test_extended_schema.rb @@ -1,11 +1,12 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) class BitwiseAndAttribute < JSON::Schema::Attribute def self.validate(current_schema, data, fragments, processor, 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, self, current_schema) + return unless data.is_a?(Integer) + + if 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-and']}" + validation_error(processor, message, fragments, current_schema, self, options[:record_errors]) end end end @@ -17,14 +18,14 @@ def initialize @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 +class TestExtendedSchema < Minitest::Test + def test_extended_schema_validation schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", + "$schema" => "http://test.com/test.json", "properties" => { "a" => { "bitwise-and" => 1 @@ -34,16 +35,16 @@ def test_schema_from_file } } } - - 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)) - + + assert_valid schema, {"a" => 1, "b" => "taco"} + refute_valid schema, {"a" => 0, "b" => "taco"} + refute_valid schema, {"a" => 1, "b" => 5} + end + + def test_unextended_schema + # Verify that using the original schema disregards the `bitwise-and` property schema = { - "$schema" => "http://test.com/test.json", + "$schema" => "http://json-schema.org/draft-03/schema#", "properties" => { "a" => { "bitwise-and" => 1 @@ -53,16 +54,9 @@ def test_schema_from_file } } } - - 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)) + + assert_valid schema, {"a" => 0, "b" => "taco"} + assert_valid schema, {"a" => 1, "b" => "taco"} + refute_valid schema, {"a" => 1, "b" => 5} end -end \ No newline at end of file +end diff --git a/test/test_extends_and_additionalProperties.rb b/test/test_extends_and_additionalProperties.rb index c139a738..9f6b40db 100644 --- a/test/test_extends_and_additionalProperties.rb +++ b/test/test_extends_and_additionalProperties.rb @@ -1,47 +1,49 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) -class ExtendsNestedTest < Test::Unit::TestCase +class ExtendsNestedTest < Minitest::Test - def assert_validity(valid, schema_name, data, msg=nil) - file = File.expand_path("../schemas/#{schema_name}.schema.json",__FILE__) - errors = JSON::Validator.fully_validate file, data - msg.sub! /\.$/, '' if msg - send (valid ? :assert_equal : :assert_not_equal), [], errors, \ - "Schema should be #{valid ? :valid : :invalid}#{msg ? ".\n[#{schema_name}] #{msg}" : ''}" - end + def assert_validity(valid, schema_name, data, msg) + msg = "Schema should be #{valid ? :valid : :invalid}.\n(#{schema_name}) #{msg}" + schema = schema_fixture_path("#{schema_name}.schema.json") + errors = JSON::Validator.fully_validate(schema, data) - def assert_valid(schema_name, data, msg=nil) assert_validity true, schema_name, data, msg end - def refute_valid(schema_name, data, msg=nil) assert_validity false, schema_name, data, msg end + if valid + assert_equal([], errors, msg) + else + refute_equal([], errors, msg) + end + end %w[ - extends_and_additionalProperties-1-filename extends_and_additionalProperties-1-ref - extends_and_additionalProperties-2-filename extends_and_additionalProperties-2-ref + extends_and_additionalProperties-1-filename + extends_and_additionalProperties-1-ref + extends_and_additionalProperties-2-filename + extends_and_additionalProperties-2-ref ].each do |schema_name| - test_prefix= 'test_' + schema_name.gsub('-','_') - class_eval <<-EOB + test_prefix = 'test_' + schema_name.gsub('-','_') + class_eval <<-EOB def #{test_prefix}_valid_outer - assert_valid '#{schema_name}', {"outerC"=>true}, "Outer defn is broken, maybe the outer extends overrode it?" + assert_validity true, '#{schema_name}', {"outerC"=>true}, "Outer defn is broken, maybe the outer extends overrode it" end def #{test_prefix}_valid_outer_extended - assert_valid '#{schema_name}', {"innerA"=>true}, "Extends at the root level isn't working." + assert_validity true, '#{schema_name}', {"innerA"=>true}, "Extends at the root level isn't working" end def #{test_prefix}_valid_inner - assert_valid '#{schema_name}', {"outerB"=>[{"innerA"=>true}]}, "Extends isn't working in the array element defn." + assert_validity true, '#{schema_name}', {"outerB"=>[{"innerA"=>true}]}, "Extends isn't working in the array element defn" end def #{test_prefix}_invalid_inner - refute_valid '#{schema_name}', {"outerB"=>[{"whaaaaat"=>true}]}, "Array element defn allowing anything when it should only allow what's in inner.schema" + assert_validity false, '#{schema_name}', {"outerB"=>[{"whaaaaat"=>true}]}, "Array element defn allowing anything when it should only allow what's in inner.schema" end EOB if schema_name['extends_and_additionalProperties-1'] class_eval <<-EOB def #{test_prefix}_invalid_outer - refute_valid '#{schema_name}', {"whaaaaat"=>true}, "Outer defn allowing anything when it shouldn't." + assert_validity false, '#{schema_name}', {"whaaaaat"=>true}, "Outer defn allowing anything when it shouldn't" end EOB end diff --git a/test/test_file_uri.rb b/test/test_file_uri.rb index e45deca4..ca92702d 100644 --- a/test/test_file_uri.rb +++ b/test/test_file_uri.rb @@ -1,9 +1,8 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) -class FileUriTest < Test::Unit::TestCase +class FileUriTest < Minitest::Test def test_file_uri_has_correct_components - path = File.absolute_path(File.dirname(__FILE__) + '/data/all_of_ref_data.json') + path = data_fixture_path('all_of_ref_data.json') uri = URI.parse("file://#{path}#name") assert_equal('file', uri.scheme) assert_equal('', uri.host) @@ -13,13 +12,13 @@ def test_file_uri_has_correct_components end def test_file_uri_loads_real_file - path = File.absolute_path(File.dirname(__FILE__) + '/data/all_of_ref_data.json') + path = data_fixture_path('all_of_ref_data.json') assert_equal(%Q({\n "name" : "john"\n}\n), open("file://#{path}").read) end def test_file_uri_fails_for_invalid_file path = File.absolute_path(File.dirname(__FILE__) + '/this_should_not_exist') - assert_raise(Errno::ENOENT) do + assert_raises(Errno::ENOENT) do open("file://#{path}").read end end diff --git a/test/test_files_v3.rb b/test/test_files_v3.rb index 547a3dd4..d85d970c 100644 --- a/test/test_files_v3.rb +++ b/test/test_files_v3.rb @@ -1,52 +1,43 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) -class JSONSchemaTest < Test::Unit::TestCase +class JSONSchemaTest < Minitest::Test # # These tests are ONLY run if there is an appropriate JSON backend parser available # def test_schema_from_file - data = {"a" => 5} - assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data)) - data = {"a" => "bad"} - assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data)) + assert_valid schema_fixture_path('good_schema_1.json'), { "a" => 5 } + refute_valid schema_fixture_path('good_schema_1.json'), { "a" => "bad" } end def test_data_from_file schema = {"$schema" => "http://json-schema.org/draft-03/schema#","type" => "object", "properties" => {"a" => {"type" => "integer"}}} - assert(JSON::Validator.validate_uri(schema,File.join(File.dirname(__FILE__),"data/good_data_1.json"))) - assert(!JSON::Validator.validate_uri(schema,File.join(File.dirname(__FILE__),"data/bad_data_1.json"))) + assert_valid schema, data_fixture_path('good_data_1.json'), :uri => true + refute_valid schema, data_fixture_path('bad_data_1.json'), :uri => true end def test_data_from_json if JSON::Validator.json_backend != nil schema = {"$schema" => "http://json-schema.org/draft-03/schema#","type" => "object", "properties" => {"a" => {"type" => "integer"}}} - assert(JSON::Validator.validate_json(schema, %Q({"a" : 5}))) - assert(!JSON::Validator.validate_json(schema, %Q({"a" : "poop"}))) + assert_valid schema, %Q({"a": 5}), :json => true + refute_valid schema, %Q({"a": "poop"}), :json => true end end def test_both_from_file - assert(JSON::Validator.validate_uri(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),File.join(File.dirname(__FILE__),"data/good_data_1.json"))) - assert(!JSON::Validator.validate_uri(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),File.join(File.dirname(__FILE__),"data/bad_data_1.json"))) + assert_valid schema_fixture_path('good_schema_1.json'), data_fixture_path('good_data_1.json'), :uri => true + refute_valid schema_fixture_path('good_schema_1.json'), data_fixture_path('bad_data_1.json'), :uri => true end def test_file_ref - data = {"b" => {"a" => 5}} - assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_2.json"),data)) - - data = {"b" => {"a" => "boo"}} - assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data)) + assert_valid schema_fixture_path('good_schema_2.json'), { "b" => { "a" => 5 } } + refute_valid schema_fixture_path('good_schema_1.json'), { "b" => { "a" => "boo" } } end def test_file_extends - data = {"a" => 5} - assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_extends1.json"),data)) - - data = {"a" => 5, "b" => {"a" => 5}} - assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_extends2.json"),data)) + assert_valid schema_fixture_path('good_schema_extends1.json'), { "a" => 5 } + assert_valid schema_fixture_path('good_schema_extends2.json'), { "a" => 5, "b" => { "a" => 5 } } end end diff --git a/test/test_fragment_resolution.rb b/test/test_fragment_resolution.rb index 11f45cad..95985c40 100644 --- a/test/test_fragment_resolution.rb +++ b/test/test_fragment_resolution.rb @@ -1,7 +1,6 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) -class FragmentResolution < Test::Unit::TestCase +class FragmentResolution < Minitest::Test def test_fragment_resolution schema = { "$schema" => "http://json-schema.org/draft-04/schema#", @@ -17,14 +16,14 @@ def test_fragment_resolution } data = {"b" => 5} - assert(!JSON::Validator.validate(schema,data)) - assert(JSON::Validator.validate(schema,data,:fragment => "#/properties/a")) + refute_valid schema, data + assert_valid schema, data, :fragment => "#/properties/a" - assert_raise JSON::Schema::SchemaError do + assert_raises JSON::Schema::SchemaError do JSON::Validator.validate!(schema,data,:fragment => "/properties/a") end - assert_raise JSON::Schema::SchemaError do + assert_raises JSON::Schema::SchemaError do JSON::Validator.validate!(schema,data,:fragment => "#/properties/b") end end diff --git a/test/test_fragment_validation_with_ref.rb b/test/test_fragment_validation_with_ref.rb index 3f17a955..459c541c 100644 --- a/test/test_fragment_validation_with_ref.rb +++ b/test/test_fragment_validation_with_ref.rb @@ -1,7 +1,6 @@ require File.expand_path('../test_helper', __FILE__) -require 'json-schema' -class FragmentValidationWithRef < Test::Unit::TestCase +class FragmentValidationWithRef < Minitest::Test def whole_schema { "$schema" => "http://json-schema.org/draft-04/schema#", @@ -30,11 +29,6 @@ def whole_schema def test_validation_of_fragment data = [{"content" => "ohai", "author" => "Bob"}] - v = nil - assert_nothing_raised do - v = JSON::Validator.fully_validate(whole_schema,data,:fragment => "#/definitions/posts") - end - - assert(v.empty?, v.join("\n")) + assert_valid whole_schema, data, :fragment => "#/definitions/posts" end end diff --git a/test/test_full_validation.rb b/test/test_full_validation.rb index e11fcebf..4fe470a5 100644 --- a/test/test_full_validation.rb +++ b/test/test_full_validation.rb @@ -1,7 +1,6 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) -class JSONFullValidation < Test::Unit::TestCase +class JSONFullValidation < Minitest::Test def test_full_validation data = {"b" => {"a" => 5}} diff --git a/test/test_helper.rb b/test/test_helper.rb index 1acfa6a0..962eeb38 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,2 +1,36 @@ -require 'test/unit' +require 'minitest/autorun' + $:.unshift(File.expand_path('../../lib', __FILE__)) +require 'json-schema' + +Dir[File.join(File.expand_path('../support', __FILE__), '*.rb')].each do |support_file| + require support_file +end + +class Minitest::Test + def schema_fixture_path(filename) + File.join(File.dirname(__FILE__), 'schemas', filename) + end + + def data_fixture_path(filename) + File.join(File.dirname(__FILE__), 'data', filename) + end + + def assert_valid(schema, data, options = {}) + if !options.key?(:version) && respond_to?(:schema_version) + options = options.merge(:version => schema_version) + end + + errors = JSON::Validator.fully_validate(schema, data, options) + assert_equal([], errors, "#{data.inspect} should be valid for schema:\n#{schema.inspect}") + end + + def refute_valid(schema, data, options = {}) + if !options.key?(:version) && respond_to?(:schema_version) + options = options.merge(:version => schema_version) + end + + errors = JSON::Validator.fully_validate(schema, data, options) + refute_equal([], errors, "#{data.inspect} should be invalid for schema:\n#{schema.inspect}") + end +end diff --git a/test/test_jsonschema_draft1.rb b/test/test_jsonschema_draft1.rb index b8c9c3a8..042e4b77 100644 --- a/test/test_jsonschema_draft1.rb +++ b/test/test_jsonschema_draft1.rb @@ -1,169 +1,31 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) -class JSONSchemaDraft1Test < Test::Unit::TestCase - def test_types - # Set up the default datatype - schema = { - "properties" => { - "a" => {} - } - } - data = { - "a" => nil - } - - # Test integers - schema["properties"]["a"]["type"] = "integer" - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - - # Test numbers - schema["properties"]["a"]["type"] = "number" - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = 5.2 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - - # Test strings - schema["properties"]["a"]["type"] = "string" - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - data['a'] = 'string' - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - - # Test booleans - schema["properties"]["a"]["type"] = "boolean" - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - data['a'] = true - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data['a'] = false - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - - # Test object - schema["properties"]["a"]["type"] = "object" - data["a"] = {} - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - - # Test array - schema["properties"]["a"]["type"] = "array" - data["a"] = [] - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - - # Test null - schema["properties"]["a"]["type"] = "null" - data["a"] = nil - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - - # Test any - schema["properties"]["a"]["type"] = "any" - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = 5.2 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data['a'] = 'string' - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data['a'] = true - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - - # Test a union type - schema["properties"]["a"]["type"] = ["integer","string"] - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = 'boo' - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = false - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) +class JSONSchemaDraft1Test < Minitest::Test + def schema_version + :draft1 + end - # Test a union type with schemas - schema["properties"]["a"]["type"] = [{ "type" => "string" }, {"type" => "object", "properties" => {"b" => {"type" => "integer"}}}] + def exclusive_minimum + { 'minimumCanEqual' => false } + end - data["a"] = "test" - assert(JSON::Validator.validate(schema,data,:version => :draft1)) + def exclusive_maximum + { 'maximumCanEqual' => false } + end - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) + include ArrayValidation::ItemsTests - data["a"] = {"b" => 5} - assert(JSON::Validator.validate(schema,data,:version => :draft1)) + include NumberValidation::MinMaxTests - data["a"] = {"b" => "taco"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - end + include ObjectValidation::AdditionalPropertiesTests + include StringValidation::ValueTests + include StringValidation::FormatTests + include StringValidation::DateAndTimeFormatTests + include TypeValidation::SimpleTypeTests + include TypeValidation::AnyTypeTests + include TypeValidation::SchemaUnionTypeTests def test_optional # Set up the default datatype @@ -174,9 +36,9 @@ def test_optional } data = {} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) + refute_valid schema, data data['a'] = "Hello" - assert(JSON::Validator.validate(schema,data,:version => :draft1)) + assert_valid schema, data schema = { "properties" => { @@ -185,237 +47,9 @@ def test_optional } data = {} - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - end - - - - def test_minimum - # Set up the default datatype - schema = { - "properties" => { - "a" => {"minimum" => 5} - } - } - - data = { - "a" => nil - } - - - # Test an integer - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = 4 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - # Test a float - data["a"] = 5.0 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = 4.9 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - # Test a non-number - data["a"] = "a string" - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - # Test exclusiveMinimum - schema["properties"]["a"]["minimumCanEqual"] = false - - data["a"] = 6 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - # Test with float - data["a"] = 5.00000001 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = 5.0 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - end - - - - def test_maximum - # Set up the default datatype - schema = { - "properties" => { - "a" => {"maximum" => 5} - } - } - - data = { - "a" => nil - } - - - # Test an integer - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = 6 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - # Test a float - data["a"] = 5.0 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = 5.1 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - # Test a non-number - data["a"] = "a string" - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - # Test exclusiveMinimum - schema["properties"]["a"]["maximumCanEqual"] = false - - data["a"] = 4 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - # Test with float - data["a"] = 4.9999999 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = 5.0 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - end - - - def test_min_items - # Set up the default datatype - schema = { - "properties" => { - "a" => {"minItems" => 1} - } - } - - data = { - "a" => nil - } - - # Test with an array - data["a"] = ["boo"] - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = [] - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - # Test with a non-array - data["a"] = "boo" - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - end - - - - def test_max_items - # Set up the default datatype - schema = { - "properties" => { - "a" => {"maxItems" => 1} - } - } - - data = { - "a" => nil - } - - # Test with an array - data["a"] = ["boo"] - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = ["boo","taco"] - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - # Test with a non-array - data["a"] = "boo" - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - end - - - def test_pattern - # Set up the default datatype - schema = { - "properties" => { - "a" => {"pattern" => "\\d+ taco"} - } - } - - data = { - "a" => nil - } - - # Test strings - data["a"] = "156 taco bell" - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - # Test a non-string - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = "taco" - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - end - - - def test_min_length - # Set up the default datatype - schema = { - "properties" => { - "a" => {"minLength" => 1} - } - } - - data = { - "a" => nil - } - - # Try out strings - data["a"] = "t" - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = "" - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - # Try out non-string - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - end - - - def test_max_length - # Set up the default datatype - schema = { - "properties" => { - "a" => {"maxLength" => 1} - } - } - - data = { - "a" => nil - } - - # Try out strings - data["a"] = "t" - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - - data["a"] = "tt" - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - # Try out non-string - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) + assert_valid schema, data end - def test_enum # Set up the default datatype schema = { @@ -430,24 +64,24 @@ def test_enum # Make sure all of the above are valid... data["a"] = 1 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) + assert_valid schema, data data["a"] = 'boo' - assert(JSON::Validator.validate(schema,data,:version => :draft1)) + assert_valid schema, data data["a"] = [1,2,3] - assert(JSON::Validator.validate(schema,data,:version => :draft1)) + assert_valid schema, data data["a"] = {"a" => "b"} - assert(JSON::Validator.validate(schema,data,:version => :draft1)) + assert_valid schema, data # Test something that doesn't exist data["a"] = 'taco' - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) + refute_valid schema, data # Try it without the key data = {} - assert(JSON::Validator.validate(schema,data,:version => :draft1)) + assert_valid schema, data end @@ -464,22 +98,22 @@ def test_max_decimal } data["a"] = 3.35 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) + assert_valid schema, data data["a"] = 3.455 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) + refute_valid schema, data schema["properties"]["a"]["maxDecimal"] = 0 data["a"] = 4.0 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) + refute_valid schema, data data["a"] = 'boo' - assert(JSON::Validator.validate(schema,data,:version => :draft1)) + assert_valid schema, data data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft1)) + assert_valid schema, data end @@ -498,225 +132,39 @@ def test_disallow data["a"] = 'string' - assert(JSON::Validator.validate(schema,data,:version => :draft1)) + assert_valid schema, data data["a"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) + refute_valid schema, data schema["properties"]["a"]["disallow"] = ["integer","string"] data["a"] = 'string' - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) + refute_valid schema, data data["a"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) + refute_valid schema, data data["a"] = false - assert(JSON::Validator.validate(schema,data,:version => :draft1)) + assert_valid schema, data end - - - def test_additional_properties - # Test no additional properties allowed - schema = { - "properties" => { - "a" => { "type" => "integer" } - }, - "additionalProperties" => false - } - - data = { - "a" => 10 - } - - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - data["b"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - # Test additional properties match a schema - schema["additionalProperties"] = { "type" => "string" } - data["b"] = "taco" - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - data["b"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - end - - - def test_items - schema = { - "items" => { "type" => "integer" } - } - - data = [1,2,4] - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - data = [1,2,"string"] - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - schema = { - "items" => [ - {"type" => "integer"}, - {"type" => "string"} - ] - } - - data = [1,"string"] - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - data = [1,"string",3] - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - data = ["string",1] - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - - end - - - def test_format_ipv4 - schema = { - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "ip-address"}} - } - - data = {"a" => "1.1.1.1"} - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "1.1.1"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "1.1.1.300"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => 5} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "1.1.1"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "1.1.1.1b"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "b1.1.1.1"} - end - - - def test_format_ipv6 - schema = { - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "ipv6"}} - } - - data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff"} - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "1111:0:8888:0:0:0:eeee:ffff"} - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "1111:2222:8888::eeee:ffff"} - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "1111:2222:8888:99999:aaaa:cccc:eeee:ffff"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:gggg"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "1111:2222::9999::cccc:eeee:ffff"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - assert(JSON::Validator.validate(schema, {"a" => "::1"}, :version => :draft1), 'validate with shortcut') - assert(!JSON::Validator.validate(schema, {"a" => "42"}, :version => :draft1), 'not validate a simple number') - end - - def test_format_time - schema = { - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "time"}} - } - - data = {"a" => "12:00:00"} - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "12:00"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "12:00:60"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "12:60:00"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "24:00:00"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "0:00:00"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "-12:00:00"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "12:00:00b"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "12:00:00"} - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "12:00:00\nabc"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - end - - - def test_format_date - schema = { - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "date"}} - } - - data = {"a" => "2010-01-01"} - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "2010-01-32"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "n2010-01-01"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "2010-1-01"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "2010-01-1"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "2010-01-01n"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "2010-01-01\nabc"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - end - def test_format_datetime schema = { "type" => "object", "properties" => { "a" => {"type" => "string", "format" => "date-time"}} } - data = {"a" => "2010-01-01T12:00:00Z"} - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "2010-01-32T12:00:00Z"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "2010-13-01T12:00:00Z"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "2010-01-01T24:00:00Z"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "2010-01-01T12:60:00Z"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "2010-01-01T12:00:60Z"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "2010-01-01T12:00:00z"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "2010-01-0112:00:00Z"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => "2010-01-01T12:00:00Z\nabc"} - assert(!JSON::Validator.validate(schema,data,:version => :draft1)) - end - - def test_format_unknown - schema = { - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "unknown"}} - } - - data = {"a" => "I can write what I want here"} - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - data = {"a" => ""} - assert(JSON::Validator.validate(schema,data,:version => :draft1)) - end - - - def test_format_union - data1 = {"a" => "boo"} - data2 = {"a" => nil} - - schema = { - "type" => "object", - "properties" => { "a" => {"type" => ["string","null"], "format" => "ip-address"}} - } - assert(!JSON::Validator.validate(schema,data1,:version => :draft1)) - assert(JSON::Validator.validate(schema,data2,:version => :draft1)) + assert_valid schema, {"a" => "2010-01-01T12:00:00Z"} + refute_valid schema, {"a" => "2010-01-32T12:00:00Z"} + refute_valid schema, {"a" => "2010-13-01T12:00:00Z"} + refute_valid schema, {"a" => "2010-01-01T24:00:00Z"} + refute_valid schema, {"a" => "2010-01-01T12:60:00Z"} + refute_valid schema, {"a" => "2010-01-01T12:00:60Z"} + refute_valid schema, {"a" => "2010-01-01T12:00:00z"} + refute_valid schema, {"a" => "2010-01-0112:00:00Z"} + refute_valid schema, {"a" => "2010-01-01T12:00:00Z\nabc"} end end diff --git a/test/test_jsonschema_draft2.rb b/test/test_jsonschema_draft2.rb index 2f644f65..af919d06 100644 --- a/test/test_jsonschema_draft2.rb +++ b/test/test_jsonschema_draft2.rb @@ -1,169 +1,37 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) -class JSONSchemaDraft2Test < Test::Unit::TestCase - def test_types - # Set up the default datatype - schema = { - "properties" => { - "a" => {} - } - } - data = { - "a" => nil - } - - # Test integers - schema["properties"]["a"]["type"] = "integer" - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - - # Test numbers - schema["properties"]["a"]["type"] = "number" - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 5.2 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - - # Test strings - schema["properties"]["a"]["type"] = "string" - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - data['a'] = 'string' - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - - # Test booleans - schema["properties"]["a"]["type"] = "boolean" - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - data['a'] = true - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data['a'] = false - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - - # Test object - schema["properties"]["a"]["type"] = "object" - data["a"] = {} - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - - # Test array - schema["properties"]["a"]["type"] = "array" - data["a"] = [] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - - # Test null - schema["properties"]["a"]["type"] = "null" - data["a"] = nil - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - - # Test any - schema["properties"]["a"]["type"] = "any" - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 5.2 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data['a'] = 'string' - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data['a'] = true - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - - # Test a union type - schema["properties"]["a"]["type"] = ["integer","string"] - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 'boo' - assert(JSON::Validator.validate(schema,data,:version => :draft2)) +class JSONSchemaDraft2Test < Minitest::Test + def schema_version + :draft2 + end - data["a"] = false - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) + def exclusive_minimum + { 'minimumCanEqual' => false } + end - # Test a union type with schemas - schema["properties"]["a"]["type"] = [{ "type" => "string" }, {"type" => "object", "properties" => {"b" => {"type" => "integer"}}}] + def exclusive_maximum + { 'maximumCanEqual' => false } + end - data["a"] = "test" - assert(JSON::Validator.validate(schema,data,:version => :draft2)) + def multiple_of + 'divisibleBy' + end - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) + include ArrayValidation::ItemsTests + include ArrayValidation::UniqueItemsTests - data["a"] = {"b" => 5} - assert(JSON::Validator.validate(schema,data,:version => :draft2)) + include NumberValidation::MinMaxTests + include NumberValidation::MultipleOfTests - data["a"] = {"b" => "taco"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - end + include ObjectValidation::AdditionalPropertiesTests + include StringValidation::ValueTests + include StringValidation::FormatTests + include StringValidation::DateAndTimeFormatTests + include TypeValidation::SimpleTypeTests + include TypeValidation::AnyTypeTests + include TypeValidation::SchemaUnionTypeTests def test_optional # Set up the default datatype @@ -174,9 +42,9 @@ def test_optional } data = {} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) + refute_valid schema, data data['a'] = "Hello" - assert(JSON::Validator.validate(schema,data,:version => :draft2)) + assert_valid schema, data schema = { "properties" => { @@ -185,309 +53,9 @@ def test_optional } data = {} - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - + assert_valid schema, data end - - - def test_minimum - # Set up the default datatype - schema = { - "properties" => { - "a" => {"minimum" => 5} - } - } - - data = { - "a" => nil - } - - - # Test an integer - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 4 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - # Test a float - data["a"] = 5.0 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 4.9 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - # Test a non-number - data["a"] = "a string" - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - # Test exclusiveMinimum - schema["properties"]["a"]["minimumCanEqual"] = false - - data["a"] = 6 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - # Test with float - data["a"] = 5.00000001 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 5.0 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - end - - - - def test_maximum - # Set up the default datatype - schema = { - "properties" => { - "a" => {"maximum" => 5} - } - } - - data = { - "a" => nil - } - - - # Test an integer - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 6 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - # Test a float - data["a"] = 5.0 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 5.1 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - # Test a non-number - data["a"] = "a string" - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - # Test exclusiveMinimum - schema["properties"]["a"]["maximumCanEqual"] = false - - data["a"] = 4 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - # Test with float - data["a"] = 4.9999999 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 5.0 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - end - - - def test_min_items - # Set up the default datatype - schema = { - "properties" => { - "a" => {"minItems" => 1} - } - } - - data = { - "a" => nil - } - - # Test with an array - data["a"] = ["boo"] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = [] - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - # Test with a non-array - data["a"] = "boo" - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - end - - - - def test_max_items - # Set up the default datatype - schema = { - "properties" => { - "a" => {"maxItems" => 1} - } - } - - data = { - "a" => nil - } - - # Test with an array - data["a"] = ["boo"] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = ["boo","taco"] - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - # Test with a non-array - data["a"] = "boo" - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - end - - - - def test_unique_items - # Set up the default datatype - schema = { - "properties" => { - "a" => {"uniqueItems" => true} - } - } - - data = { - "a" => nil - } - - # Test with nulls - data["a"] = [nil,5] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = [nil,nil] - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - # Test with booleans - data["a"] = [true,4] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = [true,false] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = [true,true] - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - # Test with numbers - data["a"] = [4,true] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = [4,4.1] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = [4,4] - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - # Test with strings - data["a"] = ['a',true] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = ['a','ab'] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = ['a','a'] - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - # Test with arrays - data["a"] = [[1],true] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = [[1,2],[1,3]] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = [[1,2,3],[1,2,3]] - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - # Test with objects - data["a"] = [{"a" => 1},true] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = [{"a" => 1},{"a" => 2}] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = [{"a" => 1, "b" => 2}, {"a" => 1, "b" => 2}] - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - end - - - def test_pattern - # Set up the default datatype - schema = { - "properties" => { - "a" => {"pattern" => "\\d+ taco"} - } - } - - data = { - "a" => nil - } - - # Test strings - data["a"] = "156 taco bell" - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - # Test a non-string - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = "taco" - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - end - - - def test_min_length - # Set up the default datatype - schema = { - "properties" => { - "a" => {"minLength" => 1} - } - } - - data = { - "a" => nil - } - - # Try out strings - data["a"] = "t" - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = "" - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - # Try out non-string - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - end - - - def test_max_length - # Set up the default datatype - schema = { - "properties" => { - "a" => {"maxLength" => 1} - } - } - - data = { - "a" => nil - } - - # Try out strings - data["a"] = "t" - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = "tt" - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - # Try out non-string - data["a"] = 5 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - end - - def test_enum # Set up the default datatype schema = { @@ -502,60 +70,26 @@ def test_enum # Make sure all of the above are valid... data["a"] = 1 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) + assert_valid schema, data data["a"] = 'boo' - assert(JSON::Validator.validate(schema,data,:version => :draft2)) + assert_valid schema, data data["a"] = [1,2,3] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) + assert_valid schema, data data["a"] = {"a" => "b"} - assert(JSON::Validator.validate(schema,data,:version => :draft2)) + assert_valid schema, data # Test something that doesn't exist data["a"] = 'taco' - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) + refute_valid schema, data # Try it without the key data = {} - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - end - - - def test_divisible_by - # Set up the default datatype - schema = { - "properties" => { - "a" => {"divisibleBy" => 1.1} - } - } - - data = { - "a" => nil - } - - data["a"] = 3.3 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 3.4 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - schema["properties"]["a"]["divisibleBy"] = 2.0 - - data["a"] = 4.0 - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 'boo' - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - data["a"] = 5 - schema["properties"]["a"]["divisibleBy"] = 0 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) + assert_valid schema, data end - - def test_disallow # Set up the default datatype schema = { @@ -570,172 +104,21 @@ def test_disallow data["a"] = 'string' - assert(JSON::Validator.validate(schema,data,:version => :draft2)) + assert_valid schema, data data["a"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) + refute_valid schema, data schema["properties"]["a"]["disallow"] = ["integer","string"] data["a"] = 'string' - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) + refute_valid schema, data data["a"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) + refute_valid schema, data data["a"] = false - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - - end - - - - def test_additional_properties - # Test no additional properties allowed - schema = { - "properties" => { - "a" => { "type" => "integer" } - }, - "additionalProperties" => false - } - - data = { - "a" => 10 - } - - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - data["b"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - # Test additional properties match a schema - schema["additionalProperties"] = { "type" => "string" } - data["b"] = "taco" - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - data["b"] = 5 - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - end - - - def test_items - schema = { - "items" => { "type" => "integer" } - } - - data = [1,2,4] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - data = [1,2,"string"] - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - schema = { - "items" => [ - {"type" => "integer"}, - {"type" => "string"} - ] - } - - data = [1,"string"] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - data = [1,"string",3] - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - data = ["string",1] - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - - end - - - def test_format_ipv4 - schema = { - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "ip-address"}} - } - - data = {"a" => "1.1.1.1"} - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "1.1.1"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "1.1.1.300"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => 5} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "1.1.1"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "1.1.1.1b"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "b1.1.1.1"} - end - - - def test_format_ipv6 - schema = { - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "ipv6"}} - } - - data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff"} - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "1111:0:8888:0:0:0:eeee:ffff"} - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "1111:2222:8888::eeee:ffff"} - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "1111:2222:8888:99999:aaaa:cccc:eeee:ffff"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:gggg"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "1111:2222::9999::cccc:eeee:ffff"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - assert(JSON::Validator.validate(schema, {"a" => "::1"}, :version => :draft2), 'validate with shortcut') - assert(!JSON::Validator.validate(schema, {"a" => "42"}, :version => :draft2), 'not validate a simple number') - end - - def test_format_time - schema = { - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "time"}} - } - - data = {"a" => "12:00:00"} - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "12:00"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "12:00:60"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "12:60:00"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "24:00:00"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "0:00:00"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "-12:00:00"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "12:00:00b"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "12:00:00\nabc"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - end - - - def test_format_date - schema = { - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "date"}} - } - - data = {"a" => "2010-01-01"} - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "2010-01-32"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "n2010-01-01"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "2010-1-01"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "2010-01-1"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "2010-01-01n"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "2010-01-01\nabc"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) + assert_valid schema, data end def test_format_datetime @@ -744,49 +127,15 @@ def test_format_datetime "properties" => { "a" => {"type" => "string", "format" => "date-time"}} } - data = {"a" => "2010-01-01T12:00:00Z"} - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "2010-01-32T12:00:00Z"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "2010-13-01T12:00:00Z"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "2010-01-01T24:00:00Z"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "2010-01-01T12:60:00Z"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "2010-01-01T12:00:60Z"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "2010-01-01T12:00:00z"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "2010-01-0112:00:00Z"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => "2010-01-01T12:00:00Z\nabc"} - assert(!JSON::Validator.validate(schema,data,:version => :draft2)) - end - - def test_format_unknown - schema = { - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "unknown"}} - } - - data = {"a" => "I can write what I want here"} - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - data = {"a" => ""} - assert(JSON::Validator.validate(schema,data,:version => :draft2)) - end - - - def test_format_union - data1 = {"a" => "boo"} - data2 = {"a" => nil} - - schema = { - "type" => "object", - "properties" => { "a" => {"type" => ["string","null"], "format" => "ip-address"}} - } - assert(!JSON::Validator.validate(schema,data1,:version => :draft2)) - assert(JSON::Validator.validate(schema,data2,:version => :draft2)) + assert_valid schema, {"a" => "2010-01-01T12:00:00Z"} + refute_valid schema, {"a" => "2010-01-32T12:00:00Z"} + refute_valid schema, {"a" => "2010-13-01T12:00:00Z"} + refute_valid schema, {"a" => "2010-01-01T24:00:00Z"} + refute_valid schema, {"a" => "2010-01-01T12:60:00Z"} + refute_valid schema, {"a" => "2010-01-01T12:00:60Z"} + refute_valid schema, {"a" => "2010-01-01T12:00:00z"} + refute_valid schema, {"a" => "2010-01-0112:00:00Z"} + refute_valid schema, {"a" => "2010-01-01T12:00:00Z\nabc"} end end diff --git a/test/test_jsonschema_draft3.rb b/test/test_jsonschema_draft3.rb index b63de868..56f52557 100644 --- a/test/test_jsonschema_draft3.rb +++ b/test/test_jsonschema_draft3.rb @@ -1,8 +1,41 @@ # encoding: utf-8 -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) + +class JSONSchemaDraft3Test < Minitest::Test + def schema_version + :draft3 + end + + def exclusive_minimum + { 'exclusiveMinimum' => true } + end + + def exclusive_maximum + { 'exclusiveMaximum' => true } + end + + def multiple_of + 'divisibleBy' + end + + include ArrayValidation::ItemsTests + include ArrayValidation::AdditionalItemsTests + include ArrayValidation::UniqueItemsTests + + include NumberValidation::MinMaxTests + include NumberValidation::MultipleOfTests + + include ObjectValidation::AdditionalPropertiesTests + include ObjectValidation::PatternPropertiesTests + + include StringValidation::ValueTests + include StringValidation::FormatTests + include StringValidation::DateAndTimeFormatTests + + include TypeValidation::SimpleTypeTests + include TypeValidation::AnyTypeTests + include TypeValidation::SchemaUnionTypeTests -class JSONSchemaDraft3Test < Test::Unit::TestCase def test_types # Set up the default datatype schema = { @@ -15,197 +48,6 @@ def test_types "a" => nil } - # Test integers - schema["properties"]["a"]["type"] = "integer" - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data)) - - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'integer'}, 3)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'integer'}, "hello")) - - # Test numbers - schema["properties"]["a"]["type"] = "number" - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.2 - assert(JSON::Validator.validate(schema,data)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data)) - - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'number'}, 3)) - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'number'}, 3.14159265358979)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'number'}, "hello")) - - - # Test strings - schema["properties"]["a"]["type"] = "string" - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = 'string' - assert(JSON::Validator.validate(schema,data)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data)) - - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'string'}, 'hello')) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'string'}, 3.14159265358979)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'string'}, [])) - - - # Test booleans - schema["properties"]["a"]["type"] = "boolean" - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = true - assert(JSON::Validator.validate(schema,data)) - - data['a'] = false - assert(JSON::Validator.validate(schema,data)) - - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'boolean'}, true)) - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'boolean'}, false)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'boolean'}, nil)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'boolean'}, 3)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'boolean'}, "hello")) - - - # Test object - schema["properties"]["a"]["type"] = "object" - data["a"] = {} - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data)) - - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'object'}, {'a' => true})) - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'object'}, {})) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'object'}, [])) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'object'}, 3)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'object'}, "hello")) - - - # Test array - schema["properties"]["a"]["type"] = "array" - data["a"] = [] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data)) - - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'array'}, ['a'])) - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'array'}, [])) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'array'}, {})) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'array'}, 3)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'array'}, "hello")) - - - # Test null - schema["properties"]["a"]["type"] = "null" - data["a"] = nil - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data)) - - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'null'}, nil)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'null'}, false)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'null'}, [])) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'null'}, "hello")) - - - # Test any - schema["properties"]["a"]["type"] = "any" - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.2 - assert(JSON::Validator.validate(schema,data)) - - data['a'] = 'string' - assert(JSON::Validator.validate(schema,data)) - - data['a'] = true - assert(JSON::Validator.validate(schema,data)) - - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'any'}, true)) - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'any'}, nil)) - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'any'}, {})) - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'any'}, 3)) - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => 'any'}, "hello")) - - - # Test a union type - schema["properties"]["a"]["type"] = ["integer","string"] - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 'boo' - assert(JSON::Validator.validate(schema,data)) - - data["a"] = false - assert(!JSON::Validator.validate(schema,data)) - - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => ['string', 'null']}, "hello")) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-03/schema#",'type' => ['integer', 'object']}, "hello")) - - # Test a union type with schemas - schema["properties"]["a"]["type"] = [{ "type" => "string" }, {"type" => "object", "properties" => {"b" => {"type" => "integer"}}}] - - data["a"] = "test" - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data)) - - data["a"] = {"b" => 5} - assert(JSON::Validator.validate(schema,data)) - - data["a"] = {"b" => "taco"} - assert(!JSON::Validator.validate(schema,data)) - # Test an array of unioned-type objects that prevent additionalProperties schema["properties"]["a"] = { 'type' => 'array', @@ -224,7 +66,7 @@ def test_types # This should actually pass, because this matches the first schema in the union data["a"] << {"c" => false} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data end def test_required @@ -237,9 +79,9 @@ def test_required } data = {} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data data['a'] = "Hello" - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data schema = { "$schema" => "http://json-schema.org/draft-03/schema#", @@ -249,236 +91,7 @@ def test_required } data = {} - assert(JSON::Validator.validate(schema,data)) - - end - - - - def test_minimum - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "properties" => { - "a" => {"minimum" => 5} - } - } - - data = { - "a" => nil - } - - - # Test an integer - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 4 - assert(!JSON::Validator.validate(schema,data)) - - # Test a float - data["a"] = 5.0 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 4.9 - assert(!JSON::Validator.validate(schema,data)) - - # Test a non-number - data["a"] = "a string" - assert(JSON::Validator.validate(schema,data)) - - # Test exclusiveMinimum - schema["properties"]["a"]["exclusiveMinimum"] = true - - data["a"] = 6 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data)) - - # Test with float - data["a"] = 5.00000001 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.0 - assert(!JSON::Validator.validate(schema,data)) - end - - - - def test_maximum - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "properties" => { - "a" => {"maximum" => 5} - } - } - - data = { - "a" => nil - } - - - # Test an integer - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 6 - assert(!JSON::Validator.validate(schema,data)) - - # Test a float - data["a"] = 5.0 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.1 - assert(!JSON::Validator.validate(schema,data)) - - # Test a non-number - data["a"] = "a string" - assert(JSON::Validator.validate(schema,data)) - - # Test exclusiveMinimum - schema["properties"]["a"]["exclusiveMaximum"] = true - - data["a"] = 4 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data)) - - # Test with float - data["a"] = 4.9999999 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.0 - assert(!JSON::Validator.validate(schema,data)) - end - - - def test_min_items - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "properties" => { - "a" => {"minItems" => 1} - } - } - - data = { - "a" => nil - } - - # Test with an array - data["a"] = ["boo"] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [] - assert(!JSON::Validator.validate(schema,data)) - - # Test with a non-array - data["a"] = "boo" - assert(JSON::Validator.validate(schema,data)) - end - - - - def test_max_items - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "properties" => { - "a" => {"maxItems" => 1} - } - } - - data = { - "a" => nil - } - - # Test with an array - data["a"] = ["boo"] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = ["boo","taco"] - assert(!JSON::Validator.validate(schema,data)) - - # Test with a non-array - data["a"] = "boo" - assert(JSON::Validator.validate(schema,data)) - end - - - - def test_unique_items - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "properties" => { - "a" => {"uniqueItems" => true} - } - } - - data = { - "a" => nil - } - - # Test with nulls - data["a"] = [nil,5] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [nil,nil] - assert(!JSON::Validator.validate(schema,data)) - - # Test with booleans - data["a"] = [true,4] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [true,false] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [true,true] - assert(!JSON::Validator.validate(schema,data)) - - # Test with numbers - data["a"] = [4,true] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [4,4.1] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [4,4] - assert(!JSON::Validator.validate(schema,data)) - - # Test with strings - data["a"] = ['a',true] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = ['a','ab'] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = ['a','a'] - assert(!JSON::Validator.validate(schema,data)) - - # Test with arrays - data["a"] = [[1],true] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [[1,2],[1,3]] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [[1,2,3],[1,2,3]] - assert(!JSON::Validator.validate(schema,data)) - - # Test with objects - data["a"] = [{"a" => 1},true] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [{"a" => 1},{"a" => 2}] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [{"a" => 1, "b" => 2}, {"a" => 1, "b" => 2}] - assert(!JSON::Validator.validate(schema,data)) + assert_valid schema, data end def test_strict_properties @@ -580,84 +193,6 @@ def test_strict_properties_pattern_props assert(!JSON::Validator.validate(schema,data,:strict => true)) end - def test_pattern - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "properties" => { - "a" => {"pattern" => "\\d+ taco"} - } - } - - data = { - "a" => nil - } - - # Test strings - data["a"] = "156 taco bell" - assert(JSON::Validator.validate(schema,data)) - - # Test a non-string - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = "taco" - assert(!JSON::Validator.validate(schema,data)) - end - - - def test_min_length - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "properties" => { - "a" => {"minLength" => 1} - } - } - - data = { - "a" => nil - } - - # Try out strings - data["a"] = "t" - assert(JSON::Validator.validate(schema,data)) - - data["a"] = "" - assert(!JSON::Validator.validate(schema,data)) - - # Try out non-string - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) - end - - - def test_max_length - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "properties" => { - "a" => {"maxLength" => 1} - } - } - - data = { - "a" => nil - } - - # Try out strings - data["a"] = "t" - assert(JSON::Validator.validate(schema,data)) - - data["a"] = "tt" - assert(!JSON::Validator.validate(schema,data)) - - # Try out non-string - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) - end - - def test_enum # Set up the default datatype schema = { @@ -673,61 +208,26 @@ def test_enum # Make sure all of the above are valid... data["a"] = 1 - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data["a"] = 'boo' - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data["a"] = [1,2,3] - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data["a"] = {"a" => "b"} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data # Test something that doesn't exist data["a"] = 'taco' - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data # Try it without the key data = {} - assert(JSON::Validator.validate(schema,data)) - end - - - def test_divisible_by - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "properties" => { - "a" => {"divisibleBy" => 1.1} - } - } - - data = { - "a" => nil - } - - data["a"] = 3.3 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 3.4 - assert(!JSON::Validator.validate(schema,data)) - - schema["properties"]["a"]["divisibleBy"] = 2.0 - - data["a"] = 4.0 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 'boo' - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5 - schema["properties"]["a"]["divisibleBy"] = 0 - assert(!JSON::Validator.validate(schema,data)) + assert_valid schema, data end - - def test_disallow # Set up the default datatype schema = { @@ -743,21 +243,21 @@ def test_disallow data["a"] = 'string' - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data["a"] = 5 - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data schema["properties"]["a"]["disallow"] = ["integer","string"] data["a"] = 'string' - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data data["a"] = 5 - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data data["a"] = false - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data end @@ -782,131 +282,14 @@ def test_extends "a" => 10 } - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data assert(!JSON::Validator.validate(schema2,data)) schema["extends"] = schema2 - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data end - def test_pattern_properties - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "patternProperties" => { - "\\d+ taco" => {"type" => "integer"} - } - } - - data = { - "a" => true, - "1 taco" => 1, - "20 tacos" => 20 - } - - assert(JSON::Validator.validate(schema,data)) - data["20 tacos"] = "string!" - assert(!JSON::Validator.validate(schema,data)) - end - - - def test_additional_properties - # Test no additional properties allowed - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "properties" => { - "a" => { "type" => "integer" } - }, - "additionalProperties" => false - } - - data = { - "a" => 10 - } - - assert(JSON::Validator.validate(schema,data)) - data["b"] = 5 - assert(!JSON::Validator.validate(schema,data)) - - # Test additional properties match a schema - schema["additionalProperties"] = { "type" => "string" } - data["b"] = "taco" - assert(JSON::Validator.validate(schema,data)) - data["b"] = 5 - assert(!JSON::Validator.validate(schema,data)) - - # Make sure this works with pattern properties set, too - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "patternProperties" => { - "\\d+ taco" => {"type" => "integer"} - }, - "additionalProperties" => false - } - - data = { - "5 tacos" => 5, - "20 tacos" => 20 - } - - assert(JSON::Validator.validate(schema,data)) - data["b"] = 5 - assert(!JSON::Validator.validate(schema,data)) - end - - - def test_items - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "items" => { "type" => "integer" } - } - - data = [1,2,4] - assert(JSON::Validator.validate(schema,data)) - data = [1,2,"string"] - assert(!JSON::Validator.validate(schema,data)) - - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "items" => [ - {"type" => "integer"}, - {"type" => "string"} - ] - } - - data = [1,"string"] - assert(JSON::Validator.validate(schema,data)) - data = [1,"string",3] - assert(JSON::Validator.validate(schema,data)) - data = ["string",1] - assert(!JSON::Validator.validate(schema,data)) - - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "items" => [ - {"type" => "integer"}, - {"type" => "string"} - ], - "additionalItems" => false - } - - data = [1,"string"] - assert(JSON::Validator.validate(schema,data)) - data = [1,"string",3] - assert(!JSON::Validator.validate(schema,data)) - - schema = {"$schema" => "http://json-schema.org/draft-03/schema#","items" => [{"type" => "integer"},{"type" => "string"}],"additionalItems" => {"type" => "integer"}} - - data = [1,"string"] - assert(JSON::Validator.validate(schema,data)) - data = [1,"string",3] - assert(JSON::Validator.validate(schema,data)) - data = [1,"string","string"] - assert(!JSON::Validator.validate(schema,data)) - end - - def test_list_option schema = { "$schema" => "http://json-schema.org/draft-03/schema#", @@ -916,7 +299,7 @@ def test_list_option data = [{"a" => 1},{"a" => 2},{"a" => 3}] assert(JSON::Validator.validate(schema,data,:list => true)) - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data data = {"a" => 1} assert(!JSON::Validator.validate(schema,data,:list => true)) @@ -933,110 +316,8 @@ def test_self_reference "properties" => { "a" => {"type" => "integer"}, "b" => {"$ref" => "#"}} } - data = {"a" => 5, "b" => {"b" => {"a" => 1}}} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => 5, "b" => {"b" => {"a" => 'taco'}}} - assert(!JSON::Validator.validate(schema,data)) - end - - - def test_format_ipv4 - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "ip-address"}} - } - - data = {"a" => "1.1.1.1"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "1.1.1"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "1.1.1.300"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => 5} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "1.1.1"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "1.1.1.1b"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "b1.1.1.1"} - end - - - def test_format_ipv6 - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "ipv6"}} - } - - data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "1111:0:8888:0:0:0:eeee:ffff"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "1111:2222:8888::eeee:ffff"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "1111:2222:8888:99999:aaaa:cccc:eeee:ffff"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:gggg"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "1111:2222::9999::cccc:eeee:ffff"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"} - assert(!JSON::Validator.validate(schema,data)) - assert(JSON::Validator.validate(schema, {"a" => "::1"}), 'validate with shortcut') - assert(!JSON::Validator.validate(schema, {"a" => "42"}), 'not validate a simple number') - end - - def test_format_time - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "time"}} - } - - data = {"a" => "12:00:00"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "12:00"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "12:00:60"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "12:60:00"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "24:00:00"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "0:00:00"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "-12:00:00"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "12:00:00b"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "12:00:00\nabc"} - assert(!JSON::Validator.validate(schema,data)) - end - - - def test_format_date - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "date"}} - } - - data = {"a" => "2010-01-01"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-32"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "n2010-01-01"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-1-01"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-1"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01n"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01\nabc"} - assert(!JSON::Validator.validate(schema,data)) + assert_valid schema, {"a" => 5, "b" => {"b" => {"a" => 1}}} + refute_valid schema, {"a" => 5, "b" => {"b" => {"a" => 'taco'}}} end def test_format_datetime @@ -1046,87 +327,36 @@ def test_format_datetime "properties" => { "a" => {"type" => "string", "format" => "date-time"}} } - data = {"a" => "2010-01-01T12:00:00Z"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00.1Z"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00,1Z"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-32T12:00:00Z"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-13-01T12:00:00Z"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T24:00:00Z"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:60:00Z"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:60Z"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00z"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-0112:00:00Z"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00.1Z\nabc"} - assert(!JSON::Validator.validate(schema,data)) + assert_valid schema, {"a" => "2010-01-01T12:00:00Z"} + assert_valid schema, {"a" => "2010-01-01T12:00:00.1Z"} + assert_valid schema, {"a" => "2010-01-01T12:00:00,1Z"} + refute_valid schema, {"a" => "2010-01-32T12:00:00Z"} + refute_valid schema, {"a" => "2010-13-01T12:00:00Z"} + refute_valid schema, {"a" => "2010-01-01T24:00:00Z"} + refute_valid schema, {"a" => "2010-01-01T12:60:00Z"} + refute_valid schema, {"a" => "2010-01-01T12:00:60Z"} + refute_valid schema, {"a" => "2010-01-01T12:00:00z"} + refute_valid schema, {"a" => "2010-01-0112:00:00Z"} + refute_valid schema, {"a" => "2010-01-01T12:00:00.1Z\nabc"} # test with a specific timezone - data = {"a" => "2010-01-01T12:00:00+01"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00+01:00"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00+01:30"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00+0234"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00+01:"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00+0"} - assert(!JSON::Validator.validate(schema,data)) + assert_valid schema, {"a" => "2010-01-01T12:00:00+01"} + assert_valid schema, {"a" => "2010-01-01T12:00:00+01:00"} + assert_valid schema, {"a" => "2010-01-01T12:00:00+01:30"} + assert_valid schema, {"a" => "2010-01-01T12:00:00+0234"} + refute_valid schema, {"a" => "2010-01-01T12:00:00+01:"} + refute_valid schema, {"a" => "2010-01-01T12:00:00+0"} # do not allow mixing Z and specific timezone - data = {"a" => "2010-01-01T12:00:00Z+01"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00+01Z"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00+01:30Z"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00+0Z"} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, {"a" => "2010-01-01T12:00:00Z+01"} + refute_valid schema, {"a" => "2010-01-01T12:00:00+01Z"} + refute_valid schema, {"a" => "2010-01-01T12:00:00+01:30Z"} + refute_valid schema, {"a" => "2010-01-01T12:00:00+0Z"} # test without any timezone - data = {"a" => "2010-01-01T12:00:00"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00.12345"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00,12345"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00.12345"} - assert(JSON::Validator.validate(schema,data)) - end - - def test_format_unknown - schema = { - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "unknown"}} - } - - data = {"a" => "I can write what I want here"} - assert(JSON::Validator.validate(schema,data,:version => :draft3)) - data = {"a" => ""} - assert(JSON::Validator.validate(schema,data,:version => :draft3)) - end - - - def test_format_union - data1 = {"a" => "boo"} - data2 = {"a" => nil} - - schema = { - "$schema" => "http://json-schema.org/draft-03/schema#", - "type" => "object", - "properties" => { "a" => {"type" => ["string","null"], "format" => "ip-address"}} - } - assert(!JSON::Validator.validate(schema,data1)) - assert(JSON::Validator.validate(schema,data2)) + assert_valid schema, {"a" => "2010-01-01T12:00:00"} + assert_valid schema, {"a" => "2010-01-01T12:00:00.12345"} + assert_valid schema, {"a" => "2010-01-01T12:00:00,12345"} + assert_valid schema, {"a" => "2010-01-01T12:00:00.12345"} end def test_format_uri @@ -1154,13 +384,13 @@ def test_schema } data = {"a" => "taco"} - assert(!JSON::Validator.validate(schema,data)) + assert(!JSON::Validator.validate(schema, data)) schema = { "$schema" => "http://json-schema.org/draft-03/schema#", "type" => "object" } - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data end def test_dependency @@ -1177,9 +407,9 @@ def test_dependency } data = {"a" => 1, "b" => 2} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data = {"a" => 1} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data schema = { "$schema" => "http://json-schema.org/draft-03/schema#", @@ -1195,9 +425,9 @@ def test_dependency } data = {"a" => 1, "c" => 2} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data data = {"a" => 1, "b" => 2, "c" => 3} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data end def test_default @@ -1211,7 +441,7 @@ def test_default } data = {:b => 2} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data assert_nil(data["a"]) assert(JSON::Validator.validate(schema,data, :insert_defaults => true)) assert_equal(42, data["a"]) @@ -1227,7 +457,7 @@ def test_default } data = {:b => 2} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data assert_nil(data["a"]) assert(JSON::Validator.validate(schema,data, :insert_defaults => true)) assert_equal(42, data["a"]) @@ -1243,7 +473,7 @@ def test_default } data = {:b => 2} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data assert_nil(data["a"]) assert(!JSON::Validator.validate(schema,data, :insert_defaults => true)) assert_nil(data["a"]) @@ -1259,7 +489,7 @@ def test_default } data = {:b => 2} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data assert_nil(data["a"]) assert(!JSON::Validator.validate(schema,data, :insert_defaults => true)) assert_equal("42",data["a"]) diff --git a/test/test_jsonschema_draft4.rb b/test/test_jsonschema_draft4.rb index b1f465c7..bb90893f 100644 --- a/test/test_jsonschema_draft4.rb +++ b/test/test_jsonschema_draft4.rb @@ -1,195 +1,37 @@ # encoding: utf-8 -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) -class JSONSchemaDraft4Test < Test::Unit::TestCase - def test_types - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "properties" => { - "a" => {} - } - } - data = { - "a" => nil - } - - # Test integers - schema["properties"]["a"]["type"] = "integer" - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data)) - - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'integer'}, 3)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'integer'}, "hello")) - - # Test numbers - schema["properties"]["a"]["type"] = "number" - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.2 - assert(JSON::Validator.validate(schema,data)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data)) - - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'number'}, 3)) - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'number'}, 3.14159265358979)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'number'}, "hello")) - - - # Test strings - schema["properties"]["a"]["type"] = "string" - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = 'string' - assert(JSON::Validator.validate(schema,data)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data)) - - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'string'}, 'hello')) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'string'}, 3.14159265358979)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'string'}, [])) - - - # Test booleans - schema["properties"]["a"]["type"] = "boolean" - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = true - assert(JSON::Validator.validate(schema,data)) - - data['a'] = false - assert(JSON::Validator.validate(schema,data)) - - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'boolean'}, true)) - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'boolean'}, false)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'boolean'}, nil)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'boolean'}, 3)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'boolean'}, "hello")) - - # Test object - schema["properties"]["a"]["type"] = "object" - data["a"] = {} - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data)) - - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'object'}, {'a' => true})) - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'object'}, {})) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'object'}, [])) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'object'}, 3)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'object'}, "hello")) - - - # Test array - schema["properties"]["a"]["type"] = "array" - data["a"] = [] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data)) - - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'array'}, ['a'])) - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'array'}, [])) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'array'}, {})) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'array'}, 3)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'array'}, "hello")) - - - # Test null - schema["properties"]["a"]["type"] = "null" - data["a"] = nil - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.2 - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = 'string' - assert(!JSON::Validator.validate(schema,data)) - - data['a'] = true - assert(!JSON::Validator.validate(schema,data)) - - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'null'}, nil)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'null'}, false)) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'null'}, [])) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'null'}, "hello")) - - - # Test any - schema["properties"]["a"]["type"] = "any" - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.2 - assert(JSON::Validator.validate(schema,data)) +class JSONSchemaDraft4Test < Minitest::Test + def schema_version + :draft4 + end - data['a'] = 'string' - assert(JSON::Validator.validate(schema,data)) + def exclusive_minimum + { 'exclusiveMinimum' => true } + end - data['a'] = true - assert(JSON::Validator.validate(schema,data)) + def exclusive_maximum + { 'exclusiveMaximum' => true } + end - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'any'}, true)) - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'any'}, nil)) - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'any'}, {})) - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'any'}, 3)) - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => 'any'}, "hello")) + def ipv4_format + 'ipv4' + end + include ArrayValidation::ItemsTests + include ArrayValidation::AdditionalItemsTests + include ArrayValidation::UniqueItemsTests - # Test a union type - schema["properties"]["a"]["type"] = ["integer","string"] - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) + include NumberValidation::MinMaxTests + include NumberValidation::MultipleOfTests - data["a"] = 'boo' - assert(JSON::Validator.validate(schema,data)) + include ObjectValidation::AdditionalPropertiesTests + include ObjectValidation::PatternPropertiesTests - data["a"] = false - assert(!JSON::Validator.validate(schema,data)) + include StringValidation::ValueTests + include StringValidation::FormatTests - assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => ['string', 'null']}, "hello")) - assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => ['integer', 'object']}, "hello")) - end + include TypeValidation::SimpleTypeTests def test_required # Set up the default datatype @@ -202,9 +44,9 @@ def test_required } data = {} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data data['a'] = "Hello" - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data schema = { "$schema" => "http://json-schema.org/draft-04/schema#", @@ -214,207 +56,30 @@ def test_required } data = {} - assert(JSON::Validator.validate(schema,data)) - - end - - - - def test_minimum - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "properties" => { - "a" => {"minimum" => 5} - } - } - - data = { - "a" => nil - } - - - # Test an integer - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 4 - assert(!JSON::Validator.validate(schema,data)) - - # Test a float - data["a"] = 5.0 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 4.9 - assert(!JSON::Validator.validate(schema,data)) - - # Test a non-number - data["a"] = "a string" - assert(JSON::Validator.validate(schema,data)) - - # Test exclusiveMinimum - schema["properties"]["a"]["exclusiveMinimum"] = true - - data["a"] = 6 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data)) - - # Test with float - data["a"] = 5.00000001 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.0 - assert(!JSON::Validator.validate(schema,data)) + assert_valid schema, data end - - - def test_maximum - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "properties" => { - "a" => {"maximum" => 5} - } - } - - data = { - "a" => nil - } - - - # Test an integer - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 6 - assert(!JSON::Validator.validate(schema,data)) - - # Test a float - data["a"] = 5.0 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.1 - assert(!JSON::Validator.validate(schema,data)) - - # Test a non-number - data["a"] = "a string" - assert(JSON::Validator.validate(schema,data)) - - # Test exclusiveMinimum - schema["properties"]["a"]["exclusiveMaximum"] = true - - data["a"] = 4 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5 - assert(!JSON::Validator.validate(schema,data)) - - # Test with float - data["a"] = 4.9999999 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5.0 - assert(!JSON::Validator.validate(schema,data)) - end - - - def test_min_items - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "properties" => { - "a" => {"minItems" => 1} - } - } - - data = { - "a" => nil - } - - # Test with an array - data["a"] = ["boo"] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [] - assert(!JSON::Validator.validate(schema,data)) - - # Test with a non-array - data["a"] = "boo" - assert(JSON::Validator.validate(schema,data)) - end - - - - def test_max_items - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "properties" => { - "a" => {"maxItems" => 1} - } - } - - data = { - "a" => nil - } - - # Test with an array - data["a"] = ["boo"] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = ["boo","taco"] - assert(!JSON::Validator.validate(schema,data)) - - # Test with a non-array - data["a"] = "boo" - assert(JSON::Validator.validate(schema,data)) - end - - def test_min_properties - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "minProperties" => 2, - "properties" => { - } - } + schema = { 'minProperties' => 2 } - data = {"a" => nil} - assert(!JSON::Validator.validate(schema,data)) - - data = {"a" => nil, "b" => nil} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, {'a' => 1, 'b' => 2} + assert_valid schema, {'a' => 1, 'b' => 2, 'c' => 3} - data = {"a" => nil, "b" => nil, "c" => nil} - assert(JSON::Validator.validate(schema,data)) + refute_valid schema, {'a' => 1} + refute_valid schema, {} end - - def test_max_properties - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "maxProperties" => 2, - "properties" => { - } - } - - data = {"a" => nil} - assert(JSON::Validator.validate(schema,data)) + schema = { 'maxProperties' => 2 } - data = {"a" => nil, "b" => nil} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, {'a' => 1, 'b' => 2} + assert_valid schema, {'a' => 1} + assert_valid schema, {} - data = {"a" => nil, "b" => nil, "c" => nil} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, {'a' => 1, 'b' => 2, 'c' => 3} end - def test_strict_properties + def test_strict_properties schema = { "$schema" => "http://json-schema.org/draft-04/schema#", "properties" => { @@ -494,156 +159,6 @@ def test_strict_properties_pattern_props assert(!JSON::Validator.validate(schema,data,:strict => true)) end - def test_unique_items - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "properties" => { - "a" => {"uniqueItems" => true} - } - } - - data = { - "a" => nil - } - - # Test with nulls - data["a"] = [nil,5] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [nil,nil] - assert(!JSON::Validator.validate(schema,data)) - - # Test with booleans - data["a"] = [true,4] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [true,false] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [true,true] - assert(!JSON::Validator.validate(schema,data)) - - # Test with numbers - data["a"] = [4,true] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [4,4.1] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [4,4] - assert(!JSON::Validator.validate(schema,data)) - - # Test with strings - data["a"] = ['a',true] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = ['a','ab'] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = ['a','a'] - assert(!JSON::Validator.validate(schema,data)) - - # Test with arrays - data["a"] = [[1],true] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [[1,2],[1,3]] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [[1,2,3],[1,2,3]] - assert(!JSON::Validator.validate(schema,data)) - - # Test with objects - data["a"] = [{"a" => 1},true] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [{"a" => 1},{"a" => 2}] - assert(JSON::Validator.validate(schema,data)) - - data["a"] = [{"a" => 1, "b" => 2}, {"a" => 1, "b" => 2}] - assert(!JSON::Validator.validate(schema,data)) - end - - - def test_pattern - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "properties" => { - "a" => {"pattern" => "\\d+ taco"} - } - } - - data = { - "a" => nil - } - - # Test strings - data["a"] = "156 taco bell" - assert(JSON::Validator.validate(schema,data)) - - # Test a non-string - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = "taco" - assert(!JSON::Validator.validate(schema,data)) - end - - - def test_min_length - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "properties" => { - "a" => {"minLength" => 1} - } - } - - data = { - "a" => nil - } - - # Try out strings - data["a"] = "t" - assert(JSON::Validator.validate(schema,data)) - - data["a"] = "" - assert(!JSON::Validator.validate(schema,data)) - - # Try out non-string - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) - end - - - def test_max_length - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "properties" => { - "a" => {"maxLength" => 1} - } - } - - data = { - "a" => nil - } - - # Try out strings - data["a"] = "t" - assert(JSON::Validator.validate(schema,data)) - - data["a"] = "tt" - assert(!JSON::Validator.validate(schema,data)) - - # Try out non-string - data["a"] = 5 - assert(JSON::Validator.validate(schema,data)) - end - - def test_enum # Set up the default datatype schema = { @@ -659,24 +174,24 @@ def test_enum # Make sure all of the above are valid... data["a"] = 1 - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data["a"] = 'boo' - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data["a"] = [1,2,3] - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data["a"] = {"a" => "b"} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data # Test something that doesn't exist data["a"] = 'taco' - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data # Try it without the key data = {} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data end def test_enum_with_schema_validation @@ -696,157 +211,6 @@ def test_enum_with_schema_validation assert(JSON::Validator.validate(schema,data,:validate_schema => true)) end - - def test_multiple_of - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "properties" => { - "a" => {"multipleOf" => 1.1} - } - } - - data = { - "a" => nil - } - - data["a"] = 3.3 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 3.4 - assert(!JSON::Validator.validate(schema,data)) - - schema["properties"]["a"]["multipleOf"] = 2.0 - - data["a"] = 4.0 - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 'boo' - assert(JSON::Validator.validate(schema,data)) - - data["a"] = 5 - schema["properties"]["a"]["multipleOf"] = 0 - assert(!JSON::Validator.validate(schema,data)) - end - - - def test_pattern_properties - # Set up the default datatype - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "patternProperties" => { - "\\d+ taco" => {"type" => "integer"} - } - } - - data = { - "a" => true, - "1 taco" => 1, - "20 tacos" => 20 - } - - assert(JSON::Validator.validate(schema,data)) - data["20 tacos"] = "string!" - assert(!JSON::Validator.validate(schema,data)) - end - - - def test_additional_properties - # Test no additional properties allowed - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "properties" => { - "a" => { "type" => "integer" } - }, - "additionalProperties" => false - } - - data = { - "a" => 10 - } - - assert(JSON::Validator.validate(schema,data)) - data["b"] = 5 - assert(!JSON::Validator.validate(schema,data)) - - # Test additional properties match a schema - schema["additionalProperties"] = { "type" => "string" } - data["b"] = "taco" - assert(JSON::Validator.validate(schema,data)) - data["b"] = 5 - assert(!JSON::Validator.validate(schema,data)) - - # Make sure this works with pattern properties set, too - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "patternProperties" => { - "\\d+ taco" => {"type" => "integer"} - }, - "additionalProperties" => false - } - - data = { - "5 tacos" => 5, - "20 tacos" => 20 - } - - assert(JSON::Validator.validate(schema,data)) - data["b"] = 5 - assert(!JSON::Validator.validate(schema,data)) - end - - - def test_items - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "items" => { "type" => "integer" } - } - - data = [1,2,4] - assert(JSON::Validator.validate(schema,data)) - data = [1,2,"string"] - assert(!JSON::Validator.validate(schema,data)) - - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "items" => [ - {"type" => "integer"}, - {"type" => "string"} - ] - } - - data = [1,"string"] - assert(JSON::Validator.validate(schema,data)) - data = [1,"string",3] - assert(JSON::Validator.validate(schema,data)) - data = ["string",1] - assert(!JSON::Validator.validate(schema,data)) - - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "items" => [ - {"type" => "integer"}, - {"type" => "string"} - ], - "additionalItems" => false - } - - data = [1,"string"] - assert(JSON::Validator.validate(schema,data)) - data = [1,"string",3] - assert(!JSON::Validator.validate(schema,data)) - - schema = {"$schema" => "http://json-schema.org/draft-04/schema#","items" => [{"type" => "integer"},{"type" => "string"}],"additionalItems" => {"type" => "integer"}} - - data = [1,"string"] - assert(JSON::Validator.validate(schema,data)) - data = [1,"string",3] - assert(JSON::Validator.validate(schema,data)) - data = [1,"string","string"] - assert(!JSON::Validator.validate(schema,data)) - end - - def test_list_option schema = { "$schema" => "http://json-schema.org/draft-04/schema#", @@ -857,7 +221,7 @@ def test_list_option data = [{"a" => 1},{"a" => 2},{"a" => 3}] assert(JSON::Validator.validate(schema,data,:list => true)) - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data data = {"a" => 1} assert(!JSON::Validator.validate(schema,data,:list => true)) @@ -874,62 +238,10 @@ def test_self_reference "properties" => { "a" => {"type" => "integer"}, "b" => {"$ref" => "#"}} } - data = {"a" => 5, "b" => {"b" => {"a" => 1}}} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => 5, "b" => {"b" => {"a" => 'taco'}}} - assert(!JSON::Validator.validate(schema,data)) - end - - - def test_format_ipv4 - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "ipv4"}} - } - - data = {"a" => "1.1.1.1"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "1.1.1"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "1.1.1.300"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => 5} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "1.1.1"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "1.1.1.1b"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "b1.1.1.1"} - end - - - def test_format_ipv6 - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "ipv6"}} - } - - data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "1111:0:8888:0:0:0:eeee:ffff"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "1111:2222:8888::eeee:ffff"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "1111:2222:8888:99999:aaaa:cccc:eeee:ffff"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:gggg"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "1111:2222::9999::cccc:eeee:ffff"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"} - assert(!JSON::Validator.validate(schema,data)) - assert(JSON::Validator.validate(schema, {"a" => "::1"}), 'validate with shortcut') - assert(!JSON::Validator.validate(schema, {"a" => "42"}), 'not validate a simple number') + assert_valid schema, {"a" => 5, "b" => {"b" => {"a" => 1}}} + refute_valid schema, {"a" => 5, "b" => {"b" => {"a" => 'taco'}}} end - def test_format_datetime schema = { "$schema" => "http://json-schema.org/draft-04/schema#", @@ -937,30 +249,18 @@ def test_format_datetime "properties" => { "a" => {"type" => "string", "format" => "date-time"}} } - data = {"a" => "2010-01-01T12:00:00Z"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00.1Z"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00,1Z"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00+0000"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00+00:00"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-32T12:00:00Z"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-13-01T12:00:00Z"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T24:00:00Z"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:60:00Z"} - assert(!JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:60Z"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-01T12:00:00z"} - assert(JSON::Validator.validate(schema,data)) - data = {"a" => "2010-01-0112:00:00Z"} - assert(!JSON::Validator.validate(schema,data)) + assert_valid schema, {"a" => "2010-01-01T12:00:00Z"} + assert_valid schema, {"a" => "2010-01-01T12:00:00.1Z"} + refute_valid schema, {"a" => "2010-01-01T12:00:00,1Z"} + refute_valid schema, {"a" => "2010-01-01T12:00:00+0000"} + assert_valid schema, {"a" => "2010-01-01T12:00:00+00:00"} + refute_valid schema, {"a" => "2010-01-32T12:00:00Z"} + refute_valid schema, {"a" => "2010-13-01T12:00:00Z"} + assert_valid schema, {"a" => "2010-01-01T24:00:00Z"} + refute_valid schema, {"a" => "2010-01-01T12:60:00Z"} + assert_valid schema, {"a" => "2010-01-01T12:00:60Z"} + assert_valid schema, {"a" => "2010-01-01T12:00:00z"} + refute_valid schema, {"a" => "2010-01-0112:00:00Z"} end def test_format_uri @@ -969,9 +269,9 @@ def test_format_uri data3 = {"a" => "http://ja.wikipedia.org/wiki/メインページ"} schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "uri"}} + "$schema" => "http://json-schema.org/draft-04/schema#", + "type" => "object", + "properties" => { "a" => {"type" => "string", "format" => "uri"}} } assert(JSON::Validator.validate(schema,data1)) @@ -979,34 +279,6 @@ def test_format_uri assert(JSON::Validator.validate(schema,data3)) end - def test_format_unknown - schema = { - "type" => "object", - "properties" => { "a" => {"type" => "string", "format" => "unknown"}} - } - - data = {"a" => "I can write what I want here"} - assert(JSON::Validator.validate(schema,data,:version => :draft4)) - data = {"a" => ""} - assert(JSON::Validator.validate(schema,data,:version => :draft4)) - end - - - def test_format_union - data1 = {"a" => "boo"} - data2 = {"a" => nil} - - schema = { - "$schema" => "http://json-schema.org/draft-04/schema#", - "type" => "object", - "properties" => { "a" => {"type" => ["string","null"], "format" => "ipv4"}} - } - assert(!JSON::Validator.validate(schema,data1)) - assert(JSON::Validator.validate(schema,data2)) - end - - - def test_schema schema = { "$schema" => "http://json-schema.org/THIS-IS-NOT-A-SCHEMA", @@ -1020,7 +292,7 @@ def test_schema "$schema" => "http://json-schema.org/draft-04/schema#", "type" => "object" } - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data end def test_dependency @@ -1037,9 +309,9 @@ def test_dependency } data = {"a" => 1, "b" => 2} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data = {"a" => 1} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data schema = { "$schema" => "http://json-schema.org/draft-04/schema#", @@ -1055,9 +327,9 @@ def test_dependency } data = {"a" => 1, "c" => 2} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data data = {"a" => 1, "b" => 2, "c" => 3} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data end def test_schema_dependency @@ -1097,7 +369,7 @@ def test_default } data = {:b => 2} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data assert_nil(data["a"]) assert(JSON::Validator.validate(schema,data, :insert_defaults => true)) assert_equal(42, data["a"]) @@ -1114,7 +386,7 @@ def test_default } data = {:b => 2} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data assert_nil(data["a"]) assert(JSON::Validator.validate(schema,data, :insert_defaults => true)) assert_equal(42, data["a"]) @@ -1131,7 +403,7 @@ def test_default } data = {:b => 2} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data assert_nil(data["a"]) assert(!JSON::Validator.validate(schema,data, :insert_defaults => true)) assert_nil(data["a"]) @@ -1147,7 +419,7 @@ def test_default } data = {:b => 2} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data assert_nil(data["a"]) assert(!JSON::Validator.validate(schema,data, :insert_defaults => true)) assert_equal("42",data["a"]) @@ -1171,16 +443,16 @@ def test_all_of } data = {"a" => "hello", "b" => 5} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data = {"a" => "hello"} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data = {"a" => "hello", "b" => "taco"} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data data = {"b" => 5} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data end @@ -1199,19 +471,19 @@ def test_any_of } data = {"a" => "hello", "b" => 5} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data = {"a" => "hello"} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data = {"a" => "hello", "b" => "taco"} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data = {"b" => 5} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data = {"a" => 5, "b" => "taco"} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data end @@ -1230,20 +502,20 @@ def test_one_of } data = {"a" => "hello", "b" => 5} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data # This passes because b is not required, thus matches both schemas data = {"a" => "hello"} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data data = {"a" => "hello", "b" => "taco"} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data = {"b" => 5} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data = {"a" => 5, "b" => "taco"} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data end @@ -1257,13 +529,13 @@ def test_not } data = {"a" => 1} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data = {"a" => "hi!"} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data data = {"a" => true} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data # Sub-schema not schema = { @@ -1285,19 +557,19 @@ def test_not } data = {"a" => 1} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data = {"a" => "hi!"} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data data = {"a" => true} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data data = {"a" => {"b" => true}} - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data data = {"a" => {"b" => 5}} - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data end def test_not_fully_validate @@ -1333,10 +605,10 @@ def test_definitions } data = [1,2,3] - assert(JSON::Validator.validate(schema,data)) + assert_valid schema, data data = [-1,2,3] - assert(!JSON::Validator.validate(schema,data)) + refute_valid schema, data end end diff --git a/test/test_list_option.rb b/test/test_list_option.rb index 7b891388..47da250b 100644 --- a/test/test_list_option.rb +++ b/test/test_list_option.rb @@ -1,7 +1,6 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) -class ListOptionTest < Test::Unit::TestCase +class ListOptionTest < Minitest::Test def test_list_option_reusing_schemas schema_hash = { "$schema" => "http://json-schema.org/draft-04/schema#", @@ -14,9 +13,9 @@ def test_list_option_reusing_schemas JSON::Validator.add_schema(schema) data = {"a" => 1} - assert(JSON::Validator.validate(uri.to_s, data)) + assert_valid uri.to_s, data data = [{"a" => 1}] - assert(JSON::Validator.validate(uri.to_s, data, :list => true)) + assert_valid uri.to_s, data, :list => true end -end \ No newline at end of file +end diff --git a/test/test_merge_misisng_values.rb b/test/test_merge_missing_values.rb similarity index 92% rename from test/test_merge_misisng_values.rb rename to test/test_merge_missing_values.rb index 437c35cf..a375a89e 100644 --- a/test/test_merge_misisng_values.rb +++ b/test/test_merge_missing_values.rb @@ -1,7 +1,6 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) -class MergeMissingValuesTest < Test::Unit::TestCase +class MergeMissingValuesTest < Minitest::Test def test_merge_missing_values_for_string original = 'foo' updated = 'foo' diff --git a/test/test_minitems.rb b/test/test_minitems.rb index 8257a9f4..d17d2242 100644 --- a/test/test_minitems.rb +++ b/test/test_minitems.rb @@ -1,14 +1,12 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) -class MinItemsTest < Test::Unit::TestCase +class MinItemsTest < Minitest::Test def test_minitems_nils schema = { "type" => "array", "minItems" => 1, "items" => { "type" => "object" } } - data = [nil] errors = JSON::Validator.fully_validate(schema, [nil]) assert_equal(errors.length, 1) diff --git a/test/test_one_of.rb b/test/test_one_of.rb index 25580232..670f4d42 100644 --- a/test/test_one_of.rb +++ b/test/test_one_of.rb @@ -1,15 +1,12 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) -class OneOfTest < Test::Unit::TestCase +class OneOfTest < Minitest::Test def test_one_of_links_schema - schema = File.join(File.dirname(__FILE__),"schemas/one_of_ref_links_schema.json") - data = File.join(File.dirname(__FILE__),"data/one_of_ref_links_data.json") - errors = JSON::Validator.fully_validate(schema,data, :errors_as_objects => true) - assert(errors.empty?, errors.map{|e| e[:message] }.join("\n")) + schema = schema_fixture_path('one_of_ref_links_schema.json') + data = data_fixture_path('one_of_ref_links_data.json') + assert_valid schema, data end - def test_one_of_with_string_patterns schema = { "$schema" => "http://json-schema.org/draft-04/schema#", @@ -26,17 +23,10 @@ def test_one_of_with_string_patterns ] } - data = {"a" => "foo"} - assert(JSON::Validator.validate(schema,data)) - - data = {"a" => "foobar"} - assert(!JSON::Validator.validate(schema,data)) - - data = {"a" => "baz"} - assert(JSON::Validator.validate(schema,data)) - - data = {"a" => 5} - assert(!JSON::Validator.validate(schema,data)) + assert_valid schema, { "a" => "foo" } + refute_valid schema, { "a" => "foobar" } + assert_valid schema, { "a" => "baz" } + refute_valid schema, { "a" => 5 } end end diff --git a/test/test_ruby_schema.rb b/test/test_ruby_schema.rb index a823465f..7693b336 100644 --- a/test/test_ruby_schema.rb +++ b/test/test_ruby_schema.rb @@ -1,7 +1,6 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) -class RubySchemaTest < Test::Unit::TestCase +class RubySchemaTest < Minitest::Test def test_string_keys schema = { "type" => 'object', @@ -12,11 +11,7 @@ def test_string_keys } } - data = { - "a" => 5 - } - - assert(JSON::Validator.validate(schema, data)) + assert_valid schema, { "a" => 5 } end def test_symbol_keys @@ -29,11 +24,7 @@ def test_symbol_keys } } - data = { - :a => 5 - } - - assert(JSON::Validator.validate(schema, data)) + assert_valid schema, { :a => 5 } end def test_symbol_keys_in_hash_within_array @@ -63,6 +54,6 @@ def test_symbol_keys_in_hash_within_array ] } - assert(JSON::Validator.validate(schema, data, :validate_schema => true)) + assert_valid schema, data, :validate_schema => true end end diff --git a/test/test_schema_type_attribute.rb b/test/test_schema_type_attribute.rb index 0f612c9b..5b446e94 100644 --- a/test/test_schema_type_attribute.rb +++ b/test/test_schema_type_attribute.rb @@ -1,7 +1,6 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) -class TestSchemaTypeAttribute < Test::Unit::TestCase +class TestSchemaTypeAttribute < Minitest::Test def test_type_of_data assert_equal(type_of_data(String.new), 'string') assert_equal(type_of_data(Numeric.new), 'number') diff --git a/test/test_schema_validation.rb b/test/test_schema_validation.rb index 9474b53e..940715e4 100644 --- a/test/test_schema_validation.rb +++ b/test/test_schema_validation.rb @@ -1,8 +1,7 @@ -require 'test/unit' +require File.expand_path('../test_helper', __FILE__) require 'tmpdir' -require File.dirname(__FILE__) + '/../lib/json-schema' -class JSONSchemaValidation < Test::Unit::TestCase +class JSONSchemaValidation < Minitest::Test def valid_schema_v3 { "$schema" => "http://json-schema.org/draft-03/schema#", @@ -135,7 +134,7 @@ def test_validate_just_schema_draft03 errors = JSON::Validator.fully_validate_schema(invalid_schema_v3, :version => :draft3) assert_equal 1, errors.size - assert_match /the property .*required.*did not match/i, errors.first + assert_match(/the property .*required.*did not match/i, errors.first) end @@ -151,7 +150,7 @@ def test_validate_just_schema_draft04 errors = JSON::Validator.fully_validate_schema(invalid_schema_v4, :version => :draft4) assert_equal 1, errors.size - assert_match /the property .*required.*did not match/i, errors.first + assert_match(/the property .*required.*did not match/i, errors.first) end def test_validate_schema_3_without_version_option diff --git a/test/test_stringify.rb b/test/test_stringify.rb index f77b534d..99994373 100644 --- a/test/test_stringify.rb +++ b/test/test_stringify.rb @@ -1,7 +1,6 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/json-schema' +require File.expand_path('../test_helper', __FILE__) -class StringifyTest < Test::Unit::TestCase +class StringifyTest < Minitest::Test def test_stringify_on_hash hash = { :a => 'foo',