jschema 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jschema/json_reference.rb +1 -1
  3. data/lib/jschema/schema_uri.rb +6 -2
  4. data/lib/jschema/simple_validator.rb +3 -3
  5. data/lib/jschema/string_length_validator.rb +2 -2
  6. data/lib/jschema/validator/dependencies.rb +2 -2
  7. data/lib/jschema/validator/format.rb +7 -3
  8. data/lib/jschema/validator/items.rb +2 -2
  9. data/lib/jschema/validator/max_items.rb +2 -2
  10. data/lib/jschema/validator/max_properties.rb +2 -2
  11. data/lib/jschema/validator/maximum.rb +2 -2
  12. data/lib/jschema/validator/min_items.rb +2 -2
  13. data/lib/jschema/validator/min_properties.rb +2 -2
  14. data/lib/jschema/validator/minimum.rb +2 -2
  15. data/lib/jschema/validator/multiple_of.rb +2 -2
  16. data/lib/jschema/validator/pattern.rb +2 -2
  17. data/lib/jschema/validator/properties.rb +2 -2
  18. data/lib/jschema/validator/required.rb +2 -2
  19. data/lib/jschema/validator/unique_items.rb +2 -2
  20. metadata +4 -78
  21. data/test/assert_received.rb +0 -15
  22. data/test/string_length_validator_tests.rb +0 -18
  23. data/test/test_assert_received.rb +0 -39
  24. data/test/test_integration.rb +0 -30
  25. data/test/test_json_reference.rb +0 -115
  26. data/test/test_schema.rb +0 -132
  27. data/test/test_schema_ref.rb +0 -19
  28. data/test/test_schema_uri.rb +0 -56
  29. data/test/test_validator.rb +0 -29
  30. data/test/validator/argument_is_array_of_schemas_tests.rb +0 -22
  31. data/test/validator/assertions.rb +0 -56
  32. data/test/validator/properties_limit_tests.rb +0 -13
  33. data/test/validator/schema_validation_helpers.rb +0 -29
  34. data/test/validator/test_all_of.rb +0 -30
  35. data/test/validator/test_any_of.rb +0 -31
  36. data/test/validator/test_dependencies.rb +0 -106
  37. data/test/validator/test_enum.rb +0 -30
  38. data/test/validator/test_format.rb +0 -78
  39. data/test/validator/test_items.rb +0 -119
  40. data/test/validator/test_max_items.rb +0 -26
  41. data/test/validator/test_max_length.rb +0 -30
  42. data/test/validator/test_max_properties.rb +0 -29
  43. data/test/validator/test_maximum.rb +0 -58
  44. data/test/validator/test_min_items.rb +0 -27
  45. data/test/validator/test_min_length.rb +0 -30
  46. data/test/validator/test_min_properties.rb +0 -29
  47. data/test/validator/test_minimum.rb +0 -58
  48. data/test/validator/test_multiple_of.rb +0 -38
  49. data/test/validator/test_not.rb +0 -32
  50. data/test/validator/test_one_of.rb +0 -31
  51. data/test/validator/test_pattern.rb +0 -32
  52. data/test/validator/test_properties.rb +0 -128
  53. data/test/validator/test_required.rb +0 -30
  54. data/test/validator/test_simple_validator.rb +0 -107
  55. data/test/validator/test_type.rb +0 -97
  56. data/test/validator/test_unique_items.rb +0 -30
  57. data/test/validator/validation_against_schemas_tests.rb +0 -24
@@ -1,30 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'webmock/minitest'
3
- require 'jschema'
4
- require 'json'
5
-
6
- class TestIntegration < Minitest::Test
7
- def test_simple_schema
8
- stub_request(:get, 'http://json-schema.org/geo')
9
- .to_return(body: Pathname.new('test/fixtures/geo.json'))
10
-
11
- validate 'json_schema1.json', 'json_data1.json'
12
- end
13
-
14
- def test_advanced_schema
15
- validate 'json_schema2.json', 'json_data2.json'
16
- end
17
-
18
- private
19
-
20
- def validate(schema_file, data_file)
21
- sch = json_fixture(schema_file).freeze
22
- schema = JSchema::Schema.build(sch)
23
- data = json_fixture(data_file)
24
- assert schema.valid?(data)
25
- end
26
-
27
- def json_fixture(filename)
28
- JSON.parse open(File.join('test', 'fixtures', filename)).read
29
- end
30
- end
@@ -1,115 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'webmock/minitest'
3
- require 'jschema'
4
- require 'ostruct'
5
-
6
- require_relative 'assert_received'
7
-
8
- class TestJSONReference < Minitest::Test
9
- def test_schema_registration_and_dereferencing
10
- schema = generate_schema('registered')
11
- JSchema::JSONReference.register_schema schema
12
- assert_equal schema, dereference(schema)
13
- end
14
-
15
- def test_schema_dereferencing_with_same_uri
16
- schema1 = generate_schema('uri')
17
- schema2 = generate_schema('uri')
18
- JSchema::JSONReference.register_schema schema1
19
- JSchema::JSONReference.register_schema schema2
20
-
21
- assert_equal schema1, dereference(schema1)
22
- assert_equal schema2, dereference(schema2)
23
- end
24
-
25
- def test_dereferencing_external_schema
26
- external_schema = { 'type' => 'number' }
27
- schema_uri = 'http://example.com/geo'
28
-
29
- expected_schema_args = [external_schema, URI(schema_uri), nil]
30
- assert_received JSchema::Schema, :new, expected_schema_args do
31
- JSchema::JSONReference.stub :register_schema, nil do
32
- dereference_external_schema schema_uri, external_schema.to_json, false
33
- end
34
- end
35
- end
36
-
37
- def test_that_external_schema_is_cached_after_dereferencing
38
- schema = Object.new
39
- JSchema::Schema.stub :new, schema do
40
- assert_received JSchema::JSONReference, :register_schema, [schema] do
41
- dereference_external_schema 'http://example.com', '{}'
42
- end
43
- end
44
- end
45
-
46
- def test_dereferencing_external_schema_when_it_is_not_valid
47
- assert_raises(JSchema::InvalidSchema) do
48
- dereference_external_schema 'http://example.com/', '}'
49
- end
50
- end
51
-
52
- def test_dereferencing_external_schema_when_it_is_not_available
53
- assert_raises(JSchema::InvalidSchema) do
54
- stub_request(:get, //).to_timeout
55
- dereference generate_schema('http://example.com/')
56
- end
57
- end
58
-
59
- def test_dereferencing_external_schema_when_protocol_is_not_supported
60
- assert_raises(JSchema::InvalidSchema) do
61
- dereference_external_schema 'ftp://example.com/', '{}'
62
- end
63
- end
64
-
65
- def test_dereferencing_within_schema_with_non_default_uri
66
- schema = generate_schema('http://example.com/', false)
67
- JSchema::JSONReference.register_schema schema
68
- assert_equal schema, JSchema::JSONReference.dereference(URI('#'), schema)
69
- end
70
-
71
- def test_dereferencing_root_schema
72
- schema1 = generate_schema('#', false)
73
- schema2 = generate_schema('http://example.com/', false)
74
- JSchema::JSONReference.register_schema schema1
75
- JSchema::JSONReference.register_schema schema2
76
-
77
- assert_equal schema2,
78
- JSchema::JSONReference.dereference(URI('#'), schema2)
79
-
80
- assert_equal schema2,
81
- JSchema::JSONReference.dereference(URI('http://example.com/#'), schema2)
82
-
83
- assert_equal schema2,
84
- JSchema::JSONReference.dereference(URI('http://example.com/'), schema2)
85
- end
86
-
87
- def test_dereferencing_schema_by_slightly_different_uri
88
- schema = generate_schema('http://example.com/#', false)
89
- JSchema::JSONReference.register_schema schema
90
-
91
- assert_equal schema,
92
- JSchema::JSONReference.dereference(URI('http://example.com/'), schema)
93
- end
94
-
95
- private
96
-
97
- def dereference_external_schema(uri, response_schema, parent = true)
98
- stub_request(:get, uri).to_return(body: response_schema)
99
- dereference generate_schema(uri, parent)
100
- end
101
-
102
- def dereference(schema)
103
- JSchema::JSONReference.dereference(schema.uri, schema.parent)
104
- end
105
-
106
- def generate_schema(uri, parent = true)
107
- parent_schema = generate_schema('#', false) if parent
108
- OpenStruct.new(uri: URI(uri), id: generate_id, parent: parent_schema)
109
- end
110
-
111
- def generate_id
112
- @id ||= 0
113
- @id += 1
114
- end
115
- end
data/test/test_schema.rb DELETED
@@ -1,132 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'webmock/minitest'
3
- require 'jschema'
4
-
5
- require_relative 'assert_received'
6
-
7
- class TestSchema < Minitest::Test
8
- def test_empty_schema
9
- JSchema::Validator.stub :build, [] do
10
- empty_schema = JSchema::Schema.build
11
- assert empty_schema.valid?('test')
12
- end
13
- end
14
-
15
- def test_passing_validation
16
- stub_validators(true) do |schema|
17
- assert schema.valid?('instance')
18
- assert_empty schema.validate('instance')
19
- end
20
- end
21
-
22
- def test_failing_validation
23
- stub_validators(false) do |schema|
24
- refute schema.valid?('instance')
25
- refute_empty schema.validate('instance')
26
- end
27
- end
28
-
29
- def test_json_refenrece
30
- schema = Object.new
31
- JSchema::SchemaRef.stub :new, schema do
32
- schema_ref = JSchema::Schema.build('$ref' => '#/sch')
33
- assert_equal schema_ref, schema
34
- end
35
- end
36
-
37
- def test_that_tilda_is_unescaped
38
- expected_ref_uri = URI('#/definitions/sch~')
39
- assert_received JSchema::SchemaRef, :new, [expected_ref_uri, nil] do
40
- JSchema::Schema.build('$ref' => '#/definitions/sch~0')
41
- end
42
- end
43
-
44
- def test_that_forward_slash_is_unescaped
45
- expected_ref_uri = URI('#/definitions/sch/sch')
46
- assert_received JSchema::SchemaRef, :new, [expected_ref_uri, nil] do
47
- JSchema::Schema.build('$ref' => '#/definitions/sch~1sch')
48
- end
49
- end
50
-
51
- def test_storing_schema_in_registry
52
- sch = Object.new
53
- JSchema::Schema.stub :new, sch do
54
- assert_received JSchema::JSONReference, :register_schema, [sch] do
55
- JSchema::Schema.build
56
- end
57
- end
58
- end
59
-
60
- # TODO: Make it isolated.
61
- def test_schema_caching
62
- parent = JSchema::Schema.build
63
- sch = { 'type' => 'string' }
64
- schema1 = JSchema::Schema.build(sch, parent)
65
- schema2 = JSchema::Schema.build(sch, parent)
66
- assert_equal schema1.object_id, schema2.object_id
67
- end
68
-
69
- # TODO: Make it isolated.
70
- def test_that_root_schemas_are_not_cached
71
- sch = { 'type' => 'string' }
72
- schema1 = JSchema::Schema.build(sch)
73
- schema2 = JSchema::Schema.build(sch)
74
- refute_equal schema1.object_id, schema2.object_id
75
- end
76
-
77
- # TODO: Make it isolated.
78
- def test_definitions
79
- schema_def_uri = URI('#/definitions/schema1')
80
- schema = JSchema::Schema.build('definitions' => { 'schema1' => {} })
81
- definition = JSchema::SchemaRef.new(schema_def_uri, schema)
82
- assert_equal schema_def_uri, definition.uri
83
- end
84
-
85
- # TODO: Make it isolated.
86
- def test_definitions
87
- schema = JSchema::Schema.build(
88
- 'definitions' => { 'schema1' => {} },
89
- '$ref' => '#/definitions/schema1'
90
- )
91
-
92
- schema.valid?('')
93
- end
94
-
95
- def test_that_exception_is_raised_when_schema_version_is_not_supported
96
- assert_raises(JSchema::InvalidSchema) do
97
- JSchema::Schema.build('$schema' => 'unsupported')
98
- end
99
- end
100
-
101
- def test_to_s
102
- schema = JSchema::Schema.build
103
- assert_equal '#', schema.to_s
104
- end
105
-
106
- def test_simplified_syntax
107
- assert_instance_of JSchema::Schema, JSchema.build
108
- end
109
-
110
- private
111
-
112
- def stub_validators(ret_val)
113
- validator = validator_stub.new(ret_val)
114
- JSchema::Validator.stub :build, [validator] do
115
- yield JSchema::Schema.build
116
- end
117
- end
118
-
119
- def validator_stub
120
- Struct.new(:valid) do
121
- def validate(_)
122
- valid ? nil : ['error']
123
- end
124
- end
125
- end
126
-
127
- def schema_uri(schema_id = nil, parent_id = nil, id = nil)
128
- parent = JSchema::Schema.build('id' => parent_id) if parent_id || id
129
- child = JSchema::Schema.build({ 'id' => schema_id }, parent, id)
130
- child.uri
131
- end
132
- end
@@ -1,19 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'jschema'
3
-
4
- class TestSchemaRef < Minitest::Test
5
- def test_that_schema_ref_acts_like_schema
6
- schema = JSchema::Schema.build
7
- JSchema::JSONReference.stub :dereference, schema do
8
- assert_equal JSchema::SchemaRef.new(schema.uri, nil), schema
9
- end
10
- end
11
-
12
- def test_that_exception_is_raised_if_reference_is_incorrect
13
- JSchema::JSONReference.stub :dereference, nil do
14
- assert_raises(JSchema::InvalidSchema) do
15
- JSchema::SchemaRef.new('invalid', nil).parent
16
- end
17
- end
18
- end
19
- end
@@ -1,56 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'jschema'
3
-
4
- class TestSchemaURI < Minitest::Test
5
- def test_default_uri
6
- assert_equal URI('#'), schema_uri
7
- end
8
-
9
- def test_schema_uri_when_parent_is_not_specified
10
- assert_equal URI('test'), schema_uri('test')
11
- end
12
-
13
- def test_schema_uri_when_parent_uri_is_absolute
14
- uri = schema_uri('test', 'http://example.com/')
15
- assert_equal URI('http://example.com/test'), uri
16
- end
17
-
18
- def test_schema_uri_when_parent_uri_is_relative
19
- assert_raises(JSchema::InvalidSchema) do
20
- schema_uri('relative/', 'relative/')
21
- end
22
- end
23
-
24
- def test_schema_uri_when_both_parent_and_schema_uri_are_absolute
25
- schema_id = 'http://example.com/'
26
- parent_id = 'http://localhost/'
27
- uri = schema_uri(schema_id, parent_id)
28
- assert_equal URI(schema_id), uri
29
- end
30
-
31
- def test_schema_uri_when_ids_are_not_specified
32
- assert_equal URI('#/child'), schema_uri(nil, nil, 'child')
33
- end
34
-
35
- def test_that_schema_uri_is_normalized
36
- uri = schema_uri('etc/../path', 'http://Example.com')
37
- assert_equal URI('http://example.com/path'), uri
38
- end
39
-
40
- def test_that_implicit_schema_id_treated_as_uri_fragment
41
- uri = schema_uri(nil, 'http://example.com/path#sch', 'sub')
42
- assert_equal URI('http://example.com/path#sch/sub'), uri
43
- end
44
-
45
- def test_escaping
46
- uri = schema_uri(nil, '#/definitions', 'schema%')
47
- assert_equal URI('#/definitions/schema%25'), uri
48
- end
49
-
50
- private
51
-
52
- def schema_uri(schema_id = nil, parent_id = nil, id = nil)
53
- parent_schema = JSchema::Schema.build('id' => parent_id) if parent_id || id
54
- JSchema::SchemaURI.build(schema_id, parent_schema, id)
55
- end
56
- end
@@ -1,29 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'jschema'
3
-
4
- class TestValidatorInstantiation < Minitest::Test
5
- def test_creating_of_validators
6
- return_validator = Object.new
7
- assert_equal [return_validator], build_validators(return_validator)
8
- end
9
-
10
- def test_parsing_of_empty_schema
11
- assert_empty build_validators(nil)
12
- end
13
-
14
- private
15
-
16
- def build_validators(return_validator)
17
- schema = { 'keyword' => 'arg' }
18
- parent_schema = Object.new
19
- validator_class = MiniTest::Mock.new
20
- validator_class.expect :build, return_validator, [schema, parent_schema]
21
- result = JSchema::Validator.stub :constants, [:Test] do
22
- JSchema::Validator.stub :const_get, validator_class do
23
- JSchema::Validator.build(schema, parent_schema)
24
- end
25
- end
26
- validator_class.verify
27
- result
28
- end
29
- end
@@ -1,22 +0,0 @@
1
- module ArgumentIsArrayOfSchemasTests
2
- def test_that_argument_is_array
3
- assert_raises(JSchema::InvalidSchema) { validator(0) }
4
- end
5
-
6
- def test_that_argument_is_not_empty_array
7
- assert_raises(JSchema::InvalidSchema) { validator [] }
8
- end
9
-
10
- def test_that_argument_contains_only_unique_values
11
- assert_raises(JSchema::InvalidSchema) { validator [{}, {}] }
12
- end
13
-
14
- def test_that_argument_contains_only_valid_json_schemas
15
- assert_raises(JSchema::InvalidSchema) do
16
- raises_error = ->(*_) { fail JSchema::InvalidSchema }
17
- JSchema::Schema.stub :build, raises_error do
18
- validator [{}]
19
- end
20
- end
21
- end
22
- end
@@ -1,56 +0,0 @@
1
- module Assertions
2
- def assert_raises_unless(keyword, *valid_types)
3
- basic_types = {
4
- array: [], boolean: true, integer: 1,
5
- number: 1.2, object: {}, string: 'test'
6
- }
7
-
8
- invalid_values = basic_types.select do |type, _|
9
- !valid_types.include?(type)
10
- end.values
11
-
12
- assert_raises_for_values keyword, invalid_values
13
- end
14
-
15
- def assert_raises_unless_schema(keyword, value = {})
16
- raises_error = ->(*_) { fail JSchema::InvalidSchema }
17
- JSchema::Schema.stub :build, raises_error do
18
- assert_raises_for_values keyword, [value]
19
- end
20
- end
21
-
22
- def assert_raises_unless_schema_array(keyword)
23
- assert_raises_unless_schema keyword, [{}]
24
- end
25
-
26
- def assert_raises_unless_string_array(keyword)
27
- invalid_values = [0, [], ['test', 'test'], [0]]
28
- assert_raises_for_values keyword, invalid_values
29
- end
30
-
31
- def assert_raises_unless_non_empty_array(keyword)
32
- invalid_values = [0, [], ['test', 'test']]
33
- assert_raises_for_values keyword, invalid_values
34
- end
35
-
36
- def assert_raises_unless_non_negative_integer(keyword)
37
- assert_raises_unless keyword, :integer
38
- assert_raises_for_values keyword, [-1]
39
- end
40
-
41
- def assert_raises_for_values(keyword, invalid_values)
42
- invalid_values.each do |invalid_value|
43
- assert_raises(JSchema::InvalidSchema) do
44
- build_from_schema keyword => invalid_value
45
- end
46
- end
47
- end
48
-
49
- def validator(*args)
50
- validator_class.new(*args, nil)
51
- end
52
-
53
- def build_from_schema(schema = {})
54
- validator_class.build(schema, nil)
55
- end
56
- end