jschema 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/lib/jschema.rb +41 -0
  3. data/lib/jschema/json_reference.rb +65 -0
  4. data/lib/jschema/schema.rb +84 -0
  5. data/lib/jschema/schema_ref.rb +13 -0
  6. data/lib/jschema/simple_validator.rb +86 -0
  7. data/lib/jschema/string_length_validator.rb +17 -0
  8. data/lib/jschema/validator.rb +14 -0
  9. data/lib/jschema/validator/all_of.rb +29 -0
  10. data/lib/jschema/validator/any_of.rb +29 -0
  11. data/lib/jschema/validator/dependencies.rb +63 -0
  12. data/lib/jschema/validator/enum.rb +23 -0
  13. data/lib/jschema/validator/format.rb +75 -0
  14. data/lib/jschema/validator/items.rb +79 -0
  15. data/lib/jschema/validator/max_items.rb +28 -0
  16. data/lib/jschema/validator/max_length.rb +23 -0
  17. data/lib/jschema/validator/max_properties.rb +31 -0
  18. data/lib/jschema/validator/maximum.rb +31 -0
  19. data/lib/jschema/validator/min_items.rb +28 -0
  20. data/lib/jschema/validator/min_length.rb +23 -0
  21. data/lib/jschema/validator/min_properties.rb +31 -0
  22. data/lib/jschema/validator/minimum.rb +31 -0
  23. data/lib/jschema/validator/multiple_of.rb +32 -0
  24. data/lib/jschema/validator/not.rb +23 -0
  25. data/lib/jschema/validator/one_of.rb +29 -0
  26. data/lib/jschema/validator/pattern.rb +36 -0
  27. data/lib/jschema/validator/properties.rb +106 -0
  28. data/lib/jschema/validator/required.rb +34 -0
  29. data/lib/jschema/validator/type.rb +57 -0
  30. data/lib/jschema/validator/unique_items.rb +27 -0
  31. data/test/assert_received.rb +15 -0
  32. data/test/string_length_validator_tests.rb +18 -0
  33. data/test/test_assert_received.rb +39 -0
  34. data/test/test_integration.rb +30 -0
  35. data/test/test_json_reference.rb +84 -0
  36. data/test/test_schema.rb +125 -0
  37. data/test/test_schema_ref.rb +11 -0
  38. data/test/test_validator.rb +29 -0
  39. data/test/validator/argument_is_array_of_schemas_tests.rb +22 -0
  40. data/test/validator/assertions.rb +56 -0
  41. data/test/validator/properties_limit_tests.rb +13 -0
  42. data/test/validator/schema_validation_helpers.rb +29 -0
  43. data/test/validator/test_all_of.rb +30 -0
  44. data/test/validator/test_any_of.rb +31 -0
  45. data/test/validator/test_dependencies.rb +106 -0
  46. data/test/validator/test_enum.rb +30 -0
  47. data/test/validator/test_format.rb +70 -0
  48. data/test/validator/test_items.rb +113 -0
  49. data/test/validator/test_max_items.rb +26 -0
  50. data/test/validator/test_max_length.rb +30 -0
  51. data/test/validator/test_max_properties.rb +29 -0
  52. data/test/validator/test_maximum.rb +58 -0
  53. data/test/validator/test_min_items.rb +27 -0
  54. data/test/validator/test_min_length.rb +30 -0
  55. data/test/validator/test_min_properties.rb +29 -0
  56. data/test/validator/test_minimum.rb +58 -0
  57. data/test/validator/test_multiple_of.rb +38 -0
  58. data/test/validator/test_not.rb +32 -0
  59. data/test/validator/test_one_of.rb +31 -0
  60. data/test/validator/test_pattern.rb +32 -0
  61. data/test/validator/test_properties.rb +136 -0
  62. data/test/validator/test_required.rb +30 -0
  63. data/test/validator/test_simple_validator.rb +100 -0
  64. data/test/validator/test_type.rb +97 -0
  65. data/test/validator/test_unique_items.rb +30 -0
  66. data/test/validator/validation_against_schemas_tests.rb +24 -0
  67. metadata +144 -0
@@ -0,0 +1,30 @@
1
+ require 'minitest/autorun'
2
+ require 'jschema'
3
+
4
+ require_relative 'assertions'
5
+
6
+ class TestEnum < Minitest::Test
7
+ include Assertions
8
+
9
+ def test_that_argument_is_non_empty_array_of_unique_values
10
+ assert_raises_unless_non_empty_array 'enum'
11
+ end
12
+
13
+ def test_passing_validation
14
+ assert validator(['test']).valid?('test')
15
+ end
16
+
17
+ def test_passing_validtion_with_null
18
+ assert validator([nil]).valid?(nil)
19
+ end
20
+
21
+ def test_failing_validation
22
+ refute validator(['test']).valid?(0)
23
+ end
24
+
25
+ private
26
+
27
+ def validator_class
28
+ JSchema::Validator::Enum
29
+ end
30
+ end
@@ -0,0 +1,70 @@
1
+ require 'minitest/autorun'
2
+ require_relative 'assertions'
3
+
4
+ class TestFormat < Minitest::Test
5
+ include Assertions
6
+
7
+ def test_that_argument_is_string
8
+ assert_raises_unless('format', :string)
9
+ end
10
+
11
+ def test_that_argument_is_one_of_allowed_formats
12
+ assert_raises(JSchema::InvalidSchema) do
13
+ validator 'unsupported_format'
14
+ end
15
+ end
16
+
17
+ def test_passing_validation_by_date_time_format
18
+ assert validator('date-time').valid?('2001-02-03T04:05:06+07:00')
19
+ end
20
+
21
+ def test_failing_validation_by_date_time_format
22
+ refute validator('date-time').valid?('2001-x2-03T04:05:06+07:00')
23
+ end
24
+
25
+ def test_passing_validation_by_email_format
26
+ assert validator('email').valid?('steve@apple.com')
27
+ end
28
+
29
+ def test_failing_validation_by_email_format
30
+ refute validator('email').valid?('invalid_email')
31
+ end
32
+
33
+ def test_passing_validation_by_hostname_format
34
+ assert validator('hostname').valid?('apple.com')
35
+ end
36
+
37
+ def test_failing_validation_by_hostname_format
38
+ refute validator('hostname').valid?('invalid+hostname')
39
+ end
40
+
41
+ def test_passing_validation_by_ipv4_format
42
+ assert validator('ipv4').valid?('127.0.0.1')
43
+ end
44
+
45
+ def test_failing_validation_by_ipv4_format
46
+ refute validator('ipv4').valid?('0')
47
+ end
48
+
49
+ def test_passing_validation_by_ipv6_format
50
+ assert validator('ipv6').valid?('::1')
51
+ end
52
+
53
+ def test_failing_validation_by_ipv6_format
54
+ refute validator('ipv6').valid?('0')
55
+ end
56
+
57
+ def test_passing_validation_by_uri_format
58
+ assert validator('uri').valid?('http://example.com/')
59
+ end
60
+
61
+ def test_failing_validation_by_uri_format
62
+ refute validator('uri').valid?('://')
63
+ end
64
+
65
+ private
66
+
67
+ def validator_class
68
+ JSchema::Validator::Format
69
+ end
70
+ end
@@ -0,0 +1,113 @@
1
+ require 'minitest/autorun'
2
+ require 'jschema'
3
+
4
+ require_relative 'assertions'
5
+ require_relative 'schema_validation_helpers'
6
+
7
+ class TestItems < Minitest::Test
8
+ include Assertions
9
+ include SchemaValidationHelpers
10
+
11
+ def test_that_additional_items_must_be_boolean_or_schema
12
+ assert_raises_unless 'additionalItems', :boolean, :object
13
+ end
14
+
15
+ def test_that_additional_items_accepts_only_valid_schema
16
+ assert_raises_unless_schema 'additionalItems'
17
+ end
18
+
19
+ def test_that_items_must_be_object_or_array
20
+ assert_raises_unless 'items', :object, :array
21
+ end
22
+
23
+ def test_that_items_must_be_a_valid_schema
24
+ assert_raises_unless_schema 'items'
25
+ end
26
+
27
+ def test_that_items_must_be_schema_array
28
+ assert_raises_unless_schema_array 'items'
29
+ end
30
+
31
+ def test_passing_validation_by_items_schema
32
+ stub_schema_validations(true) do
33
+ assert build_from_schema('items' => {}).valid?(['test'])
34
+ end
35
+ end
36
+
37
+ def test_passing_validation_by_items_schema_array
38
+ stub_schema_validations(true) do
39
+ schema = { 'items' => [generate_schema, generate_schema] }
40
+ assert build_from_schema(schema).valid?(['test', 'test'])
41
+ end
42
+ end
43
+
44
+ def test_passing_validation_by_items_when_additional_items_is_omitted
45
+ stub_schema_validations(true) do
46
+ schema = { 'items' => [generate_schema] }
47
+ assert build_from_schema(schema).valid?(['test', 'test'])
48
+ end
49
+ end
50
+
51
+ def test_passing_validation_by_items_and_additional_items
52
+ stub_schema_validations(true) do
53
+ schema = {
54
+ 'items' => [generate_schema],
55
+ 'additionalItems' => generate_schema
56
+ }
57
+ assert build_from_schema(schema).valid?(['test', 'test'])
58
+ end
59
+ end
60
+
61
+ def test_passing_validation_when_additional_items_are_not_allowed
62
+ stub_schema_validations(true) do
63
+ schema = {
64
+ 'items' => [generate_schema],
65
+ 'additionalItems' => false
66
+ }
67
+ assert build_from_schema(schema).valid?(['test'])
68
+ end
69
+ end
70
+
71
+ def test_passing_validation_when_instance_size_is_less_than_items_size
72
+ stub_schema_validations(true) do
73
+ schema = {
74
+ 'items' => [generate_schema, generate_schema],
75
+ 'additionalItems' => false
76
+ }
77
+ assert build_from_schema(schema).valid?(['test'])
78
+ end
79
+ end
80
+
81
+ def test_failing_validation_when_additional_items_are_not_allowed
82
+ stub_schema_validations(true) do
83
+ schema = {
84
+ 'items' => [generate_schema],
85
+ 'additionalItems' => false
86
+ }
87
+ refute build_from_schema(schema).valid?(['test', 'test'])
88
+ end
89
+ end
90
+
91
+ def test_failing_validation_by_items_schema_array
92
+ stub_schema_validations(false, true) do
93
+ schema = { 'items' => [generate_schema, generate_schema] }
94
+ refute build_from_schema(schema).valid?(['test', 'test'])
95
+ end
96
+ end
97
+
98
+ def test_failing_validation_by_additional_items_schema
99
+ stub_schema_validations(true, false) do
100
+ schema = {
101
+ 'items' => [generate_schema],
102
+ 'additionalItems' => generate_schema
103
+ }
104
+ refute build_from_schema(schema).valid?(['test', 'test'])
105
+ end
106
+ end
107
+
108
+ private
109
+
110
+ def validator_class
111
+ JSchema::Validator::Items
112
+ end
113
+ end
@@ -0,0 +1,26 @@
1
+ require 'minitest/autorun'
2
+ require 'jschema'
3
+
4
+ require_relative 'assertions'
5
+
6
+ class TestMaxItems < Minitest::Test
7
+ include Assertions
8
+
9
+ def test_that_argument_is_non_negative_iteger
10
+ assert_raises_unless_non_negative_integer 'maxItems'
11
+ end
12
+
13
+ def test_passing_validation
14
+ assert validator(3).valid?([1, 2])
15
+ end
16
+
17
+ def test_failing_validation
18
+ refute validator(2).valid?([1, 2, 3])
19
+ end
20
+
21
+ private
22
+
23
+ def validator_class
24
+ JSchema::Validator::MaxItems
25
+ end
26
+ end
@@ -0,0 +1,30 @@
1
+ require 'minitest/autorun'
2
+ require 'jschema'
3
+
4
+ require_relative 'assertions'
5
+ require_relative '../string_length_validator_tests'
6
+
7
+ class TestMaxLength < Minitest::Test
8
+ include Assertions
9
+ include StringLengthValidatorTests
10
+
11
+ def test_that_argument_is_a_positive_number
12
+ assert_raises_for_values 'maxLength', [0]
13
+ end
14
+
15
+ def test_passing_validation
16
+ assert validator(4).valid?('test')
17
+ assert validator(10).valid?('test')
18
+ assert validator(10).valid?('')
19
+ end
20
+
21
+ def test_failing_validation
22
+ refute validator(3).valid?('test')
23
+ end
24
+
25
+ private
26
+
27
+ def validator_class
28
+ JSchema::Validator::MaxLength
29
+ end
30
+ end
@@ -0,0 +1,29 @@
1
+ require 'minitest/autorun'
2
+ require 'jschema'
3
+
4
+ require_relative 'assertions'
5
+ require_relative 'properties_limit_tests'
6
+
7
+ class TestMaxProperties < Minitest::Test
8
+ include Assertions
9
+ include PropertiesLimitTests
10
+
11
+ def test_building_validator_from_schema
12
+ validator = build_from_schema('maxProperties' => 10)
13
+ assert_instance_of JSchema::Validator::MaxProperties, validator
14
+ end
15
+
16
+ def test_passing_validation
17
+ assert validator(1).valid?('test' => 1)
18
+ end
19
+
20
+ def test_failing_validation
21
+ refute validator(1).valid?('test1' => 1, 'test2' => 2)
22
+ end
23
+
24
+ private
25
+
26
+ def validator_class
27
+ JSchema::Validator::MaxProperties
28
+ end
29
+ end
@@ -0,0 +1,58 @@
1
+ require 'minitest/autorun'
2
+ require 'jschema'
3
+
4
+ require_relative 'assertions'
5
+
6
+ class TestMaximum < Minitest::Test
7
+ include Assertions
8
+
9
+ def test_that_maximum_is_a_number
10
+ assert_raises_unless 'maximum', :integer, :number
11
+ end
12
+
13
+ def test_that_exclusive_maximum_is_boolean
14
+ assert_raises(JSchema::InvalidSchema) do
15
+ build_from_schema('maximum' => 1, 'exclusiveMaximum' => 0)
16
+ end
17
+ end
18
+
19
+ def test_that_exclusive_maximum_can_not_be_specified_without_maximum
20
+ assert_raises(JSchema::InvalidSchema) do
21
+ build_from_schema('exclusiveMaximum' => false)
22
+ end
23
+ end
24
+
25
+ def test_passing_validation_when_exclusive_maximum_is_not_specified
26
+ assert build_from_schema('maximum' => 3).valid?(2)
27
+ end
28
+
29
+ def test_passing_validation_when_exclusive_maximum_is_false
30
+ schema = build_from_schema('maximum' => 2, 'exclusiveMaximum' => false)
31
+ assert schema.valid?(2)
32
+ end
33
+
34
+ def test_passing_validation_when_exclusive_maximum_is_true
35
+ schema = build_from_schema('maximum' => 3, 'exclusiveMaximum' => true)
36
+ assert schema.valid?(2)
37
+ end
38
+
39
+ def test_failing_validation_when_exclusive_maximum_is_not_specified
40
+ refute build_from_schema('maximum' => 1).valid?(2)
41
+ end
42
+
43
+ def test_failing_validation_when_exclusive_maximum_is_false
44
+ schema = build_from_schema('maximum' => 1, 'exclusiveMaximum' => false)
45
+ refute schema.valid?(2)
46
+ end
47
+
48
+ def test_failing_validation_when_exclusive_maximum_is_true
49
+ schema = build_from_schema('maximum' => 1, 'exclusiveMaximum' => true)
50
+ refute schema.valid?(1)
51
+ end
52
+
53
+ private
54
+
55
+ def validator_class
56
+ JSchema::Validator::Maximum
57
+ end
58
+ end
@@ -0,0 +1,27 @@
1
+ require 'minitest/autorun'
2
+ require 'jschema'
3
+
4
+ require_relative 'assertions'
5
+
6
+ class TestMinItems < Minitest::Test
7
+ include Assertions
8
+
9
+ def test_that_argument_is_non_negative_iteger
10
+ assert_raises_unless_non_negative_integer 'minItems'
11
+ end
12
+
13
+ def test_passing_validation
14
+ assert validator(1).valid?([1, 2])
15
+ assert validator(1).valid?([1])
16
+ end
17
+
18
+ def test_failing_validation
19
+ refute validator(2).valid?([1])
20
+ end
21
+
22
+ private
23
+
24
+ def validator_class
25
+ JSchema::Validator::MinItems
26
+ end
27
+ end
@@ -0,0 +1,30 @@
1
+ require 'minitest/autorun'
2
+ require 'jschema'
3
+
4
+ require_relative 'assertions'
5
+ require_relative '../string_length_validator_tests'
6
+
7
+ class TestMinLength < Minitest::Test
8
+ include Assertions
9
+ include StringLengthValidatorTests
10
+
11
+ def test_that_argument_is_not_a_negative_number
12
+ assert_raises_unless_non_negative_integer 'minLength'
13
+ end
14
+
15
+ def test_passing_validation
16
+ assert validator(4).valid?('test')
17
+ assert validator(3).valid?('test')
18
+ assert validator(0).valid?('')
19
+ end
20
+
21
+ def test_failing_validation
22
+ refute validator(5).valid?('test')
23
+ end
24
+
25
+ private
26
+
27
+ def validator_class
28
+ JSchema::Validator::MinLength
29
+ end
30
+ end
@@ -0,0 +1,29 @@
1
+ require 'minitest/autorun'
2
+ require 'jschema'
3
+
4
+ require_relative 'assertions'
5
+ require_relative 'properties_limit_tests'
6
+
7
+ class TestMinProperties < Minitest::Test
8
+ include Assertions
9
+ include PropertiesLimitTests
10
+
11
+ def test_building_validator_from_schema
12
+ validator = build_from_schema('minProperties' => 10)
13
+ assert_instance_of JSchema::Validator::MinProperties, validator
14
+ end
15
+
16
+ def test_passing_validation
17
+ assert validator(0).valid?('test' => 1)
18
+ end
19
+
20
+ def test_failing_validation
21
+ refute validator(3).valid?('test1' => 1, 'test2' => 2)
22
+ end
23
+
24
+ private
25
+
26
+ def validator_class
27
+ JSchema::Validator::MinProperties
28
+ end
29
+ end