jsi 0.4.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +33 -0
  4. data/LICENSE.md +1 -1
  5. data/README.md +114 -42
  6. data/jsi.gemspec +14 -12
  7. data/lib/jsi/base/node.rb +183 -0
  8. data/lib/jsi/base.rb +388 -220
  9. data/lib/jsi/jsi_coder.rb +8 -7
  10. data/lib/jsi/metaschema.rb +0 -1
  11. data/lib/jsi/metaschema_node/bootstrap_schema.rb +101 -0
  12. data/lib/jsi/metaschema_node.rb +159 -135
  13. data/lib/jsi/ptr.rb +303 -0
  14. data/lib/jsi/schema/application/child_application/contains.rb +25 -0
  15. data/lib/jsi/schema/application/child_application/draft04.rb +22 -0
  16. data/lib/jsi/schema/application/child_application/draft06.rb +29 -0
  17. data/lib/jsi/schema/application/child_application/draft07.rb +29 -0
  18. data/lib/jsi/schema/application/child_application/items.rb +18 -0
  19. data/lib/jsi/schema/application/child_application/properties.rb +25 -0
  20. data/lib/jsi/schema/application/child_application.rb +38 -0
  21. data/lib/jsi/schema/application/draft04.rb +8 -0
  22. data/lib/jsi/schema/application/draft06.rb +8 -0
  23. data/lib/jsi/schema/application/draft07.rb +8 -0
  24. data/lib/jsi/schema/application/inplace_application/dependencies.rb +28 -0
  25. data/lib/jsi/schema/application/inplace_application/draft04.rb +26 -0
  26. data/lib/jsi/schema/application/inplace_application/draft06.rb +27 -0
  27. data/lib/jsi/schema/application/inplace_application/draft07.rb +33 -0
  28. data/lib/jsi/schema/application/inplace_application/ifthenelse.rb +20 -0
  29. data/lib/jsi/schema/application/inplace_application/ref.rb +18 -0
  30. data/lib/jsi/schema/application/inplace_application/someof.rb +44 -0
  31. data/lib/jsi/schema/application/inplace_application.rb +41 -0
  32. data/lib/jsi/schema/application.rb +12 -0
  33. data/lib/jsi/schema/draft04.rb +14 -0
  34. data/lib/jsi/schema/draft06.rb +14 -0
  35. data/lib/jsi/schema/draft07.rb +14 -0
  36. data/lib/jsi/schema/issue.rb +36 -0
  37. data/lib/jsi/schema/ref.rb +160 -0
  38. data/lib/jsi/schema/schema_ancestor_node.rb +113 -0
  39. data/lib/jsi/schema/validation/array.rb +69 -0
  40. data/lib/jsi/schema/validation/const.rb +20 -0
  41. data/lib/jsi/schema/validation/contains.rb +25 -0
  42. data/lib/jsi/schema/validation/core.rb +39 -0
  43. data/lib/jsi/schema/validation/dependencies.rb +49 -0
  44. data/lib/jsi/schema/validation/draft04/minmax.rb +91 -0
  45. data/lib/jsi/schema/validation/draft04.rb +112 -0
  46. data/lib/jsi/schema/validation/draft06.rb +122 -0
  47. data/lib/jsi/schema/validation/draft07.rb +159 -0
  48. data/lib/jsi/schema/validation/enum.rb +25 -0
  49. data/lib/jsi/schema/validation/ifthenelse.rb +46 -0
  50. data/lib/jsi/schema/validation/items.rb +54 -0
  51. data/lib/jsi/schema/validation/not.rb +20 -0
  52. data/lib/jsi/schema/validation/numeric.rb +121 -0
  53. data/lib/jsi/schema/validation/object.rb +45 -0
  54. data/lib/jsi/schema/validation/pattern.rb +34 -0
  55. data/lib/jsi/schema/validation/properties.rb +101 -0
  56. data/lib/jsi/schema/validation/property_names.rb +32 -0
  57. data/lib/jsi/schema/validation/ref.rb +40 -0
  58. data/lib/jsi/schema/validation/required.rb +27 -0
  59. data/lib/jsi/schema/validation/someof.rb +90 -0
  60. data/lib/jsi/schema/validation/string.rb +47 -0
  61. data/lib/jsi/schema/validation/type.rb +49 -0
  62. data/lib/jsi/schema/validation.rb +51 -0
  63. data/lib/jsi/schema.rb +508 -149
  64. data/lib/jsi/schema_classes.rb +199 -59
  65. data/lib/jsi/schema_registry.rb +151 -0
  66. data/lib/jsi/schema_set.rb +181 -0
  67. data/lib/jsi/simple_wrap.rb +23 -4
  68. data/lib/jsi/util/private/attr_struct.rb +127 -0
  69. data/lib/jsi/util/private.rb +204 -0
  70. data/lib/jsi/util/typelike.rb +229 -0
  71. data/lib/jsi/util.rb +89 -53
  72. data/lib/jsi/validation/error.rb +34 -0
  73. data/lib/jsi/validation/result.rb +210 -0
  74. data/lib/jsi/validation.rb +15 -0
  75. data/lib/jsi/version.rb +3 -1
  76. data/lib/jsi.rb +44 -14
  77. data/lib/schemas/json-schema.org/draft-04/schema.rb +10 -3
  78. data/lib/schemas/json-schema.org/draft-06/schema.rb +10 -3
  79. data/lib/schemas/json-schema.org/draft-07/schema.rb +14 -0
  80. data/readme.rb +138 -0
  81. data/{resources}/schemas/json-schema.org/draft-04/schema.json +149 -0
  82. data/{resources}/schemas/json-schema.org/draft-06/schema.json +154 -0
  83. data/{resources}/schemas/json-schema.org/draft-07/schema.json +168 -0
  84. metadata +75 -122
  85. data/.simplecov +0 -3
  86. data/Rakefile.rb +0 -9
  87. data/lib/jsi/base/to_rb.rb +0 -128
  88. data/lib/jsi/json/node.rb +0 -203
  89. data/lib/jsi/json/pointer.rb +0 -419
  90. data/lib/jsi/json-schema-fragments.rb +0 -61
  91. data/lib/jsi/json.rb +0 -10
  92. data/lib/jsi/pathed_node.rb +0 -118
  93. data/lib/jsi/typelike_modules.rb +0 -240
  94. data/resources/icons/AGPL-3.0.png +0 -0
  95. data/test/base_array_test.rb +0 -323
  96. data/test/base_hash_test.rb +0 -337
  97. data/test/base_test.rb +0 -486
  98. data/test/jsi_coder_test.rb +0 -85
  99. data/test/jsi_json_arraynode_test.rb +0 -150
  100. data/test/jsi_json_hashnode_test.rb +0 -132
  101. data/test/jsi_json_node_test.rb +0 -257
  102. data/test/jsi_json_pointer_test.rb +0 -102
  103. data/test/jsi_test.rb +0 -11
  104. data/test/jsi_typelike_as_json_test.rb +0 -53
  105. data/test/metaschema_node_test.rb +0 -19
  106. data/test/schema_module_test.rb +0 -21
  107. data/test/schema_test.rb +0 -208
  108. data/test/spreedly_openapi_test.rb +0 -8
  109. data/test/test_helper.rb +0 -97
  110. data/test/util_test.rb +0 -62
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSI
4
+ module Schema::Validation::Draft04::MinMax
5
+ # @private
6
+ def internal_validate_maximum(result_builder)
7
+ if keyword?('exclusiveMaximum')
8
+ value = schema_content['exclusiveMaximum']
9
+ # The value of "exclusiveMaximum" MUST be a boolean.
10
+ unless [true, false].include?(value)
11
+ result_builder.schema_error('`exclusiveMaximum` is not true or false', 'exclusiveMaximum')
12
+ end
13
+ if !keyword?('maximum')
14
+ result_builder.schema_error('`exclusiveMaximum` has no effect without adjacent `maximum` keyword', 'exclusiveMaximum')
15
+ end
16
+ end
17
+
18
+ if keyword?('maximum')
19
+ value = schema_content['maximum']
20
+ # The value of "maximum" MUST be a JSON number.
21
+ if value.is_a?(Numeric)
22
+ if result_builder.instance.is_a?(Numeric)
23
+ # Successful validation depends on the presence and value of "exclusiveMaximum":
24
+ if schema_content['exclusiveMaximum'] == true
25
+ # if "exclusiveMaximum" has boolean value true, the instance is valid if it is strictly lower
26
+ # than the value of "maximum".
27
+ result_builder.validate(
28
+ result_builder.instance < value,
29
+ 'instance is not less than `maximum` value with `exclusiveMaximum` = true',
30
+ keyword: 'maximum',
31
+ )
32
+ else
33
+ # if "exclusiveMaximum" is not present, or has boolean value false, then the instance is
34
+ # valid if it is lower than, or equal to, the value of "maximum"
35
+ result_builder.validate(
36
+ result_builder.instance <= value,
37
+ 'instance is not less than or equal to `maximum` value',
38
+ keyword: 'maximum',
39
+ )
40
+ end
41
+ end
42
+ else
43
+ result_builder.schema_error('`maximum` is not a number', 'maximum')
44
+ end
45
+ end
46
+ end
47
+
48
+ # @private
49
+ def internal_validate_minimum(result_builder)
50
+ if keyword?('exclusiveMinimum')
51
+ value = schema_content['exclusiveMinimum']
52
+ # The value of "exclusiveMinimum" MUST be a boolean.
53
+ unless [true, false].include?(value)
54
+ result_builder.schema_error('`exclusiveMinimum` is not true or false', 'exclusiveMinimum')
55
+ end
56
+ if !keyword?('minimum')
57
+ result_builder.schema_error('`exclusiveMinimum` has no effect without adjacent `minimum` keyword', 'exclusiveMinimum')
58
+ end
59
+ end
60
+
61
+ if keyword?('minimum')
62
+ value = schema_content['minimum']
63
+ # The value of "minimum" MUST be a JSON number.
64
+ if value.is_a?(Numeric)
65
+ if result_builder.instance.is_a?(Numeric)
66
+ # Successful validation depends on the presence and value of "exclusiveMinimum":
67
+ if schema_content['exclusiveMinimum'] == true
68
+ # if "exclusiveMinimum" is present and has boolean value true, the instance is valid if it is
69
+ # strictly greater than the value of "minimum".
70
+ result_builder.validate(
71
+ result_builder.instance > value,
72
+ 'instance is not greater than `minimum` value with `exclusiveMinimum` = true',
73
+ keyword: 'minimum',
74
+ )
75
+ else
76
+ # if "exclusiveMinimum" is not present, or has boolean value false, then the instance is
77
+ # valid if it is greater than, or equal to, the value of "minimum"
78
+ result_builder.validate(
79
+ result_builder.instance >= value,
80
+ 'instance is not greater than or equal to `minimum` value',
81
+ keyword: 'minimum',
82
+ )
83
+ end
84
+ end
85
+ else
86
+ result_builder.schema_error('`minimum` is not a number', 'minimum')
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSI
4
+ module Schema::Validation::Draft04
5
+ autoload :MinMax, 'jsi/schema/validation/draft04/minmax'
6
+
7
+ include Schema::Validation::Core
8
+
9
+ include Schema::Validation::Ref
10
+
11
+ include Schema::Validation::MultipleOf
12
+ include Schema::Validation::Draft04::MinMax
13
+
14
+ include Schema::Validation::StringLength
15
+ include Schema::Validation::Pattern
16
+
17
+ include Schema::Validation::Items
18
+ include Schema::Validation::ArrayLength
19
+ include Schema::Validation::UniqueItems
20
+
21
+ include Schema::Validation::MinMaxProperties
22
+ include Schema::Validation::Required
23
+ include Schema::Validation::Properties
24
+ include Schema::Validation::Dependencies
25
+
26
+ include Schema::Validation::Enum
27
+ include Schema::Validation::Type
28
+
29
+ include Schema::Validation::AllOf
30
+ include Schema::Validation::AnyOf
31
+ include Schema::Validation::OneOf
32
+ include Schema::Validation::Not
33
+
34
+ # @private
35
+ def internal_validate_keywords(result_builder)
36
+ internal_validate_ref(result_builder, throw_result: true)
37
+
38
+ # 5.1. Validation keywords for numeric instances (number and integer)
39
+
40
+ # 5.1.1. multipleOf
41
+ internal_validate_multipleOf(result_builder)
42
+
43
+ # 5.1.2. maximum and exclusiveMaximum
44
+ internal_validate_maximum(result_builder)
45
+
46
+ # 5.1.3. minimum and exclusiveMinimum
47
+ internal_validate_minimum(result_builder)
48
+
49
+ # 5.2. Validation keywords for strings
50
+
51
+ # 5.2.1. maxLength
52
+ internal_validate_maxLength(result_builder)
53
+
54
+ # 5.2.2. minLength
55
+ internal_validate_minLength(result_builder)
56
+
57
+ # 5.2.3. pattern
58
+ internal_validate_pattern(result_builder)
59
+
60
+ # 5.3. Validation keywords for arrays
61
+
62
+ # 5.3.1. additionalItems and items
63
+ internal_validate_items(result_builder)
64
+
65
+ # 5.3.2. maxItems
66
+ internal_validate_maxItems(result_builder)
67
+
68
+ # 5.3.3. minItems
69
+ internal_validate_minItems(result_builder)
70
+
71
+ # 5.3.4. uniqueItems
72
+ internal_validate_uniqueItems(result_builder)
73
+
74
+ # 5.4. Validation keywords for objects
75
+
76
+ # 5.4.1. maxProperties
77
+ internal_validate_maxProperties(result_builder)
78
+
79
+ # 5.4.2. minProperties
80
+ internal_validate_minProperties(result_builder)
81
+
82
+ # 5.4.3. required
83
+ internal_validate_required(result_builder)
84
+
85
+ # 5.4.4. additionalProperties, properties and patternProperties
86
+ internal_validate_properties(result_builder)
87
+
88
+ # 5.4.5. dependencies
89
+ internal_validate_dependencies(result_builder)
90
+
91
+ # 5.5. Validation keywords for any instance type
92
+
93
+ # 5.5.1. enum
94
+ internal_validate_enum(result_builder)
95
+
96
+ # 5.5.2. type
97
+ internal_validate_type(result_builder)
98
+
99
+ # 5.5.3. allOf
100
+ internal_validate_allOf(result_builder)
101
+
102
+ # 5.5.4. anyOf
103
+ internal_validate_anyOf(result_builder)
104
+
105
+ # 5.5.5. oneOf
106
+ internal_validate_oneOf(result_builder)
107
+
108
+ # 5.5.6. not
109
+ internal_validate_not(result_builder)
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSI
4
+ module Schema::Validation::Draft06
5
+ include Schema::Validation::Core
6
+
7
+ include Schema::Validation::Ref
8
+ include Schema::Validation::AllOf
9
+ include Schema::Validation::AnyOf
10
+ include Schema::Validation::OneOf
11
+ include Schema::Validation::Not
12
+
13
+ include Schema::Validation::Items
14
+ include Schema::Validation::Contains
15
+ include Schema::Validation::Properties
16
+
17
+ include Schema::Validation::MultipleOf
18
+ include Schema::Validation::MinMax
19
+
20
+ include Schema::Validation::StringLength
21
+
22
+ include Schema::Validation::Pattern
23
+
24
+ include Schema::Validation::ArrayLength
25
+ include Schema::Validation::UniqueItems
26
+
27
+ include Schema::Validation::MinMaxProperties
28
+ include Schema::Validation::Required
29
+ include Schema::Validation::Dependencies
30
+ include Schema::Validation::PropertyNames
31
+
32
+ include Schema::Validation::Const
33
+ include Schema::Validation::Enum
34
+
35
+ include Schema::Validation::Type
36
+
37
+ # @private
38
+ def internal_validate_keywords(result_builder)
39
+ # json-schema 8. Schema references with $ref
40
+ internal_validate_ref(result_builder, throw_result: true)
41
+
42
+ # json-schema-validation 6.1. multipleOf
43
+ internal_validate_multipleOf(result_builder)
44
+
45
+ # json-schema-validation 6.2. maximum
46
+ internal_validate_maximum(result_builder)
47
+
48
+ # json-schema-validation 6.3. exclusiveMaximum
49
+ internal_validate_exclusiveMaximum(result_builder)
50
+
51
+ # json-schema-validation 6.4. minimum
52
+ internal_validate_minimum(result_builder)
53
+
54
+ # json-schema-validation 6.5. exclusiveMinimum
55
+ internal_validate_exclusiveMinimum(result_builder)
56
+
57
+ # json-schema-validation 6.6. maxLength
58
+ internal_validate_maxLength(result_builder)
59
+
60
+ # json-schema-validation 6.7. minLength
61
+ internal_validate_minLength(result_builder)
62
+
63
+ # json-schema-validation 6.8. pattern
64
+ internal_validate_pattern(result_builder)
65
+
66
+ # json-schema-validation 6.9. items
67
+ # json-schema-validation 6.10. additionalItems
68
+ internal_validate_items(result_builder)
69
+
70
+ # json-schema-validation 6.11. maxItems
71
+ internal_validate_maxItems(result_builder)
72
+
73
+ # json-schema-validation 6.12. minItems
74
+ internal_validate_minItems(result_builder)
75
+
76
+ # json-schema-validation 6.13. uniqueItems
77
+ internal_validate_uniqueItems(result_builder)
78
+
79
+ # json-schema-validation 6.14. contains
80
+ internal_validate_contains(result_builder)
81
+
82
+ # json-schema-validation 6.15. maxProperties
83
+ internal_validate_maxProperties(result_builder)
84
+
85
+ # json-schema-validation 6.16. minProperties
86
+ internal_validate_minProperties(result_builder)
87
+
88
+ # json-schema-validation 6.17. required
89
+ internal_validate_required(result_builder)
90
+
91
+ # json-schema-validation 6.18. properties
92
+ # json-schema-validation 6.19. patternProperties
93
+ # json-schema-validation 6.20. additionalProperties
94
+ internal_validate_properties(result_builder)
95
+
96
+ # json-schema-validation 6.21. dependencies
97
+ internal_validate_dependencies(result_builder)
98
+
99
+ # json-schema-validation 6.22. propertyNames
100
+ internal_validate_propertyNames(result_builder)
101
+
102
+ # json-schema-validation 6.23. enum
103
+ internal_validate_enum(result_builder)
104
+
105
+ # json-schema-validation 6.24. const
106
+ internal_validate_const(result_builder)
107
+
108
+ # json-schema-validation 6.25. type
109
+ internal_validate_type(result_builder)
110
+
111
+ # json-schema-validation 6.26. allOf
112
+ internal_validate_allOf(result_builder)
113
+ # json-schema-validation 6.27. anyOf
114
+ internal_validate_anyOf(result_builder)
115
+ # json-schema-validation 6.28. oneOf
116
+ internal_validate_oneOf(result_builder)
117
+
118
+ # json-schema-validation 6.29. not
119
+ internal_validate_not(result_builder)
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,159 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSI
4
+ module Schema::Validation::Draft07
5
+ include Schema::Validation::Core
6
+
7
+ include Schema::Validation::Ref
8
+
9
+ include Schema::Validation::Type
10
+ include Schema::Validation::Enum
11
+ include Schema::Validation::Const
12
+
13
+ include Schema::Validation::MultipleOf
14
+ include Schema::Validation::MinMax
15
+
16
+ include Schema::Validation::StringLength
17
+ include Schema::Validation::Pattern
18
+
19
+ include Schema::Validation::Items
20
+ include Schema::Validation::ArrayLength
21
+ include Schema::Validation::UniqueItems
22
+ include Schema::Validation::Contains
23
+
24
+ include Schema::Validation::MinMaxProperties
25
+ include Schema::Validation::Required
26
+
27
+ include Schema::Validation::Properties
28
+ include Schema::Validation::Dependencies
29
+ include Schema::Validation::PropertyNames
30
+
31
+ include Schema::Validation::IfThenElse
32
+ include Schema::Validation::AllOf
33
+ include Schema::Validation::AnyOf
34
+ include Schema::Validation::OneOf
35
+ include Schema::Validation::Not
36
+
37
+ # @private
38
+ def internal_validate_keywords(result_builder)
39
+ internal_validate_ref(result_builder, throw_result: true)
40
+
41
+ # 6.1. Validation Keywords for Any Instance Type
42
+
43
+ # 6.1.1. type
44
+ internal_validate_type(result_builder)
45
+
46
+ # 6.1.2. enum
47
+ internal_validate_enum(result_builder)
48
+
49
+ # 6.1.3. const
50
+ internal_validate_const(result_builder)
51
+
52
+ # 6.2. Validation Keywords for Numeric Instances (number and integer)
53
+
54
+ # 6.2.1. multipleOf
55
+ internal_validate_multipleOf(result_builder)
56
+
57
+ # 6.2.2. maximum
58
+ internal_validate_maximum(result_builder)
59
+
60
+ # 6.2.3. exclusiveMaximum
61
+ internal_validate_exclusiveMaximum(result_builder)
62
+
63
+ # 6.2.4. minimum
64
+ internal_validate_minimum(result_builder)
65
+
66
+ # 6.2.5. exclusiveMinimum
67
+ internal_validate_exclusiveMinimum(result_builder)
68
+
69
+ # 6.3. Validation Keywords for Strings
70
+
71
+ # 6.3.1. maxLength
72
+ internal_validate_maxLength(result_builder)
73
+
74
+ # 6.3.2. minLength
75
+ internal_validate_minLength(result_builder)
76
+
77
+ # 6.3.3. pattern
78
+ internal_validate_pattern(result_builder)
79
+
80
+ # 6.4. Validation Keywords for Arrays
81
+
82
+ # 6.4.1. items
83
+ # 6.4.2. additionalItems
84
+ internal_validate_items(result_builder)
85
+
86
+ # 6.4.3. maxItems
87
+ internal_validate_maxItems(result_builder)
88
+
89
+ # 6.4.4. minItems
90
+ internal_validate_minItems(result_builder)
91
+
92
+ # 6.4.5. uniqueItems
93
+ internal_validate_uniqueItems(result_builder)
94
+
95
+ # 6.4.6. contains
96
+ internal_validate_contains(result_builder)
97
+
98
+ # 6.5. Validation Keywords for Objects
99
+
100
+ # 6.5.1. maxProperties
101
+ internal_validate_maxProperties(result_builder)
102
+
103
+ # 6.5.2. minProperties
104
+ internal_validate_minProperties(result_builder)
105
+
106
+ # 6.5.3. required
107
+ internal_validate_required(result_builder)
108
+
109
+ # 6.5.4. properties
110
+ # 6.5.5. patternProperties
111
+ # 6.5.6. additionalProperties
112
+ internal_validate_properties(result_builder)
113
+
114
+ # 6.5.7. dependencies
115
+ internal_validate_dependencies(result_builder)
116
+
117
+ # 6.5.8. propertyNames
118
+ internal_validate_propertyNames(result_builder)
119
+
120
+ # 6.6. Keywords for Applying Subschemas Conditionally
121
+
122
+ # 6.6.1. if
123
+ # 6.6.2. then
124
+ # 6.6.3. else
125
+ internal_validate_ifthenelse(result_builder)
126
+
127
+ # 6.7. Keywords for Applying Subschemas With Boolean Logic
128
+
129
+ # 6.7.1. allOf
130
+ internal_validate_allOf(result_builder)
131
+
132
+ # 6.7.2. anyOf
133
+ internal_validate_anyOf(result_builder)
134
+
135
+ # 6.7.3. oneOf
136
+ internal_validate_oneOf(result_builder)
137
+
138
+ # 6.7.4. not
139
+ internal_validate_not(result_builder)
140
+
141
+ # 7. Semantic Validation With "format"
142
+ # TODO
143
+
144
+ # 10. Schema Annotations
145
+
146
+ # 10.1. "title" and "description"
147
+ # TODO
148
+
149
+ # 10.2. "default"
150
+ # TODO
151
+
152
+ # 10.3. "readOnly" and "writeOnly"
153
+ # TODO
154
+
155
+ # 10.4. "examples"
156
+ # TODO
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSI
4
+ module Schema::Validation::Enum
5
+ # @private
6
+ def internal_validate_enum(result_builder)
7
+ if keyword?('enum')
8
+ value = schema_content['enum']
9
+ # The value of this keyword MUST be an array. This array SHOULD have at least one element.
10
+ # Elements in the array SHOULD be unique.
11
+ if value.respond_to?(:to_ary)
12
+ # An instance validates successfully against this keyword if its value is equal to one of the
13
+ # elements in this keyword's array value.
14
+ result_builder.validate(
15
+ value.include?(result_builder.instance),
16
+ 'instance is not equal to any `enum` value',
17
+ keyword: 'enum',
18
+ )
19
+ else
20
+ result_builder.schema_error('`enum` is not an array', 'enum')
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSI
4
+ module Schema::Validation::IfThenElse
5
+ # @private
6
+ def internal_validate_ifthenelse(result_builder)
7
+ if keyword?('if')
8
+ # This keyword's value MUST be a valid JSON Schema.
9
+ # This validation outcome of this keyword's subschema has no direct effect on the overall validation
10
+ # result. Rather, it controls which of the "then" or "else" keywords are evaluated.
11
+ if_result = result_builder.inplace_subschema_validate(['if'])
12
+
13
+ result_builder.merge_schema_issues(if_result)
14
+
15
+ if if_result.valid?
16
+ if keyword?('then')
17
+ then_result = result_builder.inplace_subschema_validate(['then'])
18
+ result_builder.validate(
19
+ then_result.valid?,
20
+ 'instance did not validate against the schema defined by `then` value after validating against the schema defined by the `if` value',
21
+ keyword: 'if',
22
+ results: [then_result],
23
+ )
24
+ end
25
+ else
26
+ if keyword?('else')
27
+ else_result = result_builder.inplace_subschema_validate(['else'])
28
+ result_builder.validate(
29
+ else_result.valid?,
30
+ 'instance did not validate against the schema defined by `else` value after not validating against the schema defined by the `if` value',
31
+ keyword: 'if',
32
+ results: [else_result],
33
+ )
34
+ end
35
+ end
36
+ else
37
+ if keyword?('then')
38
+ result_builder.schema_warning('`then` has no effect without adjacent `if` keyword', 'then')
39
+ end
40
+ if keyword?('else')
41
+ result_builder.schema_warning('`else` has no effect without adjacent `if` keyword', 'else')
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSI
4
+ module Schema::Validation::Items
5
+ # @private
6
+ def internal_validate_items(result_builder)
7
+ if keyword?('items')
8
+ value = schema_content['items']
9
+ # The value of "items" MUST be either a valid JSON Schema or an array of valid JSON Schemas.
10
+ if value.respond_to?(:to_ary)
11
+ # If "items" is an array of schemas, validation succeeds if each element of the instance validates
12
+ # against the schema at the same position, if any.
13
+ if result_builder.instance.respond_to?(:to_ary)
14
+ results = {}
15
+ result_builder.instance.each_index do |i|
16
+ if i < value.size
17
+ results[i] = result_builder.child_subschema_validate(['items', i], [i])
18
+ elsif keyword?('additionalItems')
19
+ results[i] = result_builder.child_subschema_validate(['additionalItems'], [i])
20
+ end
21
+ end
22
+ result_builder.validate(
23
+ results.values.all?(&:valid?),
24
+ 'instance array items are not all valid against corresponding `items` or `additionalItems` schema values',
25
+ keyword: 'items',
26
+ results: results.values,
27
+ )
28
+ end
29
+ else
30
+ # If "items" is a schema, validation succeeds if all elements in the array successfully validate
31
+ # against that schema.
32
+ if result_builder.instance.respond_to?(:to_ary)
33
+ results = result_builder.instance.each_index.map do |i|
34
+ result_builder.child_subschema_validate(['items'], [i])
35
+ end
36
+ result_builder.validate(
37
+ results.all?(&:valid?),
38
+ 'instance array items are not all valid against `items` schema value',
39
+ keyword: 'items',
40
+ results: results,
41
+ )
42
+ end
43
+ if keyword?('additionalItems')
44
+ result_builder.schema_warning('`additionalItems` has no effect when adjacent `items` keyword is not an array', 'items')
45
+ end
46
+ end
47
+ else
48
+ if keyword?('additionalItems')
49
+ result_builder.schema_warning('`additionalItems` has no effect without adjacent `items` keyword', 'items')
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSI
4
+ module Schema::Validation::Not
5
+ # @private
6
+ def internal_validate_not(result_builder)
7
+ if keyword?('not')
8
+ # This keyword's value MUST be a valid JSON Schema.
9
+ # An instance is valid against this keyword if it fails to validate successfully against the schema
10
+ # defined by this keyword.
11
+ not_valid = result_builder.inplace_subschema_validate(['not']).valid?
12
+ result_builder.validate(
13
+ !not_valid,
14
+ 'instance is valid against the schema specified as `not` value',
15
+ keyword: 'not',
16
+ )
17
+ end
18
+ end
19
+ end
20
+ end