jsi 0.2.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +1 -1
- data/CHANGELOG.md +36 -0
- data/LICENSE.md +613 -0
- data/README.md +153 -52
- data/lib/jsi/base.rb +485 -338
- data/lib/jsi/jsi_coder.rb +24 -18
- data/lib/jsi/metaschema.rb +7 -0
- data/lib/jsi/metaschema_node/bootstrap_schema.rb +100 -0
- data/lib/jsi/metaschema_node.rb +245 -0
- data/lib/jsi/pathed_node.rb +49 -46
- data/lib/jsi/ptr.rb +292 -0
- data/lib/jsi/schema/application/child_application/contains.rb +16 -0
- data/lib/jsi/schema/application/child_application/draft04.rb +22 -0
- data/lib/jsi/schema/application/child_application/draft06.rb +29 -0
- data/lib/jsi/schema/application/child_application/draft07.rb +29 -0
- data/lib/jsi/schema/application/child_application/items.rb +18 -0
- data/lib/jsi/schema/application/child_application/properties.rb +25 -0
- data/lib/jsi/schema/application/child_application.rb +40 -0
- data/lib/jsi/schema/application/draft04.rb +8 -0
- data/lib/jsi/schema/application/draft06.rb +8 -0
- data/lib/jsi/schema/application/draft07.rb +8 -0
- data/lib/jsi/schema/application/inplace_application/dependencies.rb +28 -0
- data/lib/jsi/schema/application/inplace_application/draft04.rb +26 -0
- data/lib/jsi/schema/application/inplace_application/draft06.rb +27 -0
- data/lib/jsi/schema/application/inplace_application/draft07.rb +33 -0
- data/lib/jsi/schema/application/inplace_application/ifthenelse.rb +20 -0
- data/lib/jsi/schema/application/inplace_application/ref.rb +18 -0
- data/lib/jsi/schema/application/inplace_application/someof.rb +29 -0
- data/lib/jsi/schema/application/inplace_application.rb +46 -0
- data/lib/jsi/schema/application.rb +12 -0
- data/lib/jsi/schema/draft04.rb +14 -0
- data/lib/jsi/schema/draft06.rb +14 -0
- data/lib/jsi/schema/draft07.rb +14 -0
- data/lib/jsi/schema/issue.rb +36 -0
- data/lib/jsi/schema/ref.rb +159 -0
- data/lib/jsi/schema/schema_ancestor_node.rb +119 -0
- data/lib/jsi/schema/validation/array.rb +69 -0
- data/lib/jsi/schema/validation/const.rb +20 -0
- data/lib/jsi/schema/validation/contains.rb +25 -0
- data/lib/jsi/schema/validation/core.rb +39 -0
- data/lib/jsi/schema/validation/dependencies.rb +49 -0
- data/lib/jsi/schema/validation/draft04/minmax.rb +91 -0
- data/lib/jsi/schema/validation/draft04.rb +112 -0
- data/lib/jsi/schema/validation/draft06.rb +122 -0
- data/lib/jsi/schema/validation/draft07.rb +159 -0
- data/lib/jsi/schema/validation/enum.rb +25 -0
- data/lib/jsi/schema/validation/ifthenelse.rb +46 -0
- data/lib/jsi/schema/validation/items.rb +54 -0
- data/lib/jsi/schema/validation/not.rb +20 -0
- data/lib/jsi/schema/validation/numeric.rb +121 -0
- data/lib/jsi/schema/validation/object.rb +45 -0
- data/lib/jsi/schema/validation/pattern.rb +34 -0
- data/lib/jsi/schema/validation/properties.rb +101 -0
- data/lib/jsi/schema/validation/property_names.rb +32 -0
- data/lib/jsi/schema/validation/ref.rb +40 -0
- data/lib/jsi/schema/validation/required.rb +27 -0
- data/lib/jsi/schema/validation/someof.rb +90 -0
- data/lib/jsi/schema/validation/string.rb +47 -0
- data/lib/jsi/schema/validation/type.rb +49 -0
- data/lib/jsi/schema/validation.rb +51 -0
- data/lib/jsi/schema.rb +528 -233
- data/lib/jsi/schema_classes.rb +238 -51
- data/lib/jsi/schema_registry.rb +141 -0
- data/lib/jsi/schema_set.rb +141 -0
- data/lib/jsi/simple_wrap.rb +8 -3
- data/lib/jsi/typelike_modules.rb +75 -68
- data/lib/jsi/util/attr_struct.rb +106 -0
- data/lib/jsi/util.rb +167 -64
- data/lib/jsi/validation/error.rb +34 -0
- data/lib/jsi/validation/result.rb +210 -0
- data/lib/jsi/validation.rb +15 -0
- data/lib/jsi/version.rb +3 -1
- data/lib/jsi.rb +72 -9
- data/lib/schemas/json-schema.org/draft-04/schema.rb +12 -0
- data/lib/schemas/json-schema.org/draft-06/schema.rb +12 -0
- data/lib/schemas/json-schema.org/draft-07/schema.rb +12 -0
- data/readme.rb +138 -0
- data/{resources}/schemas/json-schema.org/draft-04/schema.json +149 -0
- data/{resources}/schemas/json-schema.org/draft-06/schema.json +154 -0
- data/{resources}/schemas/json-schema.org/draft-07/schema.json +168 -0
- metadata +80 -107
- data/.simplecov +0 -1
- data/LICENSE.txt +0 -21
- data/Rakefile.rb +0 -9
- data/jsi.gemspec +0 -31
- data/lib/jsi/base/to_rb.rb +0 -126
- data/lib/jsi/json/node.rb +0 -243
- data/lib/jsi/json/pointer.rb +0 -330
- data/lib/jsi/json-schema-fragments.rb +0 -59
- data/lib/jsi/json.rb +0 -8
- data/test/base_array_test.rb +0 -209
- data/test/base_hash_test.rb +0 -204
- data/test/base_test.rb +0 -422
- data/test/jsi_coder_test.rb +0 -85
- data/test/jsi_json_arraynode_test.rb +0 -150
- data/test/jsi_json_hashnode_test.rb +0 -132
- data/test/jsi_json_node_test.rb +0 -310
- data/test/jsi_json_pointer_test.rb +0 -106
- data/test/jsi_test.rb +0 -11
- data/test/jsi_typelike_as_json_test.rb +0 -53
- data/test/schema_test.rb +0 -196
- data/test/spreedly_openapi_test.rb +0 -8
- data/test/test_helper.rb +0 -63
- data/test/util_test.rb +0 -62
data/lib/jsi/schema.rb
CHANGED
@@ -1,311 +1,606 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module JSI
|
4
|
-
# JSI::Schema
|
5
|
-
#
|
6
|
-
#
|
7
|
-
|
8
|
-
|
4
|
+
# JSI::Schema is a module which extends instances which represent JSON schemas.
|
5
|
+
#
|
6
|
+
# the content of an instance which is a JSI::Schema (referred to in this context as schema_content) is
|
7
|
+
# expected to be a Hash (JSON object) or a Boolean.
|
8
|
+
module Schema
|
9
|
+
autoload :Application, 'jsi/schema/application'
|
10
|
+
autoload :Validation, 'jsi/schema/validation'
|
11
|
+
autoload :Issue, 'jsi/schema/issue'
|
9
12
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
autoload :SchemaAncestorNode, 'jsi/schema/schema_ancestor_node'
|
14
|
+
|
15
|
+
autoload :Ref, 'jsi/schema/ref'
|
16
|
+
|
17
|
+
autoload :Draft04, 'jsi/schema/draft04'
|
18
|
+
autoload :Draft06, 'jsi/schema/draft06'
|
19
|
+
autoload :Draft07, 'jsi/schema/draft07'
|
20
|
+
|
21
|
+
class Error < StandardError
|
22
|
+
end
|
23
|
+
|
24
|
+
# an exception raised when a thing is expected to be a JSI::Schema, but is not
|
25
|
+
class NotASchemaError < Error
|
26
|
+
end
|
27
|
+
|
28
|
+
# an exception raised when we are unable to resolve a schema reference
|
29
|
+
class ReferenceError < StandardError
|
30
|
+
end
|
31
|
+
|
32
|
+
# extends any schema which uses the keyword '$id' to identify its canonical URI
|
33
|
+
module BigMoneyId
|
34
|
+
# the contents of a $id keyword whose value is a string, or nil
|
35
|
+
# @return [#to_str, nil]
|
36
|
+
def id
|
37
|
+
if schema_content.respond_to?(:to_hash) && schema_content['$id'].respond_to?(:to_str)
|
38
|
+
schema_content['$id']
|
17
39
|
else
|
18
|
-
|
40
|
+
nil
|
19
41
|
end
|
20
42
|
end
|
21
43
|
end
|
22
44
|
|
23
|
-
#
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
@schema_node = JSI::JSON::Node.new_doc(JSI.deep_stringify_symbol_keys(schema_object))
|
34
|
-
elsif schema_object == true
|
35
|
-
@schema_node = JSI::JSON::Node.new_doc({})
|
36
|
-
elsif schema_object == false
|
37
|
-
@schema_node = JSI::JSON::Node.new_doc({"not" => {}})
|
38
|
-
else
|
39
|
-
raise(TypeError, "cannot instantiate Schema from: #{schema_object.pretty_inspect.chomp}")
|
45
|
+
# extends any schema which uses the keyword 'id' to identify its canonical URI
|
46
|
+
module OldId
|
47
|
+
# the contents of an `id` keyword whose value is a string, or nil
|
48
|
+
# @return [#to_str, nil]
|
49
|
+
def id
|
50
|
+
if schema_content.respond_to?(:to_hash) && schema_content['id'].respond_to?(:to_str)
|
51
|
+
schema_content['id']
|
52
|
+
else
|
53
|
+
nil
|
54
|
+
end
|
40
55
|
end
|
41
56
|
end
|
42
57
|
|
43
|
-
#
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
parent_id = node_for_id['$id'] || node_for_id['id']
|
73
|
-
else
|
74
|
-
# will look at '$id' everywhere since it is less likely to show up outside schemas than
|
75
|
-
# 'id', but it will be better to only look at parents that are schemas for this too.
|
76
|
-
parent_id = node_for_id['$id']
|
77
|
-
end
|
58
|
+
# extends any schema which defines an anchor as a URI fragment in the schema id
|
59
|
+
module IdWithAnchor
|
60
|
+
# a URI for the schema's id, unless the id defines an anchor in its
|
61
|
+
# fragment. nil if the schema defines no id.
|
62
|
+
# @return [Addressable::URI, nil]
|
63
|
+
def id_without_fragment
|
64
|
+
if id
|
65
|
+
id_uri = Addressable::URI.parse(id)
|
66
|
+
if id_uri.merge(fragment: nil).empty?
|
67
|
+
# fragment-only id is just an anchor
|
68
|
+
# e.g. #foo
|
69
|
+
nil
|
70
|
+
elsif id_uri.fragment == nil
|
71
|
+
# no fragment
|
72
|
+
# e.g. http://localhost:1234/bar
|
73
|
+
id_uri
|
74
|
+
elsif id_uri.fragment == ''
|
75
|
+
# empty fragment
|
76
|
+
# e.g. http://json-schema.org/draft-07/schema#
|
77
|
+
id_uri.merge(fragment: nil)
|
78
|
+
elsif jsi_schema_base_uri && jsi_schema_base_uri.join(id_uri).merge(fragment: nil) == jsi_schema_base_uri
|
79
|
+
# the id, resolved against the base uri, consists of the base uri plus an anchor fragment.
|
80
|
+
# so there's no non-fragment id.
|
81
|
+
# e.g. base uri is http://localhost:1234/bar
|
82
|
+
# and id is http://localhost:1234/bar#foo
|
83
|
+
nil
|
84
|
+
else
|
85
|
+
# e.g. http://localhost:1234/bar#foo
|
86
|
+
id_uri.merge(fragment: nil)
|
78
87
|
end
|
88
|
+
else
|
89
|
+
nil
|
90
|
+
end
|
91
|
+
end
|
79
92
|
|
80
|
-
|
81
|
-
|
93
|
+
# an anchor defined by a non-empty fragment in the id uri
|
94
|
+
# @return [String]
|
95
|
+
def anchor
|
96
|
+
if id
|
97
|
+
id_uri = Addressable::URI.parse(id)
|
98
|
+
if id_uri.fragment == ''
|
99
|
+
nil
|
82
100
|
else
|
83
|
-
|
84
|
-
node_for_id = node_for_id.parent_node
|
101
|
+
id_uri.fragment
|
85
102
|
end
|
86
|
-
end
|
87
|
-
if parent_id
|
88
|
-
parent_auri = Addressable::URI.parse(parent_id)
|
89
103
|
else
|
90
|
-
|
91
|
-
validator = ::JSON::Validator.new(Typelike.as_json(node_for_id), nil)
|
92
|
-
# TODO not good instance_exec'ing into another library's ivars
|
93
|
-
parent_auri = validator.instance_exec { @base_schema }.uri
|
94
|
-
end
|
95
|
-
if parent_auri.fragment
|
96
|
-
# add onto the fragment
|
97
|
-
parent_id_path = JSI::JSON::Pointer.from_fragment('#' + parent_auri.fragment).reference_tokens
|
98
|
-
path_from_id_node = parent_id_path + path_from_id_node
|
99
|
-
parent_auri.fragment = nil
|
100
|
-
#else: no fragment so parent_id good as is
|
104
|
+
nil
|
101
105
|
end
|
102
|
-
|
103
|
-
fragment = JSI::JSON::Pointer.new(path_from_id_node).fragment
|
104
|
-
schema_id = parent_auri.to_s + fragment
|
105
|
-
|
106
|
-
schema_id
|
107
106
|
end
|
108
107
|
end
|
109
108
|
|
110
|
-
# @
|
111
|
-
|
112
|
-
|
109
|
+
# @private
|
110
|
+
module IntegerAllows0Fraction
|
111
|
+
# is `value` an integer?
|
112
|
+
# @private
|
113
|
+
# @param value
|
114
|
+
# @return [Boolean]
|
115
|
+
def internal_integer?(value)
|
116
|
+
value.is_a?(Integer) || (value.is_a?(Numeric) && value % 1.0 == 0.0)
|
117
|
+
end
|
113
118
|
end
|
114
119
|
|
115
|
-
|
120
|
+
# @private
|
121
|
+
module IntegerDisallows0Fraction
|
122
|
+
# is `value` an integer?
|
123
|
+
# @private
|
124
|
+
# @param value
|
125
|
+
# @return [Boolean]
|
126
|
+
def internal_integer?(value)
|
127
|
+
value.is_a?(Integer)
|
128
|
+
end
|
129
|
+
end
|
116
130
|
|
117
|
-
#
|
118
|
-
#
|
131
|
+
# JSI::Schema::DescribesSchema: a schema which describes another schema. this module
|
132
|
+
# extends a JSI::Schema instance and indicates that JSIs which instantiate the schema
|
133
|
+
# are themselves also schemas.
|
119
134
|
#
|
120
|
-
#
|
121
|
-
|
122
|
-
|
135
|
+
# examples of a schema which describes a schema include the draft JSON Schema metaschemas and
|
136
|
+
# the OpenAPI schema definition which describes "A deterministic version of a JSON Schema object."
|
137
|
+
module DescribesSchema
|
138
|
+
# instantiates the given schema content as a JSI Schema.
|
139
|
+
#
|
140
|
+
# the schema is instantiated after recursively converting any symbol hash keys in the structure
|
141
|
+
# to strings. note that this is in contrast to {JSI::Schema#new_jsi}, which does not alter its
|
142
|
+
# given instance.
|
143
|
+
#
|
144
|
+
# the schema will be registered with the `JSI.schema_registry`.
|
145
|
+
#
|
146
|
+
# @param schema_content [#to_hash, Boolean] an object to be instantiated as a schema
|
147
|
+
# @param uri [nil, #to_str, Addressable::URI] the URI of the schema document.
|
148
|
+
# relative URIs within the document are resolved using this uri as their base.
|
149
|
+
# the result schema will be registered with this URI in the {JSI.schema_registry}.
|
150
|
+
# @return [JSI::Base, JSI::Schema] a JSI whose instance is the given schema_content and whose schemas
|
151
|
+
# are inplace applicators matched from self to the schema being instantiated.
|
152
|
+
def new_schema(schema_content,
|
153
|
+
uri: nil
|
154
|
+
)
|
155
|
+
schema_jsi = new_jsi(Util.deep_stringify_symbol_keys(schema_content),
|
156
|
+
uri: uri,
|
157
|
+
)
|
158
|
+
JSI.schema_registry.register(schema_jsi)
|
159
|
+
schema_jsi
|
160
|
+
end
|
161
|
+
|
162
|
+
# instantiates a given schema object as a JSI Schema and returns its JSI Schema Module.
|
163
|
+
#
|
164
|
+
# shortcut to chain {#new_schema} + {Schema#jsi_schema_module}.
|
165
|
+
#
|
166
|
+
# @param (see #new_schema)
|
167
|
+
# @return [Module, JSI::SchemaModule] the JSI Schema Module of the schema
|
168
|
+
def new_schema_module(schema_content, **kw)
|
169
|
+
new_schema(schema_content, **kw).jsi_schema_module
|
170
|
+
end
|
123
171
|
end
|
124
172
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
#
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
173
|
+
class << self
|
174
|
+
# an application-wide default metaschema set by {default_metaschema=}, used by {JSI.new_schema}
|
175
|
+
#
|
176
|
+
# @return [nil, #new_schema]
|
177
|
+
def default_metaschema
|
178
|
+
return @default_metaschema if instance_variable_defined?(:@default_metaschema)
|
179
|
+
return JSONSchemaOrgDraft07
|
180
|
+
end
|
181
|
+
|
182
|
+
# sets an application-wide default metaschema used by {JSI.new_schema}
|
183
|
+
#
|
184
|
+
# @param default_metaschema [#new_schema] the default metaschema. this may be a metaschema or a
|
185
|
+
# metaschema's schema module (e.g. `JSI::JSONSchemaOrgDraft07`).
|
186
|
+
def default_metaschema=(default_metaschema)
|
187
|
+
unless default_metaschema.respond_to?(:new_schema)
|
188
|
+
raise(TypeError, "given default_metaschema does not respond to #new_schema")
|
141
189
|
end
|
190
|
+
@default_metaschema = default_metaschema
|
142
191
|
end
|
143
|
-
return self
|
144
|
-
end
|
145
192
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
193
|
+
# instantiates a given schema object as a JSI Schema.
|
194
|
+
#
|
195
|
+
# the metaschema to use to instantiate the schema must be indicated.
|
196
|
+
#
|
197
|
+
# - if the schema object has a `$schema` property, that URI is resolved using the {JSI.schema_registry},
|
198
|
+
# and that metaschema is used.
|
199
|
+
# - if no `$schema` property is present, the `default_metaschema` param is used, if the caller
|
200
|
+
# specifies it.
|
201
|
+
# - if no `default_metaschema` param is specified, the application-wide default
|
202
|
+
# {JSI::Schema.default_metaschema JSI::Schema.default_metaschema} is used,
|
203
|
+
# if the application has set it.
|
204
|
+
#
|
205
|
+
# an ArgumentError is raised if none of these indicate a metaschema to use.
|
206
|
+
#
|
207
|
+
# note that if you are instantiating a schema known to have no `$schema` property, an alternative to
|
208
|
+
# passing the `default_metaschema` param is to use `.new_schema` on the metaschema or its module, e.g.
|
209
|
+
# `JSI::JSONSchemaOrgDraft07.new_schema(my_schema_object)`
|
210
|
+
#
|
211
|
+
# if the given schema_object is a JSI::Base but not already a JSI::Schema, an error
|
212
|
+
# will be raised. schemas which describe schemas must have JSI::Schema in their
|
213
|
+
# Schema#jsi_schema_instance_modules.
|
214
|
+
#
|
215
|
+
# @param schema_object [#to_hash, Boolean, JSI::Schema] an object to be instantiated as a schema.
|
216
|
+
# if it's already a JSI::Schema, it is returned as-is.
|
217
|
+
# @param uri (see DescribesSchema#new_schema)
|
218
|
+
# @param default_metaschema [#new_schema] the metaschema to use if the schema_object does not have
|
219
|
+
# a '$schema' property. this may be a metaschema or a metaschema's schema module
|
220
|
+
# (e.g. `JSI::JSONSchemaOrgDraft07`).
|
221
|
+
# @return [JSI::Schema] a JSI::Schema representing the given schema_object
|
222
|
+
def new_schema(schema_object, default_metaschema: nil, **kw)
|
223
|
+
default_metaschema_new_schema = -> {
|
224
|
+
default_metaschema ||= JSI::Schema.default_metaschema
|
225
|
+
if default_metaschema.nil?
|
226
|
+
raise(ArgumentError, [
|
227
|
+
"when instantiating a schema with no `$schema` property, you must specify the metaschema.",
|
228
|
+
"you may pass the `default_metaschema` param to this method.",
|
229
|
+
"JSI::Schema.default_metaschema may be set to an application-wide default metaschema.",
|
230
|
+
"you may alternatively use new_schema on the appropriate metaschema or its schema module.",
|
231
|
+
"instantiating schema_object: #{schema_object.pretty_inspect.chomp}",
|
232
|
+
].join("\n"))
|
233
|
+
end
|
234
|
+
if !default_metaschema.respond_to?(:new_schema)
|
235
|
+
raise(TypeError, "given default_metaschema does not respond to #new_schema: #{default_metaschema.pretty_inspect.chomp}")
|
236
|
+
end
|
237
|
+
default_metaschema.new_schema(schema_object, **kw)
|
238
|
+
}
|
239
|
+
if schema_object.is_a?(Schema)
|
240
|
+
schema_object
|
241
|
+
elsif schema_object.is_a?(JSI::Base)
|
242
|
+
raise(NotASchemaError, "the given schema_object is a JSI::Base, but is not a JSI::Schema: #{schema_object.pretty_inspect.chomp}")
|
243
|
+
elsif schema_object.respond_to?(:to_hash)
|
244
|
+
if schema_object.key?('$schema') && schema_object['$schema'].respond_to?(:to_str)
|
245
|
+
metaschema = Schema::Ref.new(schema_object['$schema']).deref_schema
|
246
|
+
unless metaschema.describes_schema?
|
247
|
+
raise(Schema::ReferenceError, "given schema_object contains a $schema but the resource it identifies does not describe a schema")
|
158
248
|
end
|
249
|
+
metaschema.new_schema(schema_object, **kw)
|
250
|
+
else
|
251
|
+
default_metaschema_new_schema.call
|
159
252
|
end
|
160
|
-
|
161
|
-
|
253
|
+
elsif [true, false].include?(schema_object)
|
254
|
+
default_metaschema_new_schema.call
|
255
|
+
else
|
256
|
+
raise(TypeError, "cannot instantiate Schema from: #{schema_object.pretty_inspect.chomp}")
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
# @deprecated
|
261
|
+
alias_method :new, :new_schema
|
262
|
+
|
263
|
+
# @deprecated
|
264
|
+
alias_method :from_object, :new_schema
|
265
|
+
|
266
|
+
# ensure the given object is a JSI Schema
|
267
|
+
#
|
268
|
+
# @param schema [Object] the thing the caller wishes to ensure is a Schema
|
269
|
+
# @param msg [#to_s, #to_ary] lines of the error message preceding the pretty-printed schema param
|
270
|
+
# if the schema param is not a schema
|
271
|
+
# @raise [NotASchemaError] if the schema param is not a schema
|
272
|
+
# @return [Schema] the given schema
|
273
|
+
def ensure_schema(schema, msg: "indicated object is not a schema:", reinstantiate_as: nil)
|
274
|
+
if schema.is_a?(Schema)
|
275
|
+
schema
|
276
|
+
else
|
277
|
+
if reinstantiate_as
|
278
|
+
# TODO warn; behavior is undefined and I hate this implementation
|
279
|
+
|
280
|
+
result_schema_schemas = schema.jsi_schemas + reinstantiate_as
|
281
|
+
|
282
|
+
result_schema_class = JSI::SchemaClasses.class_for_schemas(result_schema_schemas)
|
283
|
+
|
284
|
+
result_schema_class.new(Base::NOINSTANCE,
|
285
|
+
jsi_document: schema.jsi_document,
|
286
|
+
jsi_ptr: schema.jsi_ptr,
|
287
|
+
jsi_root_node: schema.jsi_root_node,
|
288
|
+
jsi_schema_base_uri: schema.jsi_schema_base_uri,
|
289
|
+
jsi_schema_resource_ancestors: schema.jsi_schema_resource_ancestors,
|
290
|
+
)
|
162
291
|
else
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
end
|
292
|
+
raise(NotASchemaError, [
|
293
|
+
*msg,
|
294
|
+
schema.pretty_inspect.chomp,
|
295
|
+
].join("\n"))
|
168
296
|
end
|
169
297
|
end
|
170
298
|
end
|
171
299
|
end
|
172
300
|
|
173
|
-
#
|
174
|
-
#
|
175
|
-
#
|
176
|
-
def
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
301
|
+
# the underlying JSON data used to instantiate this JSI::Schema.
|
302
|
+
# this is an alias for PathedNode#jsi_node_content, named for clarity in the context of working with
|
303
|
+
# a schema.
|
304
|
+
def schema_content
|
305
|
+
jsi_node_content
|
306
|
+
end
|
307
|
+
|
308
|
+
# the URI of this schema, calculated from our `#id`, resolved against our `#jsi_schema_base_uri`
|
309
|
+
# @return [Addressable::URI, nil]
|
310
|
+
def schema_absolute_uri
|
311
|
+
if respond_to?(:id_without_fragment) && id_without_fragment
|
312
|
+
if jsi_schema_base_uri
|
313
|
+
Addressable::URI.parse(jsi_schema_base_uri).join(id_without_fragment)
|
314
|
+
elsif id_without_fragment.absolute?
|
315
|
+
id_without_fragment
|
186
316
|
else
|
317
|
+
# TODO warn / schema_error
|
187
318
|
nil
|
188
319
|
end
|
189
320
|
end
|
190
321
|
end
|
191
322
|
|
192
|
-
#
|
193
|
-
#
|
194
|
-
#
|
195
|
-
#
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
323
|
+
# a nonrelative URI which refers to this schema.
|
324
|
+
# nil if no parent of this schema defines an id.
|
325
|
+
# see {#schema_uris} for all URIs known to refer to this schema.
|
326
|
+
# @return [Addressable::URI, nil]
|
327
|
+
def schema_uri
|
328
|
+
schema_uris.first
|
329
|
+
end
|
330
|
+
|
331
|
+
# nonrelative URIs (that is, absolute, but possibly with a fragment) which refer to this schema
|
332
|
+
# @return [Array<Addressable::URI>]
|
333
|
+
def schema_uris
|
334
|
+
jsi_memoize(:schema_uris) do
|
335
|
+
each_schema_uri.to_a
|
336
|
+
end
|
337
|
+
end
|
338
|
+
|
339
|
+
# see {#schema_uris}
|
340
|
+
# @yield [Addressable::URI]
|
341
|
+
# @return [Enumerator, nil]
|
342
|
+
def each_schema_uri
|
343
|
+
return to_enum(__method__) unless block_given?
|
344
|
+
|
345
|
+
yield schema_absolute_uri if schema_absolute_uri
|
346
|
+
|
347
|
+
parent_schemas = jsi_subschema_resource_ancestors.reverse_each.select do |resource|
|
348
|
+
resource.is_a?(Schema) && resource.schema_absolute_uri
|
349
|
+
end
|
350
|
+
|
351
|
+
anchored = self.anchor
|
352
|
+
parent_schemas.each do |parent_schema|
|
353
|
+
if anchored
|
354
|
+
if parent_schema.jsi_anchor_subschema(anchor) == self
|
355
|
+
yield parent_schema.schema_absolute_uri.merge(fragment: anchor)
|
356
|
+
else
|
357
|
+
anchored = false
|
213
358
|
end
|
214
359
|
end
|
360
|
+
|
361
|
+
relative_ptr = self.jsi_ptr.ptr_relative_to(parent_schema.jsi_ptr)
|
362
|
+
yield parent_schema.schema_absolute_uri.merge(fragment: relative_ptr.fragment)
|
215
363
|
end
|
364
|
+
|
365
|
+
nil
|
366
|
+
end
|
367
|
+
|
368
|
+
# a module which extends all instances of this schema. this may be opened by the application to add
|
369
|
+
# methods to schema instances.
|
370
|
+
#
|
371
|
+
# this module includes accessor methods for object property names this schema
|
372
|
+
# describes (see {#described_object_property_names}). these accessors wrap {Base#[]} and {Base#[]=}.
|
373
|
+
#
|
374
|
+
# some functionality is also defined on the module itself (its singleton class, not for its instances):
|
375
|
+
#
|
376
|
+
# - the module is extended with {JSI::SchemaModule}, which defines .new_jsi to instantiate instances
|
377
|
+
# of this schema (see {#new_jsi}).
|
378
|
+
# - properties described by this schema's metaschema are defined as methods to get subschemas' schema
|
379
|
+
# modules, so for example `schema.jsi_schema_module.items` returns the same module
|
380
|
+
# as `schema.items.jsi_schema_module`.
|
381
|
+
# - method .schema which returns this schema.
|
382
|
+
#
|
383
|
+
# @return [Module]
|
384
|
+
def jsi_schema_module
|
385
|
+
JSI::SchemaClasses.module_for_schema(self)
|
386
|
+
end
|
387
|
+
|
388
|
+
# Evaluates the given block in the context of this schema's JSI schema module.
|
389
|
+
# Any arguments passed to this method will be passed to the block.
|
390
|
+
# shortcut to invoke [Module#module_exec](https://ruby-doc.org/core/Module.html#method-i-module_exec)
|
391
|
+
# on our {#jsi_schema_module}.
|
392
|
+
#
|
393
|
+
# @return the result of evaluating the block
|
394
|
+
def jsi_schema_module_exec(*a, **kw, &block)
|
395
|
+
jsi_schema_module.module_exec(*a, **kw, &block)
|
396
|
+
end
|
397
|
+
|
398
|
+
# @private @deprecated
|
399
|
+
def jsi_schema_class
|
400
|
+
JSI::SchemaClasses.class_for_schemas(SchemaSet[self])
|
401
|
+
end
|
402
|
+
|
403
|
+
# instantiates the given instance as a JSI::Base class for schemas matched from this schema to the
|
404
|
+
# instance.
|
405
|
+
#
|
406
|
+
# @param instance [Object] the JSON Schema instance to be represented as a JSI
|
407
|
+
# @param uri (see SchemaSet#new_jsi)
|
408
|
+
# @return [JSI::Base subclass] a JSI whose instance is the given instance and whose schemas are matched
|
409
|
+
# from this schema.
|
410
|
+
def new_jsi(instance,
|
411
|
+
**kw
|
412
|
+
)
|
413
|
+
SchemaSet[self].new_jsi(instance, **kw)
|
414
|
+
end
|
415
|
+
|
416
|
+
# does this schema itself describe a schema?
|
417
|
+
# @return [Boolean]
|
418
|
+
def describes_schema?
|
419
|
+
jsi_schema_instance_modules.any? { |m| m <= JSI::Schema }
|
420
|
+
end
|
421
|
+
|
422
|
+
# modules to apply to instances described by this schema. these modules are included
|
423
|
+
# on this schema's {#jsi_schema_module}
|
424
|
+
# @return [Set<Module>]
|
425
|
+
def jsi_schema_instance_modules
|
426
|
+
return @jsi_schema_instance_modules if instance_variable_defined?(:@jsi_schema_instance_modules)
|
427
|
+
return Set[].freeze
|
428
|
+
end
|
429
|
+
|
430
|
+
# see {#jsi_schema_instance_modules}
|
431
|
+
#
|
432
|
+
# @return [void]
|
433
|
+
def jsi_schema_instance_modules=(jsi_schema_instance_modules)
|
434
|
+
@jsi_schema_instance_modules = Util.ensure_module_set(jsi_schema_instance_modules)
|
435
|
+
end
|
436
|
+
|
437
|
+
# a resource containing this schema.
|
438
|
+
#
|
439
|
+
# if any parent, or this schema itself, is a schema with an absolute uri (see #schema_absolute_uri),
|
440
|
+
# the resource root is the closest schema with an absolute uri.
|
441
|
+
#
|
442
|
+
# if no parent schema has an absolute uri, the schema_resource_root is the root of the document
|
443
|
+
# (our #jsi_root_node). in this case, the resource root may or may not be a schema itself.
|
444
|
+
#
|
445
|
+
# @return [JSI::Base] resource containing this schema
|
446
|
+
def schema_resource_root
|
447
|
+
jsi_subschema_resource_ancestors.reverse_each.detect(&:schema_resource_root?) || jsi_root_node
|
216
448
|
end
|
217
449
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
450
|
+
# is this schema the root of a schema resource?
|
451
|
+
# @return [Boolean]
|
452
|
+
def schema_resource_root?
|
453
|
+
jsi_ptr.root? || !!schema_absolute_uri
|
454
|
+
end
|
455
|
+
|
456
|
+
# a subschema of this Schema
|
457
|
+
#
|
458
|
+
# @param subptr [JSI::Ptr, #to_ary] a relative pointer, or array of tokens, pointing to the subschema
|
459
|
+
# @return [JSI::Schema] the subschema at the location indicated by subptr. self if subptr is empty.
|
460
|
+
def subschema(subptr)
|
461
|
+
subschema_map[Ptr.ary_ptr(subptr)]
|
462
|
+
end
|
463
|
+
|
464
|
+
private
|
465
|
+
|
466
|
+
def subschema_map
|
467
|
+
jsi_memomap(:subschema) do |subptr|
|
468
|
+
if is_a?(MetaschemaNode::BootstrapSchema)
|
469
|
+
self.class.new(
|
470
|
+
jsi_document,
|
471
|
+
jsi_ptr: jsi_ptr + subptr,
|
472
|
+
jsi_schema_base_uri: jsi_resource_ancestor_uri,
|
473
|
+
)
|
222
474
|
else
|
223
|
-
|
475
|
+
Schema.ensure_schema(subptr.evaluate(self, as_jsi: true), msg: [
|
476
|
+
"subschema is not a schema at pointer: #{subptr.pointer}"
|
477
|
+
])
|
224
478
|
end
|
225
|
-
else
|
226
|
-
nil
|
227
479
|
end
|
228
480
|
end
|
229
481
|
|
230
|
-
|
231
|
-
|
482
|
+
public
|
483
|
+
|
484
|
+
# a schema in the same schema resource as this one (see #schema_resource_root) at the given
|
485
|
+
# pointer relative to the root of the schema resource.
|
486
|
+
#
|
487
|
+
# @param ptr [JSI::Ptr, #to_ary] a pointer to a schema from our schema resource root
|
488
|
+
# @return [JSI::Schema] the schema pointed to by ptr
|
489
|
+
def resource_root_subschema(ptr)
|
490
|
+
resource_root_subschema_map[Ptr.ary_ptr(ptr)]
|
232
491
|
end
|
233
492
|
|
234
|
-
|
235
|
-
|
236
|
-
def
|
237
|
-
|
493
|
+
private
|
494
|
+
|
495
|
+
def resource_root_subschema_map
|
496
|
+
jsi_memomap(:resource_root_subschema_map) do |ptr|
|
497
|
+
schema = self
|
498
|
+
if schema.is_a?(MetaschemaNode::BootstrapSchema)
|
499
|
+
# BootstrapSchema does not track jsi_schema_resource_ancestors used by #schema_resource_root;
|
500
|
+
# resource_root_subschema is always relative to the document root.
|
501
|
+
# BootstrapSchema also does not implement jsi_root_node or #[]. we instantiate the ptr directly
|
502
|
+
# rather than as a subschema from the root.
|
503
|
+
schema.class.new(
|
504
|
+
schema.jsi_document,
|
505
|
+
jsi_ptr: ptr,
|
506
|
+
jsi_schema_base_uri: nil,
|
507
|
+
)
|
508
|
+
else
|
509
|
+
resource_root = schema.schema_resource_root
|
510
|
+
Schema.ensure_schema(ptr.evaluate(resource_root, as_jsi: true),
|
511
|
+
msg: [
|
512
|
+
"subschema is not a schema at pointer: #{ptr.pointer}"
|
513
|
+
],
|
514
|
+
reinstantiate_as: schema.jsi_schemas.select(&:describes_schema?)
|
515
|
+
)
|
516
|
+
end
|
517
|
+
end
|
238
518
|
end
|
239
519
|
|
240
|
-
|
241
|
-
|
242
|
-
|
520
|
+
public
|
521
|
+
|
522
|
+
# any object property names this schema indicates may be present on its instances.
|
523
|
+
# this includes any keys of this schema's "properties" object and any entries of this schema's
|
524
|
+
# array of "required" property keys.
|
525
|
+
# @return [Set]
|
526
|
+
def described_object_property_names
|
527
|
+
jsi_memoize(:described_object_property_names) do
|
528
|
+
Set.new.tap do |property_names|
|
529
|
+
if schema_content.respond_to?(:to_hash) && schema_content['properties'].respond_to?(:to_hash)
|
530
|
+
property_names.merge(schema_content['properties'].keys)
|
531
|
+
end
|
532
|
+
if schema_content.respond_to?(:to_hash) && schema_content['required'].respond_to?(:to_ary)
|
533
|
+
property_names.merge(schema_content['required'].to_ary)
|
534
|
+
end
|
535
|
+
end.freeze
|
536
|
+
end
|
243
537
|
end
|
244
538
|
|
245
|
-
#
|
246
|
-
#
|
247
|
-
# @
|
248
|
-
#
|
249
|
-
def
|
250
|
-
|
539
|
+
# validates the given instance against this schema
|
540
|
+
#
|
541
|
+
# @param instance [Object] the instance to validate against this schema
|
542
|
+
# @return [JSI::Validation::Result]
|
543
|
+
def instance_validate(instance)
|
544
|
+
if instance.is_a?(JSI::PathedNode)
|
545
|
+
instance_ptr = instance.jsi_ptr
|
546
|
+
instance_document = instance.jsi_document
|
547
|
+
else
|
548
|
+
instance_ptr = Ptr[]
|
549
|
+
instance_document = instance
|
550
|
+
end
|
551
|
+
internal_validate_instance(instance_ptr, instance_document)
|
251
552
|
end
|
252
553
|
|
253
|
-
#
|
254
|
-
#
|
255
|
-
#
|
256
|
-
def
|
257
|
-
|
554
|
+
# whether the given instance is valid against this schema
|
555
|
+
# @param instance [Object] the instance to validate against this schema
|
556
|
+
# @return [Boolean]
|
557
|
+
def instance_valid?(instance)
|
558
|
+
if instance.is_a?(JSI::PathedNode)
|
559
|
+
instance = instance.jsi_node_content
|
560
|
+
end
|
561
|
+
internal_validate_instance(Ptr[], instance, validate_only: true).valid?
|
258
562
|
end
|
259
563
|
|
260
|
-
# @
|
261
|
-
def
|
262
|
-
|
564
|
+
# @private
|
565
|
+
def fully_validate_instance(other_instance, errors_as_objects: false)
|
566
|
+
raise(NotImplementedError, "Schema#fully_validate_instance removed: see new validation interface Schema#instance_validate")
|
263
567
|
end
|
264
568
|
|
265
|
-
# @
|
266
|
-
|
267
|
-
|
268
|
-
# validation errors against its metaschema
|
269
|
-
def validate_schema!
|
270
|
-
::JSON::Validator.validate!(JSI::Typelike.as_json(schema_node.node_document), [], fragment: schema_node.node_ptr.fragment, validate_schema: true, list: true)
|
569
|
+
# @private
|
570
|
+
def validate_instance(other_instance)
|
571
|
+
raise(NotImplementedError, "Schema#validate_instance renamed: see Schema#instance_valid?")
|
271
572
|
end
|
272
573
|
|
273
|
-
# @
|
274
|
-
def
|
275
|
-
"
|
574
|
+
# @private
|
575
|
+
def validate_instance!(other_instance)
|
576
|
+
raise(NotImplementedError, "Schema#validate_instance! removed")
|
276
577
|
end
|
277
578
|
|
278
|
-
# @
|
279
|
-
def
|
280
|
-
"
|
579
|
+
# @private
|
580
|
+
def fully_validate_schema(errors_as_objects: false)
|
581
|
+
raise(NotImplementedError, "Schema#fully_validate_schema removed: use validation interface Base#jsi_validate on the schema")
|
281
582
|
end
|
282
|
-
alias_method :to_s, :inspect
|
283
583
|
|
284
|
-
#
|
285
|
-
|
286
|
-
|
287
|
-
q.instance_exec(self) do |obj|
|
288
|
-
text "\#<#{obj.class.inspect} #{obj.object_group_text}"
|
289
|
-
group_sub {
|
290
|
-
nest(2) {
|
291
|
-
breakable ' '
|
292
|
-
pp obj.schema_object
|
293
|
-
}
|
294
|
-
}
|
295
|
-
breakable ''
|
296
|
-
text '>'
|
297
|
-
end
|
584
|
+
# @private
|
585
|
+
def validate_schema
|
586
|
+
raise(NotImplementedError, "Schema#validate_schema removed: use validation interface Base#jsi_valid? on the schema")
|
298
587
|
end
|
299
588
|
|
300
|
-
# @
|
301
|
-
def
|
302
|
-
|
589
|
+
# @private
|
590
|
+
def validate_schema!
|
591
|
+
raise(NotImplementedError, "Schema#validate_schema! removed")
|
303
592
|
end
|
304
593
|
|
305
|
-
#
|
306
|
-
|
307
|
-
|
594
|
+
# schema resources which are ancestors of any subschemas below this schema.
|
595
|
+
# this may include this schema if this is a schema resource root.
|
596
|
+
# @private
|
597
|
+
# @return [Array<JSI::Schema>]
|
598
|
+
def jsi_subschema_resource_ancestors
|
599
|
+
if schema_resource_root?
|
600
|
+
jsi_schema_resource_ancestors + [self]
|
601
|
+
else
|
602
|
+
jsi_schema_resource_ancestors
|
603
|
+
end
|
308
604
|
end
|
309
|
-
include FingerprintHash
|
310
605
|
end
|
311
606
|
end
|