From 640a4c6687f946e7fc461260927ac4a2baa893af Mon Sep 17 00:00:00 2001 From: Tim Meusel Date: Sat, 18 Feb 2023 19:06:15 +0100 Subject: [PATCH] rubocop: cleanup whitespace --- .rubocop.yml | 2 + .rubocop_todo.yml | 26 ++-- Gemfile | 1 + test/all_of_ref_schema_test.rb | 2 + test/any_of_ref_schema_test.rb | 2 + test/caching_test.rb | 6 + test/common_test_suite_test.rb | 1 + test/custom_format_test.rb | 25 ++-- test/draft1_test.rb | 12 ++ test/draft2_test.rb | 7 ++ test/draft3_test.rb | 39 +++++- test/draft4_test.rb | 142 +++++++++++++++++++++- test/draft6_test.rb | 6 + test/extends_nested_test.rb | 5 + test/files_test.rb | 4 + test/fragment_resolution_test.rb | 1 + test/fragment_validation_with_ref_test.rb | 2 + test/full_validation_test.rb | 71 ++++++----- test/initialize_data_test.rb | 23 ++++ test/list_option_test.rb | 2 + test/merge_missing_values_test.rb | 10 +- test/min_items_test.rb | 3 +- test/one_of_test.rb | 2 + test/relative_definition_test.rb | 1 + test/schema_reader_test.rb | 2 + test/schema_validation_test.rb | 19 ++- test/stringify_test.rb | 5 + test/support/enum_validation.rb | 20 +++ test/support/strict_validation.rb | 17 +++ test/support/test_helper.rb | 4 +- test/support/type_validation.rb | 4 + test/type_attribute_test.rb | 16 +-- test/uri_parsing_test.rb | 4 + test/uri_util_test.rb | 7 ++ test/validator_schema_reader_test.rb | 4 + 35 files changed, 428 insertions(+), 69 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 65de9ee5..54e8f31e 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -5,6 +5,8 @@ require: - rubocop-rake - rubocop-rspec - rubocop-performance + - rubocop-minitest + AllCops: TargetRubyVersion: 2.7 NewCops: enable diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 0bc713da..00151004 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,12 +1,12 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-02-18 18:00:43 UTC using RuboCop version 1.45.1. +# on 2023-02-18 18:13:01 UTC using RuboCop version 1.45.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 2 +# Offense count: 3 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: TreatCommentsAsGroupSeparators, ConsiderPunctuation, Include. # Include: **/*.gemfile, **/Gemfile, **/gems.rb @@ -38,14 +38,6 @@ Layout/MultilineMethodCallBraceLayout: Exclude: - 'test/common_test_suite_test.rb' -# Offense count: 8 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyleForEmptyBraces. -# SupportedStyles: space, no_space, compact -# SupportedStylesForEmptyBraces: space, no_space -Layout/SpaceInsideHashLiteralBraces: - EnforcedStyle: space - # Offense count: 1 # Configuration parameters: AllowedMethods, AllowedPatterns. Lint/AmbiguousBlockAssociation: @@ -184,6 +176,20 @@ Metrics/ParameterLists: Metrics/PerceivedComplexity: Max: 20 +# Offense count: 63 +Minitest/MultipleAssertions: + Max: 25 + +# Offense count: 53 +# This cop supports unsafe autocorrection (--autocorrect-all). +Minitest/RefuteFalse: + Exclude: + - 'test/custom_format_test.rb' + - 'test/draft3_test.rb' + - 'test/draft4_test.rb' + - 'test/schema_validation_test.rb' + - 'test/support/strict_validation.rb' + # Offense count: 1 Naming/AccessorMethodName: Exclude: diff --git a/Gemfile b/Gemfile index fb45bd5d..edc9f054 100644 --- a/Gemfile +++ b/Gemfile @@ -17,4 +17,5 @@ group :tests do gem 'rubocop-rspec', '~> 2.18.1' gem 'rubocop-rake', '~> 0.6.0' gem 'rubocop-performance', '~> 1.16.0' + gem 'rubocop-minitest', '~> 0.27.0' end diff --git a/test/all_of_ref_schema_test.rb b/test/all_of_ref_schema_test.rb index 42530158..2d5d7d6d 100644 --- a/test/all_of_ref_schema_test.rb +++ b/test/all_of_ref_schema_test.rb @@ -20,6 +20,7 @@ def test_all_of_ref_schema_succeeds def test_all_of_ref_subschema_errors 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') assert_match(%r{the property '#/name' of type string did not match the following type: integer}i, nested_errors[:allof_0][0][:message]) end @@ -30,6 +31,7 @@ def test_all_of_ref_message - allOf #0: - The property '#/name' of type string did not match the following type: integer"'' + assert_equal(expected_message, errors[0]) end diff --git a/test/any_of_ref_schema_test.rb b/test/any_of_ref_schema_test.rb index 7e011ede..ffb7dbbf 100644 --- a/test/any_of_ref_schema_test.rb +++ b/test/any_of_ref_schema_test.rb @@ -13,6 +13,7 @@ def test_any_of_ref_subschema_errors data = %({"names": ["jack"]}) errors = JSON::Validator.fully_validate(schema, data, errors_as_objects: true) nested_errors = errors[0][:errors] + assert_equal(%i[anyof_0 anyof_1 anyof_2], nested_errors.keys, 'should have nested errors for each anyOf subschema') assert_match(%r{the property '#/names/0' value "jack" did not match the regex 'john'}i, nested_errors[:anyof_0][0][:message]) assert_match(%r{the property '#/names/0' value "jack" did not match the regex 'jane'}i, nested_errors[:anyof_1][0][:message]) @@ -30,6 +31,7 @@ def test_any_of_ref_message - The property '#/names/0' value \"jack\" did not match the regex 'jane' - anyOf #2: - The property '#/names/0' value \"jack\" did not match the regex 'jimmy'"'' + assert_equal(expected_message, errors[0]) end end diff --git a/test/caching_test.rb b/test/caching_test.rb index d610d8c7..7516a9db 100644 --- a/test/caching_test.rb +++ b/test/caching_test.rb @@ -14,17 +14,21 @@ def teardown def test_caching set_schema('type' => 'string') + assert_valid(schema_path, 'foo', clear_cache: false) set_schema('type' => 'number') + refute_valid(schema_path, 123) end def test_clear_cache set_schema('type' => 'string') + assert_valid(schema_path, 'foo', clear_cache: true) set_schema('type' => 'number') + assert_valid(schema_path, 123) end @@ -34,9 +38,11 @@ def test_cache_schemas end set_schema('type' => 'string') + assert_valid(schema_path, 'foo', clear_cache: false) set_schema('type' => 'number') + assert_valid(schema_path, 123) ensure suppress_warnings do diff --git a/test/common_test_suite_test.rb b/test/common_test_suite_test.rb index a6a2374f..8a81e5be 100644 --- a/test/common_test_suite_test.rb +++ b/test/common_test_suite_test.rb @@ -47,6 +47,7 @@ def self.skip?(current_test, file_path) validate_schema: true, version: version, ) + assert_equal t['valid'], errors.empty?, "Common test suite case failed: #{err_id}" end end diff --git a/test/custom_format_test.rb b/test/custom_format_test.rb index ce887925..6c6c56a7 100644 --- a/test/custom_format_test.rb +++ b/test/custom_format_test.rb @@ -38,35 +38,40 @@ def setup def test_single_registration @all_versions.each do |version| - assert(JSON::Validator.validator_for_name(version).formats['custom'].nil?, "Format 'custom' for #{version || 'default'} should be nil") + assert_nil(JSON::Validator.validator_for_name(version).formats['custom'], "Format 'custom' for #{version || 'default'} should be nil") JSON::Validator.register_format_validator('custom', @format_proc, [version]) + assert(JSON::Validator.validator_for_name(version).formats['custom'].is_a?(JSON::Schema::CustomFormat), "Format 'custom' should be registered for #{version || 'default'}") (@all_versions - [version]).each do |other_version| - assert(JSON::Validator.validator_for_name(other_version).formats['custom'].nil?, "Format 'custom' should still be nil for #{other_version || 'default'}") + assert_nil(JSON::Validator.validator_for_name(other_version).formats['custom'], "Format 'custom' should still be nil for #{other_version || 'default'}") end JSON::Validator.deregister_format_validator('custom', [version]) - assert(JSON::Validator.validator_for_name(version).formats['custom'].nil?, "Format 'custom' should be deregistered for #{version || 'default'}") + + assert_nil(JSON::Validator.validator_for_name(version).formats['custom'], "Format 'custom' should be deregistered for #{version || 'default'}") end end def test_register_for_all_by_default JSON::Validator.register_format_validator('custom', @format_proc) + @all_versions.each do |version| assert(JSON::Validator.validator_for_name(version).formats['custom'].is_a?(JSON::Schema::CustomFormat), "Format 'custom' should be registered for #{version || 'default'}") end JSON::Validator.restore_default_formats + @all_versions.each do |version| - assert(JSON::Validator.validator_for_name(version).formats['custom'].nil?, "Format 'custom' should still be nil for #{version || 'default'}") + assert_nil(JSON::Validator.validator_for_name(version).formats['custom'], "Format 'custom' should still be nil for #{version || 'default'}") end end def test_multi_registration unregistered_version = @all_versions.delete('draft1') JSON::Validator.register_format_validator('custom', @format_proc, @all_versions) + @all_versions.each do |version| assert(JSON::Validator.validator_for_name(version).formats['custom'].is_a?(JSON::Schema::CustomFormat), "Format 'custom' should be registered for #{version || 'default'}") end - assert(JSON::Validator.validator_for_name(unregistered_version).formats['custom'].nil?, "Format 'custom' should still be nil for #{unregistered_version}") + assert_nil(JSON::Validator.validator_for_name(unregistered_version).formats['custom'], "Format 'custom' should still be nil for #{unregistered_version}") end def test_format_validation @@ -81,18 +86,22 @@ def test_format_validation JSON::Validator.register_format_validator('custom', @format_proc, [version]) data['a'] = '42' + assert(JSON::Validator.validate(schema, data), "#{prefix} succeeds with 'custom' format validator and correct data") data['a'] = '23' + assert(!JSON::Validator.validate(schema, data), "#{prefix} fails with 'custom' format validator and wrong data") errors = JSON::Validator.fully_validate(schema, data) - assert_equal(errors.count, 1) + + assert_equal(1, errors.count) assert_match(%r{The property '#/a' must be 42 in schema}, errors.first, "#{prefix} records format error") data['a'] = 23 errors = JSON::Validator.fully_validate(schema, data) - assert_equal(errors.count, 1) + + assert_equal(1, errors.count) assert_match(%r{The property '#/a' of type integer did not match the following type: string}i, errors.first, "#{prefix} records no format error on type mismatch") end end @@ -109,6 +118,7 @@ def test_override_default_format assert(JSON::Validator.validate(schema, data), "#{prefix} succeeds for default format with correct data") data['a'] = 'no_ip6_address' + assert(!JSON::Validator.validate(schema, data), "#{prefix} fails for default format and wrong data") data['a'] = '42' @@ -117,6 +127,7 @@ def test_override_default_format JSON::Validator.deregister_format_validator('ipv6', [version]) data['a'] = '2001:db8:85a3:0:0:8a2e:370:7334' + assert(JSON::Validator.validate(schema, data), "#{prefix} restores the default format on deregistration") end end diff --git a/test/draft1_test.rb b/test/draft1_test.rb index 75f3b820..8c60ec01 100644 --- a/test/draft1_test.rb +++ b/test/draft1_test.rb @@ -43,6 +43,7 @@ def test_optional refute_valid schema, data data['a'] = 'Hello' + assert_valid schema, data schema = { @@ -52,6 +53,7 @@ def test_optional } data = {} + assert_valid schema, data end @@ -68,20 +70,25 @@ def test_max_decimal } data['a'] = 3.35 + assert_valid schema, data data['a'] = 3.455 + refute_valid schema, data schema['properties']['a']['maxDecimal'] = 0 data['a'] = 4.0 + refute_valid schema, data data['a'] = 'boo' + assert_valid schema, data data['a'] = 5 + assert_valid schema, data end @@ -98,19 +105,24 @@ def test_disallow } data['a'] = 'string' + assert_valid schema, data data['a'] = 5 + refute_valid schema, data schema['properties']['a']['disallow'] = %w[integer string] data['a'] = 'string' + refute_valid schema, data data['a'] = 5 + refute_valid schema, data data['a'] = false + assert_valid schema, data end diff --git a/test/draft2_test.rb b/test/draft2_test.rb index 9bc1d2db..f09a91de 100644 --- a/test/draft2_test.rb +++ b/test/draft2_test.rb @@ -49,6 +49,7 @@ def test_optional refute_valid schema, data data['a'] = 'Hello' + assert_valid schema, data schema = { @@ -58,6 +59,7 @@ def test_optional } data = {} + assert_valid schema, data end @@ -74,19 +76,24 @@ def test_disallow } data['a'] = 'string' + assert_valid schema, data data['a'] = 5 + refute_valid schema, data schema['properties']['a']['disallow'] = %w[integer string] data['a'] = 'string' + refute_valid schema, data data['a'] = 5 + refute_valid schema, data data['a'] = false + assert_valid schema, data end diff --git a/test/draft3_test.rb b/test/draft3_test.rb index 9ea06e35..e64bf38c 100644 --- a/test/draft3_test.rb +++ b/test/draft3_test.rb @@ -68,10 +68,12 @@ def test_types data['a'] = [{ 'b' => 5 }, { 'c' => 'foo' }] errors = JSON::Validator.fully_validate(schema, data) - assert(errors.empty?, errors.join("\n")) + + assert_empty(errors, errors.join("\n")) # This should actually pass, because this matches the first schema in the union data['a'] << { 'c' => false } + assert_valid schema, data end @@ -87,6 +89,7 @@ def test_required refute_valid schema, data data['a'] = 'Hello' + assert_valid schema, data schema = { @@ -97,6 +100,7 @@ def test_required } data = {} + assert_valid schema, data end @@ -110,12 +114,15 @@ def test_strict_properties_required_props } data = { 'a' => 'a' } + assert(JSON::Validator.validate(schema, data, strict: true)) data = { 'b' => 'b' } + assert(!JSON::Validator.validate(schema, data, strict: true)) data = { 'a' => 'a', 'b' => 'b' } + assert(JSON::Validator.validate(schema, data, strict: true)) end @@ -130,26 +137,31 @@ def test_strict_properties_additional_props } data = { 'a' => 'a' } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(!JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'b' => 'b' } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(!JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b' } + assert(JSON::Validator.validate(schema, data, strict: true)) assert(JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b', 'c' => 'c' } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(!JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(!JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b', 'c' => 3 } + assert(JSON::Validator.validate(schema, data, strict: true)) assert(JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) @@ -166,36 +178,43 @@ def test_strict_properties_pattern_props } data = { 'a' => 'a' } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(!JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'b' => 'b' } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(!JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b' } + assert(JSON::Validator.validate(schema, data, strict: true)) assert(JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b', 'c' => 'c' } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(!JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b', 'c' => 3 } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(!JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b', '23 taco' => 3 } + assert(JSON::Validator.validate(schema, data, strict: true)) assert(JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b', '23 taco' => 'cheese' } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(!JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(!JSON::Validator.validate(schema, data, noAdditionalProperties: true)) @@ -215,19 +234,24 @@ def test_disallow } data['a'] = 'string' + assert_valid schema, data data['a'] = 5 + refute_valid schema, data schema['properties']['a']['disallow'] = %w[integer string] data['a'] = 'string' + refute_valid schema, data data['a'] = 5 + refute_valid schema, data data['a'] = false + assert_valid schema, data end @@ -266,13 +290,16 @@ def test_list_option } data = [{ 'a' => 1 }, { 'a' => 2 }, { 'a' => 3 }] + assert(JSON::Validator.validate(schema, data, list: true)) refute_valid schema, data data = { 'a' => 1 } + assert(!JSON::Validator.validate(schema, data, list: true)) data = [{ 'a' => 1 }, { 'b' => 2 }, { 'a' => 3 }] + assert(!JSON::Validator.validate(schema, data, list: true)) end @@ -349,12 +376,14 @@ def test_schema } data = { 'a' => 'taco' } + assert(!JSON::Validator.validate(schema, data)) schema = { '$schema' => 'http://json-schema.org/draft-03/schema#', 'type' => 'object', } + assert_valid schema, data end @@ -372,8 +401,10 @@ def test_dependency } data = { 'a' => 1, 'b' => 2 } + assert_valid schema, data data = { 'a' => 1 } + refute_valid schema, data schema = { @@ -390,8 +421,10 @@ def test_dependency } data = { 'a' => 1, 'c' => 2 } + refute_valid schema, data data = { 'a' => 1, 'b' => 2, 'c' => 3 } + assert_valid schema, data end @@ -406,6 +439,7 @@ def test_default } data = { b: 2 } + assert_valid schema, data assert_nil(data['a']) assert(JSON::Validator.validate(schema, data, insert_defaults: true)) @@ -422,6 +456,7 @@ def test_default } data = { b: 2 } + refute_valid schema, data assert_nil(data['a']) assert(JSON::Validator.validate(schema, data, insert_defaults: true)) @@ -438,6 +473,7 @@ def test_default } data = { b: 2 } + refute_valid schema, data assert_nil(data['a']) assert(!JSON::Validator.validate(schema, data, insert_defaults: true)) @@ -454,6 +490,7 @@ def test_default } data = { b: 2 } + assert_valid schema, data assert_nil(data['a']) assert(!JSON::Validator.validate(schema, data, insert_defaults: true)) diff --git a/test/draft4_test.rb b/test/draft4_test.rb index 06d3c1ad..e6711b59 100644 --- a/test/draft4_test.rb +++ b/test/draft4_test.rb @@ -167,6 +167,7 @@ def test_required refute_valid schema, data data['a'] = 'Hello' + assert_valid schema, data schema = { @@ -177,6 +178,7 @@ def test_required } data = {} + assert_valid schema, data end @@ -200,6 +202,102 @@ def test_max_properties refute_valid schema, { 'a' => 1, 'b' => 2, 'c' => 3 } end + def test_strict_properties + schema = { + '$schema' => 'http://json-schema.org/draft-04/schema#', + 'properties' => { + 'a' => { 'type' => 'string' }, + 'b' => { 'type' => 'string' }, + }, + } + + data = { 'a' => 'a' } + + assert(!JSON::Validator.validate(schema, data, strict: true)) + + data = { 'b' => 'b' } + + assert(!JSON::Validator.validate(schema, data, strict: true)) + + data = { 'a' => 'a', 'b' => 'b' } + + assert(JSON::Validator.validate(schema, data, strict: true)) + + data = { 'a' => 'a', 'b' => 'b', 'c' => 'c' } + + assert(!JSON::Validator.validate(schema, data, strict: true)) + end + + def test_strict_properties_additional_props + schema = { + '$schema' => 'http://json-schema.org/draft-04/schema#', + 'properties' => { + 'a' => { 'type' => 'string' }, + 'b' => { 'type' => 'string' }, + }, + 'additionalProperties' => { 'type' => 'integer' }, + } + + data = { 'a' => 'a' } + + assert(!JSON::Validator.validate(schema, data, strict: true)) + + data = { 'b' => 'b' } + + assert(!JSON::Validator.validate(schema, data, strict: true)) + + data = { 'a' => 'a', 'b' => 'b' } + + assert(JSON::Validator.validate(schema, data, strict: true)) + + data = { 'a' => 'a', 'b' => 'b', 'c' => 'c' } + + assert(!JSON::Validator.validate(schema, data, strict: true)) + + data = { 'a' => 'a', 'b' => 'b', 'c' => 3 } + + assert(JSON::Validator.validate(schema, data, strict: true)) + end + + def test_strict_properties_pattern_props + schema = { + '$schema' => 'http://json-schema.org/draft-03/schema#', + 'properties' => { + 'a' => { 'type' => 'string' }, + 'b' => { 'type' => 'string' }, + }, + 'patternProperties' => { '\\d+ taco' => { 'type' => 'integer' } }, + } + + data = { 'a' => 'a' } + + assert(!JSON::Validator.validate(schema, data, strict: true)) + + data = { 'b' => 'b' } + + assert(!JSON::Validator.validate(schema, data, strict: true)) + + data = { 'a' => 'a', 'b' => 'b' } + + assert(JSON::Validator.validate(schema, data, strict: true)) + + data = { 'a' => 'a', 'b' => 'b', 'c' => 'c' } + + assert(!JSON::Validator.validate(schema, data, strict: true)) + + data = { 'a' => 'a', 'b' => 'b', 'c' => 3 } + + assert(!JSON::Validator.validate(schema, data, strict: true)) + + data = { 'a' => 'a', 'b' => 'b', '23 taco' => 3 } + + assert(JSON::Validator.validate(schema, data, strict: true)) + + data = { 'a' => 'a', 'b' => 'b', '23 taco' => 'cheese' } + + assert(!JSON::Validator.validate(schema, data, strict: true)) + end + def test_list_option schema = { '$schema' => 'http://json-schema.org/draft-04/schema#', @@ -209,13 +307,16 @@ def test_list_option } data = [{ 'a' => 1 }, { 'a' => 2 }, { 'a' => 3 }] + assert(JSON::Validator.validate(schema, data, list: true)) refute_valid schema, data data = { 'a' => 1 } + assert(!JSON::Validator.validate(schema, data, list: true)) data = [{ 'a' => 1 }, { 'b' => 2 }, { 'a' => 3 }] + assert(!JSON::Validator.validate(schema, data, list: true)) end @@ -275,7 +376,7 @@ def test_default_with_anyof data = {} assert(JSON::Validator.validate(schema, data, insert_defaults: true, strict: true)) - assert(data['foo'] == 'view') + assert_equal('view', data['foo']) end def test_default_with_strict_and_oneof @@ -378,12 +479,14 @@ def test_schema } data = { 'a' => 'taco' } + assert(!JSON::Validator.validate(schema, data)) schema = { '$schema' => 'http://json-schema.org/draft-04/schema#', 'type' => 'object', } + assert_valid schema, data end @@ -401,8 +504,10 @@ def test_dependency } data = { 'a' => 1, 'b' => 2 } + assert_valid schema, data data = { 'a' => 1 } + refute_valid schema, data schema = { @@ -419,8 +524,10 @@ def test_dependency } data = { 'a' => 1, 'c' => 2 } + refute_valid schema, data data = { 'a' => 1, 'b' => 2, 'c' => 3 } + assert_valid schema, data end @@ -445,8 +552,10 @@ def test_schema_dependency 'name' => 'John Doe', 'credit_card' => 5555555555555555, } + assert(!JSON::Validator.validate(schema, data), 'test schema dependency with invalid data') data['billing_address'] = 'Somewhere over the rainbow' + assert(JSON::Validator.validate(schema, data), 'test schema dependency with valid data') end @@ -461,6 +570,7 @@ def test_default } data = { b: 2 } + assert_valid schema, data assert_nil(data['a']) assert(JSON::Validator.validate(schema, data, insert_defaults: true)) @@ -478,6 +588,7 @@ def test_default } data = { b: 2 } + refute_valid schema, data assert_nil(data['a']) assert(JSON::Validator.validate(schema, data, insert_defaults: true)) @@ -495,6 +606,7 @@ def test_default } data = { b: 2 } + refute_valid schema, data assert_nil(data['a']) assert(!JSON::Validator.validate(schema, data, insert_defaults: true)) @@ -511,6 +623,7 @@ def test_default } data = { b: 2 } + assert_valid schema, data assert_nil(data['a']) assert(!JSON::Validator.validate(schema, data, insert_defaults: true)) @@ -530,6 +643,7 @@ def test_boolean_false_default } data = { b: 2 } + refute_valid schema, data assert_nil(data['a']) assert(JSON::Validator.validate(schema, data, insert_defaults: true)) @@ -552,15 +666,19 @@ def test_all_of } data = { 'a' => 'hello', 'b' => 5 } + assert_valid schema, data data = { 'a' => 'hello' } + assert_valid schema, data data = { 'a' => 'hello', 'b' => 'taco' } + refute_valid schema, data data = { 'b' => 5 } + refute_valid schema, data end @@ -579,18 +697,23 @@ def test_any_of } data = { 'a' => 'hello', 'b' => 5 } + assert_valid schema, data data = { 'a' => 'hello' } + assert_valid schema, data data = { 'a' => 'hello', 'b' => 'taco' } + assert_valid schema, data data = { 'b' => 5 } + assert_valid schema, data data = { 'a' => 5, 'b' => 'taco' } + refute_valid schema, data end @@ -609,19 +732,24 @@ def test_one_of } data = { 'a' => 'hello', 'b' => 5 } + refute_valid schema, data # This passes because b is not required, thus matches both schemas data = { 'a' => 'hello' } + refute_valid schema, data data = { 'a' => 'hello', 'b' => 'taco' } + assert_valid schema, data data = { 'b' => 5 } + assert_valid schema, data data = { 'a' => 5, 'b' => 'taco' } + refute_valid schema, data end @@ -635,12 +763,15 @@ def test_not } data = { 'a' => 1 } + assert_valid schema, data data = { 'a' => 'hi!' } + refute_valid schema, data data = { 'a' => true } + refute_valid schema, data # Sub-schema not @@ -662,18 +793,23 @@ def test_not } data = { 'a' => 1 } + assert_valid schema, data data = { 'a' => 'hi!' } + refute_valid schema, data data = { 'a' => true } + refute_valid schema, data data = { 'a' => { 'b' => true } } + refute_valid schema, data data = { 'a' => { 'b' => 5 } } + assert_valid schema, data end @@ -688,10 +824,12 @@ def test_not_fully_validate data = { 'a' => 1 } errors = JSON::Validator.fully_validate(schema, data) + assert_equal(0, errors.length) data = { 'a' => 'taco' } errors = JSON::Validator.fully_validate(schema, data) + assert_equal(1, errors.length) end @@ -710,9 +848,11 @@ def test_definitions } data = [1, 2, 3] + assert_valid schema, data data = [-1, 2, 3] + refute_valid schema, data end end diff --git a/test/draft6_test.rb b/test/draft6_test.rb index b1eba824..65285e9a 100644 --- a/test/draft6_test.rb +++ b/test/draft6_test.rb @@ -17,9 +17,11 @@ def test_const_attribute } data = { a: 'foo', b: 6 } + assert_valid schema, data data = { a: 6, b: 'foo' } + refute_valid schema, data end @@ -30,9 +32,11 @@ def test_property_names } data = { 'foo' => 'value' } + assert_valid schema, data data = { 'bar' => 'value' } + refute_valid schema, data schema = { @@ -41,9 +45,11 @@ def test_property_names } data = {} + assert_valid schema, data data = { 'foo' => 'value' } + refute_valid schema, data end end diff --git a/test/extends_nested_test.rb b/test/extends_nested_test.rb index 58cbc1d9..3413497a 100644 --- a/test/extends_nested_test.rb +++ b/test/extends_nested_test.rb @@ -9,6 +9,7 @@ class ExtendsNestedTest < Minitest::Test def test_valid_outer ALL_SCHEMAS.each do |file| path = schema_fixture_path(file) + assert_valid path, { 'outerC' => true }, {}, 'Outer defn is broken, maybe the outer extends overrode it' end end @@ -16,6 +17,7 @@ def test_valid_outer def test_valid_outer_extended ALL_SCHEMAS.each do |file| path = schema_fixture_path(file) + assert_valid path, { 'innerA' => true }, {}, "Extends at the root level isn't working" end end @@ -23,6 +25,7 @@ def test_valid_outer_extended def test_valid_inner ALL_SCHEMAS.each do |file| path = schema_fixture_path(file) + assert_valid path, { 'outerB' => [{ 'innerA' => true }] }, {}, "Extends isn't working in the array element defn" end end @@ -30,12 +33,14 @@ def test_valid_inner def test_invalid_inner ALL_SCHEMAS.each do |file| path = schema_fixture_path(file) + refute_valid path, { 'outerB' => [{ 'whaaaaat' => true }] }, {}, "Array element defn allowing anything when it should only allow what's in inner.schema" end end def test_invalid_outer path = schema_fixture_path(ADDITIONAL_PROPERTIES) + refute_valid path, { 'whaaaaat' => true }, {}, "Outer defn allowing anything when it shouldn't" end end diff --git a/test/files_test.rb b/test/files_test.rb index addcc32d..c18f75ce 100644 --- a/test/files_test.rb +++ b/test/files_test.rb @@ -12,24 +12,28 @@ def test_schema_from_file def test_data_from_file_v3 schema = { '$schema' => 'http://json-schema.org/draft-03/schema#', 'type' => 'object', 'properties' => { 'a' => { 'type' => 'integer' } } } + 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_v3 schema = { '$schema' => 'http://json-schema.org/draft-03/schema#', 'type' => 'object', 'properties' => { 'a' => { 'type' => 'integer' } } } + assert_valid schema, '{"a": 5}', json: true refute_valid schema, '{"a": "poop"}', json: true end def test_data_from_file_v4 schema = { '$schema' => 'http://json-schema.org/draft-04/schema#', 'type' => 'object', 'properties' => { 'a' => { 'type' => 'integer' } } } + 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_v4 schema = { '$schema' => 'http://json-schema.org/draft-04/schema#', 'type' => 'object', 'properties' => { 'a' => { 'type' => 'integer' } } } + assert_valid schema, '{"a": 5}', json: true refute_valid schema, '{"a": "poop"}', json: true end diff --git a/test/fragment_resolution_test.rb b/test/fragment_resolution_test.rb index 51e3f99e..f94c30cc 100644 --- a/test/fragment_resolution_test.rb +++ b/test/fragment_resolution_test.rb @@ -16,6 +16,7 @@ def test_fragment_resolution } data = { 'b' => 5 } + refute_valid schema, data assert_valid schema, data, fragment: '#/properties/a' diff --git a/test/fragment_validation_with_ref_test.rb b/test/fragment_validation_with_ref_test.rb index e13f2bee..7d44c145 100644 --- a/test/fragment_validation_with_ref_test.rb +++ b/test/fragment_validation_with_ref_test.rb @@ -56,11 +56,13 @@ def whole_schema_with_array def test_validation_of_fragment data = [{ 'content' => 'ohai', 'author' => 'Bob' }] + assert_valid whole_schema, data, fragment: '#/definitions/posts' end def test_validation_of_fragment_with_array data = { 'content' => 'ohai', 'author' => 'Bob' } + assert_valid(whole_schema_with_array, data, fragment: '#/definitions/omg/links/0/schema',) end diff --git a/test/full_validation_test.rb b/test/full_validation_test.rb index 41e87bea..31093778 100644 --- a/test/full_validation_test.rb +++ b/test/full_validation_test.rb @@ -7,21 +7,20 @@ def test_full_validation 'type' => 'object', 'required' => ['b'], 'properties' => { - 'b' => { - }, + 'b' => {}, }, } errors = JSON::Validator.fully_validate(schema, data) - assert(errors.empty?) + + assert_empty(errors) data = { 'c' => 5 } schema = { 'type' => 'object', 'required' => ['b'], 'properties' => { - 'b' => { - }, + 'b' => {}, 'c' => { 'type' => 'string', }, @@ -29,7 +28,8 @@ def test_full_validation } errors = JSON::Validator.fully_validate(schema, data) - assert(errors.length == 2) + + assert_equal(2, errors.length) end def test_full_validation_with_instantiated_validator @@ -38,23 +38,22 @@ def test_full_validation_with_instantiated_validator 'type' => 'object', 'required' => ['b'], 'properties' => { - 'b' => { - }, + 'b' => {}, }, } validator = JSON::Validator.new(schema, { record_errors: true }) - assert(validator.validate(data).empty?) - assert(validator.validate(data).empty?) - assert(validator.validate(data).empty?) + + assert_empty(validator.validate(data)) + assert_empty(validator.validate(data)) + assert_empty(validator.validate(data)) data = { 'c' => 5 } schema = { 'type' => 'object', 'required' => ['b'], 'properties' => { - 'b' => { - }, + 'b' => {}, 'c' => { 'type' => 'string', }, @@ -62,9 +61,10 @@ def test_full_validation_with_instantiated_validator } validator = JSON::Validator.new(schema, { record_errors: true }) - assert(validator.validate(data).length == 2) - assert(validator.validate(data).length == 2) - assert(validator.validate(data).length == 2) + + assert_equal(2, validator.validate(data).length) + assert_equal(2, validator.validate(data).length) + assert_equal(2, validator.validate(data).length) end def test_full_validation_with_union_types @@ -79,7 +79,8 @@ def test_full_validation_with_union_types } errors = JSON::Validator.fully_validate(schema, data) - assert(errors.empty?) + + assert_empty(errors) schema = { 'type' => 'object', @@ -91,12 +92,14 @@ def test_full_validation_with_union_types } errors = JSON::Validator.fully_validate(schema, data) - assert(errors.empty?) + + assert_empty(errors) data = { 'b' => 'a string' } errors = JSON::Validator.fully_validate(schema, data) - assert(errors.length == 1) + + assert_equal(1, errors.length) schema = { '$schema' => 'http://json-schema.org/draft-03/schema#', @@ -124,17 +127,20 @@ def test_full_validation_with_union_types data = { 'b' => { 'c' => 'taco' } } errors = JSON::Validator.fully_validate(schema, data) - assert(errors.empty?) + + assert_empty(errors) data = { 'b' => { 'd' => 6 } } errors = JSON::Validator.fully_validate(schema, data) - assert(errors.empty?) + + assert_empty(errors) data = { 'b' => { 'c' => 6, 'd' => 'OH GOD' } } errors = JSON::Validator.fully_validate(schema, data) - assert(errors.length == 1) + + assert_equal(1, errors.length) end def test_full_validation_with_object_errors @@ -143,21 +149,20 @@ def test_full_validation_with_object_errors 'type' => 'object', 'required' => ['b'], 'properties' => { - 'b' => { - }, + 'b' => {}, }, } errors = JSON::Validator.fully_validate(schema, data, errors_as_objects: true) - assert(errors.empty?) + + assert_empty(errors) data = { 'c' => 5 } schema = { 'type' => 'object', 'required' => ['b'], 'properties' => { - 'b' => { - }, + 'b' => {}, 'c' => { 'type' => 'string', }, @@ -166,11 +171,11 @@ def test_full_validation_with_object_errors errors = JSON::Validator.fully_validate(schema, data, errors_as_objects: true) - assert(errors.length == 2) - assert(errors[0][:failed_attribute] == 'Required') - assert(errors[0][:fragment] == '#/') - assert(errors[1][:failed_attribute] == 'TypeV4') - assert(errors[1][:fragment] == '#/c') + assert_equal(2, errors.length) + assert_equal('Required', errors[0][:failed_attribute]) + assert_equal('#/', errors[0][:fragment]) + assert_equal('TypeV4', errors[1][:failed_attribute]) + assert_equal('#/c', errors[1][:fragment]) end def test_full_validation_with_nested_required_properties @@ -194,6 +199,7 @@ def test_full_validation_with_nested_required_properties data = { 'x' => { 'a' => 5, 'd' => 5, 'e' => 'what?' } } errors = JSON::Validator.fully_validate(schema, data, errors_as_objects: true) + assert_equal 2, errors.length assert_equal '#/x', errors[0][:fragment] assert_equal 'Required', errors[0][:failed_attribute] @@ -227,6 +233,7 @@ def test_full_validation_with_nested_required_propertiesin_array data = { 'x' => [missing_b, e_is_wrong_type] } errors = JSON::Validator.fully_validate(schema, data, errors_as_objects: true) + assert_equal 2, errors.length assert_equal '#/x/0', errors[0][:fragment] assert_equal 'Required', errors[0][:failed_attribute] diff --git a/test/initialize_data_test.rb b/test/initialize_data_test.rb index 9a531258..008d053e 100644 --- a/test/initialize_data_test.rb +++ b/test/initialize_data_test.rb @@ -158,10 +158,12 @@ def test_parse_character_string_with_instantiated_validator data = 'hello world' v = JSON::Validator.new(schema) + assert(v.validate(data)) assert(v.validate(data)) v = JSON::Validator.new(schema, { parse_data: false }) + assert(v.validate(data)) assert(v.validate(data)) @@ -179,6 +181,7 @@ def test_parse_integer_string_with_instantiated_validator data = '42' v = JSON::Validator.new(schema) + assert(v.validate(data)) assert(v.validate(data)) @@ -191,6 +194,7 @@ def test_parse_integer_string_with_instantiated_validator assert_raises(JSON::Schema::ValidationError) { v.validate(data) } v = JSON::Validator.new(schema, { json: true }) + assert(v.validate(data)) assert(v.validate(data)) @@ -204,6 +208,7 @@ def test_parse_hash_string_with_instantiated_validator data = '{"a": "b"}' v = JSON::Validator.new(schema) + assert(v.validate(data)) assert(v.validate(data)) @@ -212,6 +217,7 @@ def test_parse_hash_string_with_instantiated_validator assert_raises(JSON::Schema::ValidationError) { v.validate(data) } v = JSON::Validator.new(schema, { json: true }) + assert(v.validate(data)) assert(v.validate(data)) @@ -225,14 +231,17 @@ def test_parse_json_string_with_instantiated_validator data = '"hello world"' v = JSON::Validator.new(schema) + assert(v.validate(data)) assert(v.validate(data)) v = JSON::Validator.new(schema, { parse_data: false }) + assert(v.validate(data)) assert(v.validate(data)) v = JSON::Validator.new(schema, { json: true }) + assert(v.validate(data)) assert(v.validate(data)) @@ -246,10 +255,12 @@ def test_parse_plain_text_string_with_instantiated_validator data = 'kapow' v = JSON::Validator.new(schema) + assert(v.validate(data)) assert(v.validate(data)) v = JSON::Validator.new(schema, { parse_data: false }) + assert(v.validate(data)) assert(v.validate(data)) @@ -269,10 +280,12 @@ def test_parse_valid_uri_string_with_instantiated_validator stub_request(:get, 'foo.bar').to_return(body: '"hello world"', status: 200) v = JSON::Validator.new(schema) + assert(v.validate(data)) assert(v.validate(data)) v = JSON::Validator.new(schema, { parse_data: false }) + assert(v.validate(data)) assert(v.validate(data)) @@ -281,6 +294,7 @@ def test_parse_valid_uri_string_with_instantiated_validator assert_raises(JSON::Schema::JsonParseError) { v.validate(data) } v = JSON::Validator.new(schema, { uri: true }) + assert(v.validate(data)) assert(v.validate(data)) end @@ -292,10 +306,12 @@ def test_parse_invalid_uri_string_with_instantiated_validator stub_request(:get, 'foo.bar').to_timeout v = JSON::Validator.new(schema) + assert(v.validate(data)) assert(v.validate(data)) v = JSON::Validator.new(schema, { parse_data: false }) + assert(v.validate(data)) assert(v.validate(data)) @@ -305,6 +321,7 @@ def test_parse_invalid_uri_string_with_instantiated_validator assert(v.validate(data)) v = JSON::Validator.new(schema, { parse_data: false }) + assert(v.validate(data)) assert(v.validate(data)) @@ -322,10 +339,12 @@ def test_parse_invalid_scheme_string_with_instantiated_validator data = 'pick one: [1, 2, 3]' v = JSON::Validator.new(schema) + assert(v.validate(data)) assert(v.validate(data)) v = JSON::Validator.new(schema, { parse_data: false }) + assert(v.validate(data)) assert(v.validate(data)) @@ -343,10 +362,12 @@ def test_parse_integer_with_instantiated_validator data = 42 v = JSON::Validator.new(schema) + assert(v.validate(data)) assert(v.validate(data)) v = JSON::Validator.new(schema, { parse_data: false }) + assert(v.validate(data)) assert(v.validate(data)) @@ -364,10 +385,12 @@ def test_parse_hash_with_instantiated_validator data = { 'a' => 'b' } v = JSON::Validator.new(schema) + assert(v.validate(data)) assert(v.validate(data)) v = JSON::Validator.new(schema, { parse_data: false }) + assert(v.validate(data)) assert(v.validate(data)) diff --git a/test/list_option_test.rb b/test/list_option_test.rb index 5750e6b9..5a17860a 100644 --- a/test/list_option_test.rb +++ b/test/list_option_test.rb @@ -13,9 +13,11 @@ def test_list_option_reusing_schemas JSON::Validator.add_schema(schema) data = { 'a' => 1 } + assert_valid uri.to_s, data, clear_cache: false data = [{ 'a' => 1 }] + assert_valid uri.to_s, data, list: true end end diff --git a/test/merge_missing_values_test.rb b/test/merge_missing_values_test.rb index 21ec91db..a525e9a7 100644 --- a/test/merge_missing_values_test.rb +++ b/test/merge_missing_values_test.rb @@ -5,6 +5,7 @@ def test_merge_missing_values_for_string original = 'foo' updated = 'foo' JSON::Validator.merge_missing_values(updated, original) + assert_equal('foo', original) end @@ -12,20 +13,23 @@ def test_merge_missing_values_for_empty_array original = [] updated = [] JSON::Validator.merge_missing_values(updated, original) - assert_equal([], original) + + assert_empty(original) end def test_merge_missing_values_for_empty_hash original = {} updated = {} JSON::Validator.merge_missing_values(updated, original) - assert_equal({}, original) + + assert_empty(original) end def test_merge_missing_values_for_new_values original = { hello: 'world' } updated = { 'hello' => 'world', 'foo' => 'bar' } JSON::Validator.merge_missing_values(updated, original) + assert_equal({ :hello => 'world', 'foo' => 'bar' }, original) end @@ -33,6 +37,7 @@ def test_merge_missing_values_for_nested_array original = [:hello, 'world', 1, 2, 3, { :foo => :bar, 'baz' => 'qux' }] updated = ['hello', 'world', 1, 2, 3, { 'foo' => 'bar', 'baz' => 'qux', 'this_is' => 'new' }] JSON::Validator.merge_missing_values(updated, original) + assert_equal([:hello, 'world', 1, 2, 3, { :foo => :bar, 'baz' => 'qux', 'this_is' => 'new' }], original) end @@ -40,6 +45,7 @@ def test_merge_missing_values_for_nested_hash original = { hello: 'world', foo: ['bar', :baz, { uno: { due: 3 } }] } updated = { 'hello' => 'world', 'foo' => ['bar', 'baz', { 'uno' => { 'due' => 3, 'this_is' => 'new' } }], 'ack' => 'sed' } JSON::Validator.merge_missing_values(updated, original) + assert_equal({ :hello => 'world', :foo => ['bar', :baz, { uno: { :due => 3, 'this_is' => 'new' } }], 'ack' => 'sed' }, original) end end diff --git a/test/min_items_test.rb b/test/min_items_test.rb index b02bb3d5..853c7910 100644 --- a/test/min_items_test.rb +++ b/test/min_items_test.rb @@ -9,7 +9,8 @@ def test_minitems_nils } errors = JSON::Validator.fully_validate(schema, [nil]) - assert_equal(errors.length, 1) + + assert_equal(1, errors.length) assert(errors[0] !~ /minimum/) assert(errors[0] =~ /null/) end diff --git a/test/one_of_test.rb b/test/one_of_test.rb index de207189..a9a8c4dd 100644 --- a/test/one_of_test.rb +++ b/test/one_of_test.rb @@ -4,6 +4,7 @@ class OneOfTest < Minitest::Test def test_one_of_links_schema 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 @@ -47,6 +48,7 @@ def test_one_of_sub_errors errors = JSON::Validator.fully_validate(schema, { 'a' => 5 }, errors_as_objects: true) nested_errors = errors[0][:errors] + assert_equal(%i[oneof_0 oneof_1 oneof_2], nested_errors.keys, 'should have nested errors for each allOf subschema') assert_match(%r{the property '#/a' of type Integer did not match the following type: string}i, nested_errors[:oneof_0][0][:message]) assert_match(%r{the property '#/a' did not have a minimum value of 10, inclusively}i, nested_errors[:oneof_2][0][:message]) diff --git a/test/relative_definition_test.rb b/test/relative_definition_test.rb index a50379ca..391bc6f7 100644 --- a/test/relative_definition_test.rb +++ b/test/relative_definition_test.rb @@ -11,6 +11,7 @@ def test_definition_schema_with_special_characters def test_relative_definition schema = schema_fixture_path('relative_definition_schema.json') + assert_valid schema, { 'a' => 5 } refute_valid schema, { 'a' => 'foo' } end diff --git a/test/schema_reader_test.rb b/test/schema_reader_test.rb index f9ddfeca..a3413a04 100644 --- a/test/schema_reader_test.rb +++ b/test/schema_reader_test.rb @@ -27,11 +27,13 @@ def test_accept_all_files def test_refuse_all_uris reader = JSON::Schema::Reader.new(accept_uri: false) + refute reader.accept_uri?(Addressable::URI.parse('http://foo.com')) end def test_refuse_all_files reader = JSON::Schema::Reader.new(accept_file: false) + refute reader.accept_file?(Pathname.new('/foo/bar/baz')) end diff --git a/test/schema_validation_test.rb b/test/schema_validation_test.rb index 67a83b2b..a56aa0c4 100644 --- a/test/schema_validation_test.rb +++ b/test/schema_validation_test.rb @@ -31,8 +31,7 @@ def valid_schema_v4 '$schema' => 'http://json-schema.org/draft-04/schema#', 'type' => 'object', 'required' => ['b'], - 'properties' => { - }, + 'properties' => {}, } end @@ -41,8 +40,7 @@ def invalid_schema_v4 '$schema' => 'http://json-schema.org/draft-04/schema#', 'type' => 'object', 'required' => 'b', - 'properties' => { - }, + 'properties' => {}, } end @@ -124,36 +122,43 @@ def symbolized_schema def test_draft03_validation data = { 'b' => { 'a' => 5 } } + assert(JSON::Validator.validate(valid_schema_v3, data, validate_schema: true, version: :draft3)) assert(!JSON::Validator.validate(invalid_schema_v3, data, validate_schema: true, version: :draft3)) end def test_validate_just_schema_draft03 errors = JSON::Validator.fully_validate_schema(valid_schema_v3, version: :draft3) - assert_equal [], errors + + assert_empty errors 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) end def test_draft04_validation data = { 'b' => { 'a' => 5 } } + assert(JSON::Validator.validate(valid_schema_v4, data, validate_schema: true, version: :draft4)) assert(!JSON::Validator.validate(invalid_schema_v4, data, validate_schema: true, version: :draft4)) end def test_validate_just_schema_draft04 errors = JSON::Validator.fully_validate_schema(valid_schema_v4, version: :draft4) - assert_equal [], errors + + assert_empty errors 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) end def test_validate_schema_3_without_version_option data = { 'b' => { 'a' => 5 } } + assert(JSON::Validator.validate(valid_schema_v3, data, validate_schema: true)) assert(!JSON::Validator.validate(invalid_schema_v3, data, validate_schema: true)) end @@ -162,6 +167,7 @@ def test_schema_validation_from_different_directory Dir.mktmpdir do |tmpdir| Dir.chdir(tmpdir) do data = { 'b' => { 'a' => 5 } } + assert(JSON::Validator.validate(valid_schema_v4, data, validate_schema: true, version: :draft4)) assert(!JSON::Validator.validate(invalid_schema_v4, data, validate_schema: true, version: :draft4)) end @@ -179,6 +185,7 @@ def test_validate_schema_with_symbol_keys 'biography' => nil, 'demographic' => nil, } + assert(JSON::Validator.validate!(symbolized_schema, data, validate_schema: true)) end diff --git a/test/stringify_test.rb b/test/stringify_test.rb index 54517077..53f5827a 100644 --- a/test/stringify_test.rb +++ b/test/stringify_test.rb @@ -6,6 +6,7 @@ def test_stringify_on_hash :a => 'foo', 'b' => :bar, } + assert_equal({ 'a' => 'foo', 'b' => 'bar' }, JSON::Schema.stringify(hash), 'symbol keys should be converted to strings') end @@ -14,6 +15,7 @@ def test_stringify_on_array :a, 'b', ] + assert_equal(%w[a b], JSON::Schema.stringify(array), 'symbols in an array should be converted to strings') end @@ -22,6 +24,7 @@ def test_stringify_on_hash_of_arrays :a => [:foo], 'b' => :bar, } + assert_equal({ 'a' => ['foo'], 'b' => 'bar' }, JSON::Schema.stringify(hash), 'symbols in a nested array should be converted to strings') end @@ -32,6 +35,7 @@ def test_stringify_on_array_of_hashes b: :bar, }, ] + assert_equal(['a', { 'b' => 'bar' }], JSON::Schema.stringify(array), 'symbols keys in a nested hash should be converted to strings') end @@ -43,6 +47,7 @@ def test_stringify_on_hash_of_hashes }, }, } + assert_equal({ 'a' => { 'b' => { 'foo' => 'bar' } } }, JSON::Schema.stringify(hash), 'symbols in a nested hash of hashes should be converted to strings') end end diff --git a/test/support/enum_validation.rb b/test/support/enum_validation.rb index 2538f8bf..7118e361 100644 --- a/test/support/enum_validation.rb +++ b/test/support/enum_validation.rb @@ -8,6 +8,7 @@ def test_enum_optional } data = {} + assert_valid schema, data end end @@ -21,6 +22,7 @@ def test_enum_optional } data = {} + assert_valid schema, data end end @@ -34,18 +36,23 @@ def test_enum_general } data = { 'a' => 1 } + assert_valid schema, data data['a'] = 'boo' + assert_valid schema, data data['a'] = [1, 2, 3] + assert_valid schema, data data['a'] = { 'a' => 'b' } + assert_valid schema, data data['a'] = 'taco' + refute_valid schema, data end @@ -60,15 +67,19 @@ def test_enum_number_integer_includes_float } data = { 'a' => 0 } + assert_valid schema, data data['a'] = 0.0 + assert_valid schema, data data['a'] = 1 + assert_valid schema, data data['a'] = 1.0 + assert_valid schema, data end @@ -83,15 +94,19 @@ def test_enum_number_float_includes_integer } data = { 'a' => 0.0 } + assert_valid schema, data data['a'] = 0 + assert_valid schema, data data['a'] = 1.0 + assert_valid schema, data data['a'] = 1 + assert_valid schema, data end @@ -106,15 +121,19 @@ def test_enum_integer_excludes_float } data = { 'a' => 0 } + assert_valid schema, data data['a'] = 0.0 + refute_valid schema, data data['a'] = 1 + assert_valid schema, data data['a'] = 1.0 + refute_valid schema, data end @@ -125,6 +144,7 @@ def test_enum_with_schema_validation }, } data = { 'a' => 1 } + assert_valid(schema, data, validate_schema: true) end end diff --git a/test/support/strict_validation.rb b/test/support/strict_validation.rb index 8238aef9..b84f0394 100644 --- a/test/support/strict_validation.rb +++ b/test/support/strict_validation.rb @@ -9,21 +9,25 @@ def test_strict_properties } data = { 'a' => 'a' } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(!JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'b' => 'b' } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(!JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b' } + assert(JSON::Validator.validate(schema, data, strict: true)) assert(JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b', 'c' => 'c' } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(!JSON::Validator.validate(schema, data, noAdditionalProperties: true)) @@ -33,6 +37,7 @@ def test_strict_error_message schema = { type: 'object', properties: { a: { type: 'string' } } } data = { a: 'abc', b: 'abc' } errors = JSON::Validator.fully_validate(schema, data, strict: true) + assert_match("The property '#/' contained undefined properties: 'b' in schema", errors[0]) end @@ -47,26 +52,31 @@ def test_strict_properties_additional_props } data = { 'a' => 'a' } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(!JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'b' => 'b' } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(!JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b' } + assert(JSON::Validator.validate(schema, data, strict: true)) assert(JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b', 'c' => 'c' } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(!JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(!JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b', 'c' => 3 } + assert(JSON::Validator.validate(schema, data, strict: true)) assert(JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) @@ -82,36 +92,43 @@ def test_strict_properties_pattern_props } data = { 'a' => 'a' } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(!JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'b' => 'b' } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(!JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b' } + assert(JSON::Validator.validate(schema, data, strict: true)) assert(JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b', 'c' => 'c' } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(!JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b', 'c' => 3 } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(!JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b', '23 taco' => 3 } + assert(JSON::Validator.validate(schema, data, strict: true)) assert(JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(JSON::Validator.validate(schema, data, noAdditionalProperties: true)) data = { 'a' => 'a', 'b' => 'b', '23 taco' => 'cheese' } + assert(!JSON::Validator.validate(schema, data, strict: true)) assert(!JSON::Validator.validate(schema, data, allPropertiesRequired: true)) assert(!JSON::Validator.validate(schema, data, noAdditionalProperties: true)) diff --git a/test/support/test_helper.rb b/test/support/test_helper.rb index ab8c74c9..24ec07d8 100644 --- a/test/support/test_helper.rb +++ b/test/support/test_helper.rb @@ -56,11 +56,13 @@ def data_fixture_path(filename) def assert_valid(schema, data, options = {}, msg = "#{data.inspect} should be valid for schema:\n#{schema.inspect}") errors = validation_errors(schema, data, options) - assert_equal([], errors, msg) + + assert_empty(errors, msg) end def refute_valid(schema, data, options = {}, msg = "#{data.inspect} should be invalid for schema:\n#{schema.inspect}") errors = validation_errors(schema, data, options) + refute_equal([], errors, msg) end diff --git a/test/support/type_validation.rb b/test/support/type_validation.rb index 6b8757ed..44d99f4c 100644 --- a/test/support/type_validation.rb +++ b/test/support/type_validation.rb @@ -19,6 +19,7 @@ module SimpleTypeTests schema = { 'properties' => { 'a' => { 'type' => name } }, } + assert_valid schema, { 'a' => value } other_values.each do |other_value| @@ -28,10 +29,12 @@ module SimpleTypeTests 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 @@ -39,6 +42,7 @@ module SimpleTypeTests def test_type_union schema = { 'type' => %w[integer string] } + assert_valid schema, 5 assert_valid schema, 'str' refute_valid schema, nil diff --git a/test/type_attribute_test.rb b/test/type_attribute_test.rb index 81da466f..433442ea 100644 --- a/test/type_attribute_test.rb +++ b/test/type_attribute_test.rb @@ -2,14 +2,14 @@ class TypeAttributeTest < Minitest::Test def test_type_of_data - assert_equal(type_of_data(String.new), 'string') - assert_equal(type_of_data(Numeric.new), 'number') - assert_equal(type_of_data(1), 'integer') - assert_equal(type_of_data(true), 'boolean') - assert_equal(type_of_data(false), 'boolean') - assert_equal(type_of_data(Hash.new), 'object') - assert_equal(type_of_data(nil), 'null') - assert_equal(type_of_data(Object.new), 'any') + assert_equal('string', type_of_data(String.new)) + assert_equal('number', type_of_data(Numeric.new)) + assert_equal('integer', type_of_data(1)) + assert_equal('boolean', type_of_data(true)) + assert_equal('boolean', type_of_data(false)) + assert_equal('object', type_of_data(Hash.new)) + assert_equal('null', type_of_data(nil)) + assert_equal('any', type_of_data(Object.new)) end private diff --git a/test/uri_parsing_test.rb b/test/uri_parsing_test.rb index d9b87eca..03a84553 100644 --- a/test/uri_parsing_test.rb +++ b/test/uri_parsing_test.rb @@ -17,6 +17,7 @@ def test_asian_characters }, } data = { 'a' => 5 } + assert_valid schema, data end @@ -37,6 +38,7 @@ def test_schema_ref_with_empty_fragment }, } data = { 'names' => [{ 'first' => 'john' }] } + assert_valid schema, data end @@ -57,12 +59,14 @@ def test_schema_ref_from_file_with_spaces }, } data = { 'names' => [{ 'first' => 'john' }] } + assert_valid schema, data end def test_schema_from_file_with_spaces data = { 'first' => 'john' } schema = 'test/schemas/ref john with spaces schema.json' + assert_valid schema, data end end diff --git a/test/uri_util_test.rb b/test/uri_util_test.rb index fb662fe8..1a15758e 100644 --- a/test/uri_util_test.rb +++ b/test/uri_util_test.rb @@ -16,6 +16,7 @@ def test_normalized_uri uri = Addressable::URI.new(scheme: 'https', host: 'www.google.com', path: 'search',) + assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home') end @@ -25,6 +26,7 @@ def test_normalized_uri_with_empty_fragment host: 'www.google.com', path: 'search', fragment: nil,) + assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home') end @@ -34,6 +36,7 @@ def test_normalized_uri_with_fragment host: 'www.google.com', path: 'search', fragment: 'foo',) + assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home') end @@ -42,6 +45,7 @@ def test_normalized_uri_for_absolute_path uri = Addressable::URI.new(scheme: 'file', host: '', path: '/foo/bar.json',) + assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home') end @@ -50,6 +54,7 @@ def test_normalized_uri_for_relative_path uri = Addressable::URI.new(scheme: 'file', host: '', path: '/home/foo/bar.json',) + assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home') end @@ -58,6 +63,7 @@ def test_normalized_uri_for_file_path_with_host uri = Addressable::URI.new(scheme: 'file', host: 'localhost', path: '/foo/bar.json',) + assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home') end @@ -66,6 +72,7 @@ def test_uri_parse uri = Addressable::URI.new(scheme: 'https', host: 'www.google.com', path: 'search',) + assert_equal uri, JSON::Util::URI.parse(str) end diff --git a/test/validator_schema_reader_test.rb b/test/validator_schema_reader_test.rb index c4ab7630..4897e6a7 100644 --- a/test/validator_schema_reader_test.rb +++ b/test/validator_schema_reader_test.rb @@ -25,6 +25,7 @@ def teardown def test_default_schema_reader reader = JSON::Validator.schema_reader + assert reader.accept_uri?(Addressable::URI.parse('http://example.com')) assert reader.accept_file?(Pathname.new('/etc/passwd')) end @@ -33,6 +34,7 @@ def test_set_default_schema_reader JSON::Validator.schema_reader = MockReader.new schema = { '$ref' => 'http://any.url/at/all' } + assert_valid schema, 'abc' refute_valid schema, 'a' end @@ -40,6 +42,7 @@ def test_set_default_schema_reader def test_validate_with_reader reader = MockReader.new schema = { '$ref' => 'http://any.url/at/all' } + assert_valid schema, 'abc', schema_reader: reader refute_valid schema, 'a', schema_reader: reader end @@ -47,6 +50,7 @@ def test_validate_with_reader def test_validate_list_with_reader reader = MockReader.new schema = { '$ref' => 'http://any.url/at/all' } + assert_valid schema, %w[abc def], schema_reader: reader, list: true refute_valid schema, %w[abc a], schema_reader: reader, list: true end