jschema 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/jschema.rb +41 -0
- data/lib/jschema/json_reference.rb +65 -0
- data/lib/jschema/schema.rb +84 -0
- data/lib/jschema/schema_ref.rb +13 -0
- data/lib/jschema/simple_validator.rb +86 -0
- data/lib/jschema/string_length_validator.rb +17 -0
- data/lib/jschema/validator.rb +14 -0
- data/lib/jschema/validator/all_of.rb +29 -0
- data/lib/jschema/validator/any_of.rb +29 -0
- data/lib/jschema/validator/dependencies.rb +63 -0
- data/lib/jschema/validator/enum.rb +23 -0
- data/lib/jschema/validator/format.rb +75 -0
- data/lib/jschema/validator/items.rb +79 -0
- data/lib/jschema/validator/max_items.rb +28 -0
- data/lib/jschema/validator/max_length.rb +23 -0
- data/lib/jschema/validator/max_properties.rb +31 -0
- data/lib/jschema/validator/maximum.rb +31 -0
- data/lib/jschema/validator/min_items.rb +28 -0
- data/lib/jschema/validator/min_length.rb +23 -0
- data/lib/jschema/validator/min_properties.rb +31 -0
- data/lib/jschema/validator/minimum.rb +31 -0
- data/lib/jschema/validator/multiple_of.rb +32 -0
- data/lib/jschema/validator/not.rb +23 -0
- data/lib/jschema/validator/one_of.rb +29 -0
- data/lib/jschema/validator/pattern.rb +36 -0
- data/lib/jschema/validator/properties.rb +106 -0
- data/lib/jschema/validator/required.rb +34 -0
- data/lib/jschema/validator/type.rb +57 -0
- data/lib/jschema/validator/unique_items.rb +27 -0
- data/test/assert_received.rb +15 -0
- data/test/string_length_validator_tests.rb +18 -0
- data/test/test_assert_received.rb +39 -0
- data/test/test_integration.rb +30 -0
- data/test/test_json_reference.rb +84 -0
- data/test/test_schema.rb +125 -0
- data/test/test_schema_ref.rb +11 -0
- data/test/test_validator.rb +29 -0
- data/test/validator/argument_is_array_of_schemas_tests.rb +22 -0
- data/test/validator/assertions.rb +56 -0
- data/test/validator/properties_limit_tests.rb +13 -0
- data/test/validator/schema_validation_helpers.rb +29 -0
- data/test/validator/test_all_of.rb +30 -0
- data/test/validator/test_any_of.rb +31 -0
- data/test/validator/test_dependencies.rb +106 -0
- data/test/validator/test_enum.rb +30 -0
- data/test/validator/test_format.rb +70 -0
- data/test/validator/test_items.rb +113 -0
- data/test/validator/test_max_items.rb +26 -0
- data/test/validator/test_max_length.rb +30 -0
- data/test/validator/test_max_properties.rb +29 -0
- data/test/validator/test_maximum.rb +58 -0
- data/test/validator/test_min_items.rb +27 -0
- data/test/validator/test_min_length.rb +30 -0
- data/test/validator/test_min_properties.rb +29 -0
- data/test/validator/test_minimum.rb +58 -0
- data/test/validator/test_multiple_of.rb +38 -0
- data/test/validator/test_not.rb +32 -0
- data/test/validator/test_one_of.rb +31 -0
- data/test/validator/test_pattern.rb +32 -0
- data/test/validator/test_properties.rb +136 -0
- data/test/validator/test_required.rb +30 -0
- data/test/validator/test_simple_validator.rb +100 -0
- data/test/validator/test_type.rb +97 -0
- data/test/validator/test_unique_items.rb +30 -0
- data/test/validator/validation_against_schemas_tests.rb +24 -0
- metadata +144 -0
data/test/test_schema.rb
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'jschema'
|
3
|
+
|
4
|
+
require_relative 'assert_received'
|
5
|
+
|
6
|
+
class TestSchema < Minitest::Test
|
7
|
+
def test_empty_schema
|
8
|
+
JSchema::Validator.stub :build, [] do
|
9
|
+
empty_schema = JSchema::Schema.build
|
10
|
+
assert empty_schema.valid?('test')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_passing_validation
|
15
|
+
stub_validators(true) do |schema|
|
16
|
+
assert schema.valid?('instance')
|
17
|
+
assert_empty schema.validate('instance')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_failing_validation
|
22
|
+
stub_validators(false) do |schema|
|
23
|
+
refute schema.valid?('instance')
|
24
|
+
refute_empty schema.validate('instance')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_default_schema_uri
|
29
|
+
assert_equal URI('#'), schema_uri
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_schema_uri_when_parent_is_not_specified
|
33
|
+
assert_equal URI('test'), schema_uri('test')
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_schema_uri_when_parent_uri_is_absolute
|
37
|
+
uri = schema_uri('test', 'http://example.com/')
|
38
|
+
assert_equal URI('http://example.com/test'), uri
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_schema_uri_when_parent_uri_is_relative
|
42
|
+
assert_raises(JSchema::InvalidSchema) do
|
43
|
+
schema_uri('relative/', 'relative/')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_schema_uri_when_both_parent_and_schema_uri_are_absolute
|
48
|
+
schema_id = 'http://example.com/'
|
49
|
+
parent_id = 'http://localhost/'
|
50
|
+
uri = schema_uri(schema_id, parent_id)
|
51
|
+
assert_equal URI(schema_id), uri
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_schema_uri_when_ids_are_not_specified
|
55
|
+
assert_equal URI('#/child'), schema_uri(nil, nil, 'child')
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_that_schema_uri_is_normalized
|
59
|
+
uri = schema_uri('etc/../path', 'http://Example.com')
|
60
|
+
assert_equal 'http://example.com/path', uri.to_s
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_json_refenrece
|
64
|
+
schema = Object.new
|
65
|
+
JSchema::SchemaRef.stub :new, schema do
|
66
|
+
schema_ref = JSchema::Schema.build('$ref' => '#/sch')
|
67
|
+
assert_equal schema_ref, schema
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_storing_schema_in_registry
|
72
|
+
sch = Object.new
|
73
|
+
JSchema::Schema.stub :new, sch do
|
74
|
+
assert_received JSchema::JSONReference, :register_schema, [sch] do
|
75
|
+
JSchema::Schema.build
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# TODO: Make it isolated.
|
81
|
+
def test_definitions
|
82
|
+
schema_def_uri = '#/definitions/schema1'
|
83
|
+
schema = JSchema::Schema.build('definitions' => { 'schema1' => {} })
|
84
|
+
definition = JSchema::SchemaRef.new(schema_def_uri, schema)
|
85
|
+
assert_equal URI(schema_def_uri), definition.uri
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_that_exception_is_raised_when_schema_version_is_not_supported
|
89
|
+
assert_raises(JSchema::InvalidSchema) do
|
90
|
+
JSchema::Schema.build('$schema' => 'unsupported')
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_to_s
|
95
|
+
schema = JSchema::Schema.build
|
96
|
+
assert_equal '#', schema.to_s
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_simplified_syntax
|
100
|
+
assert_instance_of JSchema::Schema, JSchema.build
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
|
105
|
+
def stub_validators(ret_val)
|
106
|
+
validator = validator_stub.new(ret_val)
|
107
|
+
JSchema::Validator.stub :build, [validator] do
|
108
|
+
yield JSchema::Schema.build
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def validator_stub
|
113
|
+
Struct.new(:valid) do
|
114
|
+
def validate(_)
|
115
|
+
valid ? nil : ['error']
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def schema_uri(schema_id = nil, parent_id = nil, id = nil)
|
121
|
+
parent = JSchema::Schema.build('id' => parent_id) if parent_id || id
|
122
|
+
child = JSchema::Schema.build({ 'id' => schema_id }, parent, id)
|
123
|
+
child.uri
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,11 @@
|
|
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
|
+
end
|
@@ -0,0 +1,29 @@
|
|
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
|
@@ -0,0 +1,22 @@
|
|
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
|
@@ -0,0 +1,56 @@
|
|
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
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module PropertiesLimitTests
|
2
|
+
def test_that_argument_is_an_integer
|
3
|
+
assert_raises(JSchema::InvalidSchema) { validator 'string' }
|
4
|
+
end
|
5
|
+
|
6
|
+
def test_that_argument_can_be_big_integer
|
7
|
+
validator 2**64
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_that_argument_is_not_negative
|
11
|
+
assert_raises(JSchema::InvalidSchema) { validator(-1) }
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module SchemaValidationHelpers
|
2
|
+
class SchemaStub
|
3
|
+
def initialize(validation_results)
|
4
|
+
@validation_results = validation_results.cycle
|
5
|
+
end
|
6
|
+
|
7
|
+
def valid?(instance)
|
8
|
+
validate(instance).empty?
|
9
|
+
end
|
10
|
+
|
11
|
+
def validate(instance)
|
12
|
+
result = @validation_results.next
|
13
|
+
result ? [] : ['error']
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def stub_schema_validations(*validation_results)
|
18
|
+
schema = SchemaStub.new(validation_results)
|
19
|
+
JSchema::Schema.stub :build, schema do
|
20
|
+
yield
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def generate_schema
|
25
|
+
@id ||= 0
|
26
|
+
@id += 1
|
27
|
+
{ 'id' => @id.to_s }
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'jschema'
|
3
|
+
|
4
|
+
require_relative 'assertions'
|
5
|
+
require_relative 'schema_validation_helpers'
|
6
|
+
require_relative 'validation_against_schemas_tests'
|
7
|
+
|
8
|
+
class TestAllOf < Minitest::Test
|
9
|
+
include Assertions
|
10
|
+
include SchemaValidationHelpers
|
11
|
+
include ValidationAgainstSchemasTests
|
12
|
+
|
13
|
+
def test_passing_validation_against_schemas
|
14
|
+
stub_schema_validations(true, true) do
|
15
|
+
assert validator([generate_schema, generate_schema]).valid?('test')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_failing_validation_against_schemas
|
20
|
+
stub_schema_validations(false, true) do
|
21
|
+
refute validator([generate_schema, generate_schema]).valid?('test')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def validator_class
|
28
|
+
JSchema::Validator::AllOf
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'jschema'
|
3
|
+
|
4
|
+
require_relative 'assertions'
|
5
|
+
require_relative 'schema_validation_helpers'
|
6
|
+
require_relative 'validation_against_schemas_tests'
|
7
|
+
|
8
|
+
class TestAnyOf < Minitest::Test
|
9
|
+
include Assertions
|
10
|
+
include SchemaValidationHelpers
|
11
|
+
include ValidationAgainstSchemasTests
|
12
|
+
|
13
|
+
def test_passing_validation_agaist_schemas
|
14
|
+
stub_schema_validations(true, false, true) do
|
15
|
+
schema = [generate_schema, generate_schema, generate_schema]
|
16
|
+
assert validator(schema).valid?('test')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_failing_validation_against_schemas
|
21
|
+
stub_schema_validations(false, false) do
|
22
|
+
refute validator([generate_schema, generate_schema]).valid?('test')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def validator_class
|
29
|
+
JSchema::Validator::AnyOf
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'jschema'
|
3
|
+
|
4
|
+
require_relative 'assertions'
|
5
|
+
require_relative 'schema_validation_helpers'
|
6
|
+
|
7
|
+
class TestDependencies < Minitest::Test
|
8
|
+
include Assertions
|
9
|
+
include SchemaValidationHelpers
|
10
|
+
|
11
|
+
def test_that_argument_is_an_object
|
12
|
+
assert_raises_unless 'dependencies', :object
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_that_each_value_of_argument_object_is_either_an_object_or_an_array
|
16
|
+
assert_raises(JSchema::InvalidSchema) do
|
17
|
+
validator('test' => 0)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_that_property_dependency_has_at_least_one_element
|
22
|
+
assert_raises(JSchema::InvalidSchema) do
|
23
|
+
validator('test' => [])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_that_property_dependency_includes_only_strings
|
28
|
+
assert_raises(JSchema::InvalidSchema) do
|
29
|
+
validator('test' => [0])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_that_property_dependency_containt_uniq_elements
|
34
|
+
assert_raises(JSchema::InvalidSchema) do
|
35
|
+
validator('test' => ['property', 'property'])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_that_argument_can_contain_schema_and_property_dependencies
|
40
|
+
validator('schema_dependency' => {}, 'property_dependency' => ['test'])
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_passing_schema_dependency_validation
|
44
|
+
stub_schema_validations(true) do
|
45
|
+
assert schema_dependencies_validator.valid?('test' => 0)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_failing_schema_dependency_validation
|
50
|
+
stub_schema_validations(false) do
|
51
|
+
refute schema_dependencies_validator.valid?('test' => 0)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_passing_validation_when_instance_has_no_dependencies
|
56
|
+
stub_schema_validations(false) do
|
57
|
+
assert schema_dependencies_validator.valid?('other' => 0)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_passing_property_dependency_validation
|
62
|
+
expect_required_validation(true) do
|
63
|
+
assert property_dependencies_validator.valid?('test' => 0)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_failing_property_dependency_validation
|
68
|
+
expect_required_validation(false) do
|
69
|
+
refute property_dependencies_validator.valid?('test' => 0)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_passing_property_dependency_validation_no_dependencies
|
74
|
+
expect_required_validation(false) do
|
75
|
+
assert property_dependencies_validator.valid?('other' => 0)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def validator_class
|
82
|
+
JSchema::Validator::Dependencies
|
83
|
+
end
|
84
|
+
|
85
|
+
def schema_dependencies_validator
|
86
|
+
validator('test' => {})
|
87
|
+
end
|
88
|
+
|
89
|
+
def property_dependencies_validator
|
90
|
+
validator('test' => ['required_property'])
|
91
|
+
end
|
92
|
+
|
93
|
+
def expect_required_validation(valid)
|
94
|
+
validator_stub =
|
95
|
+
Struct.new(:valid) do
|
96
|
+
def validate(_)
|
97
|
+
valid ? [] : ['error']
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
validator = validator_stub.new(valid)
|
102
|
+
JSchema::Validator::Required.stub :new, validator do
|
103
|
+
yield
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|