featurevisor-schemas 4.1.0
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/LICENSE +21 -0
- data/README.md +1139 -0
- data/lib/featurevisor/schemas/derived.rb +292 -0
- data/lib/featurevisor/schemas/engine.rb +623 -0
- data/lib/featurevisor/schemas/handle.rb +425 -0
- data/lib/featurevisor/schemas/store.rb +299 -0
- data/lib/featurevisor/schemas/support.rb +446 -0
- data/lib/featurevisor/schemas/values.rb +137 -0
- data/lib/featurevisor/schemas.rb +242 -0
- data/lib/featurevisor-schemas.rb +3 -0
- metadata +67 -0
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Featurevisor
|
|
4
|
+
module Schemas
|
|
5
|
+
# Compiles schema nodes, as the datafile holds them, into validators: the port of the JavaScript package's engine.
|
|
6
|
+
module Engine
|
|
7
|
+
# One validation: where issues go and how they are worded.
|
|
8
|
+
class Run
|
|
9
|
+
attr_reader :schema, :sources, :issues, :match
|
|
10
|
+
attr_accessor :translate, :failed, :shapes, :trial, :depth
|
|
11
|
+
|
|
12
|
+
# match answers conditions in the schema's package evaluation, with the context its modules leave.
|
|
13
|
+
def initialize(schema, sources, translate, match, trial: false, depth: 0)
|
|
14
|
+
@schema = schema
|
|
15
|
+
@sources = sources
|
|
16
|
+
@translate = translate
|
|
17
|
+
@match = match
|
|
18
|
+
@issues = []
|
|
19
|
+
@trial = trial
|
|
20
|
+
@failed = false
|
|
21
|
+
# The paths of the issues of shape found so far, which hold back the issues entries that read them.
|
|
22
|
+
@shapes = []
|
|
23
|
+
# How many references the walk follows at this point, which only a value nested as deep can need.
|
|
24
|
+
@depth = depth
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def trial_run
|
|
28
|
+
Run.new(schema, sources, translate, match, trial: true, depth: depth)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
Position = Struct.new(:path, :lookup)
|
|
33
|
+
|
|
34
|
+
# The nodes a oneOf's branches resolved to, and the branch that takes each kind of value when no two take the same.
|
|
35
|
+
OneOfKinds = Struct.new(:nodes, :by_kind)
|
|
36
|
+
|
|
37
|
+
# The most references a validation follows inside one another, which limits how deep a value can
|
|
38
|
+
# nest through them: a tree whose nodes each reference the tree's schema can be 128 levels deep. A loop
|
|
39
|
+
# of references at one value, which lint rejects, reaches the limit too. Past it, the walk fails closed.
|
|
40
|
+
MAX_REFERENCE_DEPTH = 128
|
|
41
|
+
|
|
42
|
+
# Issues after which an object's issues could not trust the values they compare, format among them,
|
|
43
|
+
# since the operators that read timestamps and versions cannot compare a value that is neither.
|
|
44
|
+
SHAPE_CODES = %w[type required enum const additionalProperties oneOf schemaNotFound format].freeze
|
|
45
|
+
TYPES = %w[string integer number boolean array object].freeze
|
|
46
|
+
NOTHING = ->(_value, _run, _at) {}
|
|
47
|
+
|
|
48
|
+
module_function
|
|
49
|
+
|
|
50
|
+
def lookup(mpath, scopes, title = nil, own = nil)
|
|
51
|
+
Messages::Lookup.new(mpath, scopes, title, own)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Reports an issue at a position, worded by the messages around it. An own message, a check's, wins over every other.
|
|
55
|
+
def report(run, at, code, params, own = nil)
|
|
56
|
+
path = at.path
|
|
57
|
+
found = at.lookup
|
|
58
|
+
run.failed = true
|
|
59
|
+
run.shapes << path if SHAPE_CODES.include?(code)
|
|
60
|
+
return if run.trial
|
|
61
|
+
|
|
62
|
+
# The field's title, so a message can say {title} is required.
|
|
63
|
+
params = params.merge(title: found.title) unless found.title.nil?
|
|
64
|
+
message = Messages.choose(code, params, path, found, run.sources, own)
|
|
65
|
+
run.issues << Issue.new(message, path.dup.freeze, code, params.freeze, run.schema, nil)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Hands each issue from an index on to the instance's translate, the last step of its wording,
|
|
69
|
+
# once mutations have named theirs, so a translator sees the mutation too.
|
|
70
|
+
def translate(run, from = 0)
|
|
71
|
+
return unless run.translate
|
|
72
|
+
|
|
73
|
+
(from...run.issues.length).each do |index|
|
|
74
|
+
issue = run.issues[index]
|
|
75
|
+
translated = run.translate.call(issue, run.sources.info)
|
|
76
|
+
run.issues[index] = issue.with(message: translated) if translated.is_a?(String)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Where a missing field's issue sits: at the field, worded by the messages for that field, with its title.
|
|
81
|
+
def missing_field(path, mpath, scopes, field, resolver)
|
|
82
|
+
scopes = own_scopes(field, mpath.length, resolver, scopes.dup)
|
|
83
|
+
Position.new(path, lookup(mpath, scopes, title_of(field, resolver)))
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# A node's annotations, over those of the schema it references, so the reference's own win.
|
|
87
|
+
def annotations_of(node, resolver, seen = [])
|
|
88
|
+
return nil unless node.is_a?(Hash)
|
|
89
|
+
|
|
90
|
+
own = node[:annotations].is_a?(Hash) ? node[:annotations] : nil
|
|
91
|
+
key = node[:schema]
|
|
92
|
+
return own unless key.is_a?(String) && !seen.include?(key)
|
|
93
|
+
|
|
94
|
+
referenced = annotations_of(resolver.node(key), resolver, seen + [key])
|
|
95
|
+
return referenced.merge(own) if referenced && own
|
|
96
|
+
|
|
97
|
+
own || referenced
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def title_of(node, resolver)
|
|
101
|
+
title = annotations_of(node, resolver)&.[](:title)
|
|
102
|
+
title.is_a?(String) ? title : nil
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# What an enum's options annotation says about one value, keyed by the value, or its JSON text.
|
|
106
|
+
def option_entry(node, value)
|
|
107
|
+
options = node.dig(:annotations, :options)
|
|
108
|
+
return nil unless options.is_a?(Hash)
|
|
109
|
+
|
|
110
|
+
entry = options[(value.is_a?(String) ? value : Values.stringify(value)).to_sym]
|
|
111
|
+
entry.is_a?(Hash) ? entry : nil
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def put_titles(params, entries)
|
|
115
|
+
titles = entries.map { |entry| entry.is_a?(Hash) ? entry[:title] : nil }
|
|
116
|
+
params[:titles] = titles if !titles.empty? && titles.all? { |title| title.is_a?(String) }
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Schema messages around a field: its own node's, and a referenced schema's root's.
|
|
120
|
+
def own_scopes(node, base, resolver, scopes, seen = [])
|
|
121
|
+
return scopes unless node.is_a?(Hash)
|
|
122
|
+
|
|
123
|
+
scopes << Messages::Scope.new(node[:messages], base) if node[:messages].is_a?(Hash)
|
|
124
|
+
key = node[:schema]
|
|
125
|
+
own_scopes(resolver.node(key), base, resolver, scopes, seen + [key]) if key.is_a?(String) && !seen.include?(key)
|
|
126
|
+
scopes
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# The node a reference resolves to, through any chain of references.
|
|
130
|
+
def resolve_node(node, resolver)
|
|
131
|
+
# The references followed, kept only for a node that is one.
|
|
132
|
+
seen = nil
|
|
133
|
+
current = node
|
|
134
|
+
while current.is_a?(Hash)
|
|
135
|
+
key = current[:schema]
|
|
136
|
+
return current unless key.is_a?(String)
|
|
137
|
+
return nil if seen&.include?(key)
|
|
138
|
+
|
|
139
|
+
(seen ||= []) << key
|
|
140
|
+
current = resolver.node(key)
|
|
141
|
+
end
|
|
142
|
+
nil
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def branch_kinds(resolved)
|
|
146
|
+
return nil if resolved[:oneOf].is_a?(Array)
|
|
147
|
+
|
|
148
|
+
type = resolved[:type]
|
|
149
|
+
return [type == "integer" ? "number" : type.to_s] if type && type != ""
|
|
150
|
+
return [Values.kind_of(resolved[:const])] if resolved.key?(:const)
|
|
151
|
+
return resolved[:enum].map { |option| Values.kind_of(option) }.uniq if resolved[:enum].is_a?(Array)
|
|
152
|
+
|
|
153
|
+
nil
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# Whether a node is a oneOf whose every branch is a const, a list of choices like an enum.
|
|
157
|
+
def const_list?(node, resolver)
|
|
158
|
+
branches = node[:oneOf]
|
|
159
|
+
branches.is_a?(Array) && !branches.empty? && branches.all? { |branch| resolve_node(branch, resolver)&.key?(:const) }
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def compile_const_list(node, resolver)
|
|
163
|
+
branches = node[:oneOf]
|
|
164
|
+
lambda do |value, run, at|
|
|
165
|
+
options = branches.map { |branch| resolve_node(branch, resolver)&.[](:const) }
|
|
166
|
+
next if options.any? { |option| Values.same?(value, option) }
|
|
167
|
+
|
|
168
|
+
params = { options: options }
|
|
169
|
+
put_titles(params, branches.map { |branch| annotations_of(branch, resolver) })
|
|
170
|
+
report(run, at, "enum", params)
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def compile_one_of(node, resolver)
|
|
175
|
+
return compile_const_list(node, resolver) if const_list?(node, resolver)
|
|
176
|
+
|
|
177
|
+
branch_nodes = node[:oneOf]
|
|
178
|
+
branches = branch_nodes.map { |branch| compile_node(branch, resolver) }
|
|
179
|
+
cached = nil
|
|
180
|
+
# What the branches take, read each time the nodes they resolve to change, since a branch's reference can resolve to another schema.
|
|
181
|
+
kinds_now = lambda do
|
|
182
|
+
nodes = branch_nodes.map { |branch| resolve_node(branch, resolver) }
|
|
183
|
+
current = cached
|
|
184
|
+
next current if current && current.nodes.each_with_index.all? { |resolved, index| resolved.equal?(nodes[index]) }
|
|
185
|
+
|
|
186
|
+
# Disjoint when every branch can say what it takes and no kind repeats across them.
|
|
187
|
+
by_kind = {}
|
|
188
|
+
disjoint = true
|
|
189
|
+
nodes.each_with_index do |resolved, index|
|
|
190
|
+
kinds = resolved && branch_kinds(resolved)
|
|
191
|
+
if kinds.nil?
|
|
192
|
+
disjoint = false
|
|
193
|
+
else
|
|
194
|
+
kinds.each { |kind| by_kind.key?(kind) ? (disjoint = false) : (by_kind[kind] = index) }
|
|
195
|
+
end
|
|
196
|
+
break unless disjoint
|
|
197
|
+
end
|
|
198
|
+
cached = OneOfKinds.new(nodes, disjoint ? by_kind : nil)
|
|
199
|
+
end
|
|
200
|
+
lambda do |value, run, at|
|
|
201
|
+
by_kind = kinds_now.call.by_kind
|
|
202
|
+
if by_kind
|
|
203
|
+
# The value's type selects a branch, whose own issues are the ones reported.
|
|
204
|
+
selected = by_kind[Values.kind_of(value)]
|
|
205
|
+
if selected
|
|
206
|
+
branches[selected].call(value, run, at)
|
|
207
|
+
else
|
|
208
|
+
report(run, at, "oneOf", { matches: 0 })
|
|
209
|
+
end
|
|
210
|
+
next
|
|
211
|
+
end
|
|
212
|
+
matches = branches.count do |branch|
|
|
213
|
+
trial = run.trial_run
|
|
214
|
+
branch.call(value, trial, at)
|
|
215
|
+
!trial.failed
|
|
216
|
+
end
|
|
217
|
+
report(run, at, "oneOf", { matches: matches }) unless matches == 1
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def number_keyword(node, key)
|
|
222
|
+
Values.number?(node[key]) ? node[key] : nil
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def compile_string(node, resolver)
|
|
226
|
+
min_length = number_keyword(node, :minLength)
|
|
227
|
+
max_length = number_keyword(node, :maxLength)
|
|
228
|
+
format = node[:format]
|
|
229
|
+
source = node[:pattern].is_a?(String) ? node[:pattern] : nil
|
|
230
|
+
pattern = source && Patterns.compile(source)
|
|
231
|
+
lambda do |value, run, at|
|
|
232
|
+
if format == "date-time" ? Featurevisor::Conditions.portable_timestamp(value).nil? : format == "semver" && !Featurevisor.valid_version?(value)
|
|
233
|
+
report(run, at, "format", { format: format })
|
|
234
|
+
end
|
|
235
|
+
if min_length || max_length
|
|
236
|
+
length = value.length
|
|
237
|
+
report(run, at, "minLength", { minLength: min_length, length: length }) if min_length && length < min_length
|
|
238
|
+
report(run, at, "maxLength", { maxLength: max_length, length: length }) if max_length && length > max_length
|
|
239
|
+
end
|
|
240
|
+
if source
|
|
241
|
+
# A pattern Ruby cannot compile fails closed, since no value can be shown to match it.
|
|
242
|
+
resolver.unsupported_pattern(source) if pattern.nil?
|
|
243
|
+
report(run, at, "pattern", { pattern: source }) if pattern.nil? || !Patterns.match?(pattern, value)
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def compile_number(node)
|
|
249
|
+
minimum = number_keyword(node, :minimum)
|
|
250
|
+
maximum = number_keyword(node, :maximum)
|
|
251
|
+
lambda do |value, run, at|
|
|
252
|
+
report(run, at, "minimum", { minimum: minimum, value: value }) if minimum && value.to_f < minimum.to_f
|
|
253
|
+
report(run, at, "maximum", { maximum: maximum, value: value }) if maximum && value.to_f > maximum.to_f
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def compile_array(node, resolver)
|
|
258
|
+
min_items = number_keyword(node, :minItems)
|
|
259
|
+
max_items = number_keyword(node, :maxItems)
|
|
260
|
+
items = node.key?(:items) ? compile_node(node[:items], resolver) : nil
|
|
261
|
+
unique = node[:uniqueItems] == true
|
|
262
|
+
lambda do |value, run, at|
|
|
263
|
+
report(run, at, "minItems", { minItems: min_items, count: value.length }) if min_items && value.length < min_items
|
|
264
|
+
report(run, at, "maxItems", { maxItems: max_items, count: value.length }) if max_items && value.length > max_items
|
|
265
|
+
if unique
|
|
266
|
+
repeated = (1...value.length).find { |index| (0...index).any? { |earlier| Values.same?(value[earlier], value[index]) } }
|
|
267
|
+
report(run, at, "uniqueItems", { index: repeated }) if repeated
|
|
268
|
+
end
|
|
269
|
+
mpath = at.lookup.mpath + ["[]"]
|
|
270
|
+
value.each_with_index do |item, index|
|
|
271
|
+
position = Position.new(at.path + [index], lookup(mpath, at.lookup.scopes))
|
|
272
|
+
if items
|
|
273
|
+
items.call(item, run, position)
|
|
274
|
+
elsif !item.is_a?(String)
|
|
275
|
+
# An array without items holds strings only.
|
|
276
|
+
report(run, position, "type", { type: "string", received: Values.kind_of(item) })
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
def compile_object(node, resolver)
|
|
283
|
+
properties = node[:properties].is_a?(Hash) ? node[:properties] : nil
|
|
284
|
+
# Fields by their names as strings, the keys of the values validated.
|
|
285
|
+
fields = (properties || {}).to_h { |key, property| [key.to_s, property] }
|
|
286
|
+
compiled = fields.transform_values { |property| compile_node(property, resolver) }
|
|
287
|
+
required = node[:required].is_a?(Array) ? node[:required].grep(String) : []
|
|
288
|
+
has_additional = node.key?(:additionalProperties)
|
|
289
|
+
additional = node[:additionalProperties]
|
|
290
|
+
compiled_additional = additional.is_a?(Hash) ? compile_node(additional, resolver) : nil
|
|
291
|
+
lambda do |value, run, at|
|
|
292
|
+
# Each key required lists must be there, whatever properties holds.
|
|
293
|
+
required.each do |key|
|
|
294
|
+
next if value.key?(key)
|
|
295
|
+
|
|
296
|
+
missing = missing_field(at.path + [key], at.lookup.mpath + [key], at.lookup.scopes, fields[key], resolver)
|
|
297
|
+
report(run, missing, "required", { key: key })
|
|
298
|
+
end
|
|
299
|
+
value.each do |key, child|
|
|
300
|
+
if compiled.key?(key)
|
|
301
|
+
compiled[key].call(child, run, Position.new(at.path + [key], lookup(at.lookup.mpath + [key], at.lookup.scopes)))
|
|
302
|
+
elsif additional == true
|
|
303
|
+
next
|
|
304
|
+
elsif compiled_additional
|
|
305
|
+
compiled_additional.call(child, run, Position.new(at.path + [key], lookup(at.lookup.mpath + ["*"], at.lookup.scopes)))
|
|
306
|
+
elsif properties || has_additional
|
|
307
|
+
# Anything but true or a schema, false included, takes no key beyond properties. An extra key
|
|
308
|
+
# has no field of its own, so the object that rejects it words its issue, without a title.
|
|
309
|
+
report(run, untitled(at, key), "additionalProperties", { key: key })
|
|
310
|
+
elsif !Values.primitive?(child)
|
|
311
|
+
# Without properties or additionalProperties, an object is flat.
|
|
312
|
+
report(run, untitled(at, key), "type", { type: "primitive", received: Values.kind_of(child) })
|
|
313
|
+
end
|
|
314
|
+
end
|
|
315
|
+
end
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
# Where a key an object has no field for sits, worded by the object's messages, without a title of its own.
|
|
319
|
+
def untitled(at, key)
|
|
320
|
+
Position.new(at.path + [key], lookup(at.lookup.mpath, at.lookup.scopes))
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
def type_matches?(type, value)
|
|
324
|
+
case type
|
|
325
|
+
when "string" then value.is_a?(String)
|
|
326
|
+
when "integer" then Values.integer?(value)
|
|
327
|
+
when "number" then Values.finite_number?(value)
|
|
328
|
+
when "boolean" then Values.boolean?(value)
|
|
329
|
+
when "array" then value.is_a?(Array)
|
|
330
|
+
when "object" then value.is_a?(Hash)
|
|
331
|
+
else true
|
|
332
|
+
end
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
def compile_rules(node, resolver)
|
|
336
|
+
reference = node[:schema]
|
|
337
|
+
if reference.is_a?(String)
|
|
338
|
+
return lambda do |value, run, at|
|
|
339
|
+
if run.depth >= MAX_REFERENCE_DEPTH
|
|
340
|
+
resolver.reference_too_deep(run.schema)
|
|
341
|
+
next report(run, at, "refineError", {})
|
|
342
|
+
end
|
|
343
|
+
target = resolver.resolve(reference)
|
|
344
|
+
next report(run, at, "schemaNotFound", { schema: reference }) unless target
|
|
345
|
+
|
|
346
|
+
run.depth += 1
|
|
347
|
+
begin
|
|
348
|
+
target.last.call(value, run, at)
|
|
349
|
+
ensure
|
|
350
|
+
run.depth -= 1
|
|
351
|
+
end
|
|
352
|
+
end
|
|
353
|
+
end
|
|
354
|
+
return compile_one_of(node, resolver) if node[:oneOf].is_a?(Array)
|
|
355
|
+
|
|
356
|
+
# A listed timestamp must still be one, so its typed rules run after the value matches.
|
|
357
|
+
typed = node[:format] == "date-time" ? compile_typed(node, resolver) : nil
|
|
358
|
+
if node.key?(:const)
|
|
359
|
+
constant = node[:const]
|
|
360
|
+
return lambda do |value, run, at|
|
|
361
|
+
if !Values.same?(value, constant)
|
|
362
|
+
report(run, at, "const", { const: constant })
|
|
363
|
+
elsif typed
|
|
364
|
+
typed.call(value, run, at)
|
|
365
|
+
end
|
|
366
|
+
end
|
|
367
|
+
end
|
|
368
|
+
if node[:enum].is_a?(Array)
|
|
369
|
+
options = node[:enum]
|
|
370
|
+
return lambda do |value, run, at|
|
|
371
|
+
if options.any? { |option| Values.same?(value, option) }
|
|
372
|
+
typed&.call(value, run, at)
|
|
373
|
+
next
|
|
374
|
+
end
|
|
375
|
+
params = { options: options }
|
|
376
|
+
put_titles(params, options.map { |option| option_entry(node, option) })
|
|
377
|
+
report(run, at, "enum", params)
|
|
378
|
+
end
|
|
379
|
+
end
|
|
380
|
+
compile_typed(node, resolver)
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
# A node's rules by its type: the type itself, then the keywords that type takes.
|
|
384
|
+
def compile_typed(node, resolver)
|
|
385
|
+
type = node[:type].is_a?(String) ? node[:type] : nil
|
|
386
|
+
# A type this port does not know, a value that is not text among them, fails every value.
|
|
387
|
+
unknown = node.key?(:type) && !TYPES.include?(type)
|
|
388
|
+
# With a type, only its own keywords can apply, so only they are compiled.
|
|
389
|
+
string = type.nil? || type == "string" ? compile_string(node, resolver) : nil
|
|
390
|
+
number = type.nil? || type == "integer" || type == "number" ? compile_number(node) : nil
|
|
391
|
+
array = type.nil? || type == "array" ? compile_array(node, resolver) : nil
|
|
392
|
+
object = type.nil? || type == "object" ? compile_object(node, resolver) : nil
|
|
393
|
+
lambda do |value, run, at|
|
|
394
|
+
if unknown || (type && !type_matches?(type, value))
|
|
395
|
+
report(run, at, "type", { type: node[:type], received: Values.kind_of(value) })
|
|
396
|
+
next
|
|
397
|
+
end
|
|
398
|
+
case value
|
|
399
|
+
when String then string&.call(value, run, at)
|
|
400
|
+
when Integer, Float then number&.call(value, run, at)
|
|
401
|
+
when Array then array&.call(value, run, at)
|
|
402
|
+
when Hash then object&.call(value, run, at)
|
|
403
|
+
end
|
|
404
|
+
end
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
# The value at a dot path inside a value, for input leaves, or Values::ABSENT.
|
|
408
|
+
def read_input(value, path)
|
|
409
|
+
path.to_s.split(".", -1).reduce(value) do |current, part|
|
|
410
|
+
case current
|
|
411
|
+
when Hash
|
|
412
|
+
return Values::ABSENT unless current.key?(part)
|
|
413
|
+
|
|
414
|
+
current[part]
|
|
415
|
+
when Array
|
|
416
|
+
# A list's own keys are its indexes and its length, as JavaScript reads items.0.name.
|
|
417
|
+
next current.length if part == "length"
|
|
418
|
+
return Values::ABSENT unless Paths::INDEX.match?(part) && part.to_i < current.length
|
|
419
|
+
|
|
420
|
+
current[part.to_i]
|
|
421
|
+
else
|
|
422
|
+
return Values::ABSENT
|
|
423
|
+
end
|
|
424
|
+
end
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
# Readers for input leaves, which read the value at the node whose conditions they answer.
|
|
428
|
+
def input_reader(value)
|
|
429
|
+
->(path) { read_input(value, path) }
|
|
430
|
+
end
|
|
431
|
+
|
|
432
|
+
# Where the rule an issue reports sits in a node's rules, such as
|
|
433
|
+
# properties.taxId.pattern, and whether the walk reached it, which it does
|
|
434
|
+
# not through a reference or a oneOf.
|
|
435
|
+
def rule_of(rules, path, code)
|
|
436
|
+
# A missing or an extra key is a rule of the object around it.
|
|
437
|
+
steps = %w[required additionalProperties].include?(code) ? path[0...-1] : path
|
|
438
|
+
at = []
|
|
439
|
+
current = rules
|
|
440
|
+
steps.each do |step|
|
|
441
|
+
node = current.is_a?(Hash) ? current : {}
|
|
442
|
+
properties = node[:properties]
|
|
443
|
+
if step.is_a?(String) && properties.is_a?(Hash) && properties.key?(step.to_sym)
|
|
444
|
+
at.push("properties", step)
|
|
445
|
+
current = properties[step.to_sym]
|
|
446
|
+
elsif step.is_a?(Integer) && node[:items].is_a?(Hash)
|
|
447
|
+
at << "items"
|
|
448
|
+
current = node[:items]
|
|
449
|
+
elsif node[:additionalProperties].is_a?(Hash)
|
|
450
|
+
at << "additionalProperties"
|
|
451
|
+
current = node[:additionalProperties]
|
|
452
|
+
else
|
|
453
|
+
return [at, false]
|
|
454
|
+
end
|
|
455
|
+
end
|
|
456
|
+
case code
|
|
457
|
+
when "required" then at << "required"
|
|
458
|
+
when "additionalProperties" then at.push("properties", path.last.to_s)
|
|
459
|
+
else at << code
|
|
460
|
+
end
|
|
461
|
+
[at, true]
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
# Whether an operation's path reaches a rule, as a prefix of it or, when the rule was reached, beyond it.
|
|
465
|
+
def reaches?(operation, rule, whole)
|
|
466
|
+
operation.zip(rule).first([operation.length, rule.length].min).all? do |part, step|
|
|
467
|
+
# A selector picks an item by its fields, as any item can.
|
|
468
|
+
part.is_a?(Hash) || part == step
|
|
469
|
+
end && (operation.length <= rule.length || whole)
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
def mutations?(node)
|
|
473
|
+
node.is_a?(Hash) && node[:mutations].is_a?(Array) && !node[:mutations].empty?
|
|
474
|
+
end
|
|
475
|
+
|
|
476
|
+
# Which of a node's mutations match the value at it, as an id such as "0,2,".
|
|
477
|
+
def matching_mutations(node, value, match)
|
|
478
|
+
# input reads the value at this node, so a mutation in address reads the address's own fields.
|
|
479
|
+
reader = input_reader(value)
|
|
480
|
+
node[:mutations].each_with_index.map do |mutation, index|
|
|
481
|
+
match.call(mutation.is_a?(Hash) ? mutation[:conditions] : nil, reader) ? "#{index}," : ""
|
|
482
|
+
end.join
|
|
483
|
+
end
|
|
484
|
+
|
|
485
|
+
# The indexes of the mutations an id such as "0,2," names, in order.
|
|
486
|
+
def matched_of(id)
|
|
487
|
+
id.split(",").map(&:to_i)
|
|
488
|
+
end
|
|
489
|
+
|
|
490
|
+
# A node's rules with the mutations an id names applied in order, without mutations. The store keeps
|
|
491
|
+
# them, so a view's nodes are the same object for the same mutations and their validators are kept.
|
|
492
|
+
def mutated_rules(node, id, resolver)
|
|
493
|
+
resolver.mutated(node, id) do
|
|
494
|
+
rules = node.reject { |key, _| key == :mutations }
|
|
495
|
+
matched_of(id).each do |index|
|
|
496
|
+
rules = Featurevisor.apply_variable_operations(rules, node[:mutations][index][:operations])
|
|
497
|
+
end
|
|
498
|
+
raise ArgumentError, "Mutations of a schema node must keep it an object" unless rules.is_a?(Hash)
|
|
499
|
+
|
|
500
|
+
rules
|
|
501
|
+
end
|
|
502
|
+
end
|
|
503
|
+
|
|
504
|
+
# A node whose rules change under conditions: each value is validated
|
|
505
|
+
# against the node with every mutation whose conditions match applied, in
|
|
506
|
+
# order. Each combination of matches compiles once and is kept for the node.
|
|
507
|
+
# An issue whose rule a matching mutation set or changed names the last such mutation.
|
|
508
|
+
def compile_mutations(node, resolver)
|
|
509
|
+
mutations = node[:mutations]
|
|
510
|
+
variants = {}
|
|
511
|
+
mutex = Mutex.new
|
|
512
|
+
lambda do |value, run, at|
|
|
513
|
+
id = matching_mutations(node, value, run.match)
|
|
514
|
+
variant = mutex.synchronize { variants[id] }
|
|
515
|
+
unless variant
|
|
516
|
+
matched = matched_of(id)
|
|
517
|
+
variant = begin
|
|
518
|
+
rules = mutated_rules(node, id, resolver)
|
|
519
|
+
[compile_node(rules, resolver), rules, matched, nil]
|
|
520
|
+
rescue StandardError => error
|
|
521
|
+
[nil, {}, [], error]
|
|
522
|
+
end
|
|
523
|
+
mutex.synchronize { variants[id] = variant }
|
|
524
|
+
end
|
|
525
|
+
validate, rules, matched, failure = variant
|
|
526
|
+
if validate.nil?
|
|
527
|
+
# Operations the interpreter refuses fail closed: no value can be shown to meet rules nobody can read.
|
|
528
|
+
resolver.mutation_failed(run.schema, failure)
|
|
529
|
+
report(run, at, "refineError", {})
|
|
530
|
+
next
|
|
531
|
+
end
|
|
532
|
+
start = run.issues.length
|
|
533
|
+
validate.call(value, run, at)
|
|
534
|
+
# Without a matching mutation, no issue names one.
|
|
535
|
+
next if matched.empty?
|
|
536
|
+
|
|
537
|
+
(start...run.issues.length).each do |position|
|
|
538
|
+
issue = run.issues[position]
|
|
539
|
+
# A mutation inside the node applies last, so its claim stands.
|
|
540
|
+
next unless issue.mutation.nil?
|
|
541
|
+
|
|
542
|
+
# An issue entry's issue sits at its own path, so the mutation is one that changed issues.
|
|
543
|
+
sets = if issue.code == "refine"
|
|
544
|
+
->(path) { path.include?("issues") }
|
|
545
|
+
else
|
|
546
|
+
rule, whole = rule_of(rules, issue.path[at.path.length..] || [], issue.code)
|
|
547
|
+
->(path) { reaches?(path, rule, whole) }
|
|
548
|
+
end
|
|
549
|
+
matched.reverse_each do |index|
|
|
550
|
+
mutation = mutations[index]
|
|
551
|
+
paths = (mutation[:operations] || []).map { |operation| operation.is_a?(Hash) ? operation[:path] || [] : [] }
|
|
552
|
+
next unless paths.any?(&sets)
|
|
553
|
+
|
|
554
|
+
run.issues[position] = issue.with(mutation: mutation[:key])
|
|
555
|
+
break
|
|
556
|
+
end
|
|
557
|
+
end
|
|
558
|
+
end
|
|
559
|
+
end
|
|
560
|
+
|
|
561
|
+
# An object's issues: after its own rules, each entry whose conditions match
|
|
562
|
+
# reports its issue at its path, with that field's title and the messages
|
|
563
|
+
# around it. An entry waits while an issue of shape sits at or inside its
|
|
564
|
+
# path, or anywhere in the object for an entry without one, as a check in
|
|
565
|
+
# code does, or at or inside a field its conditions read.
|
|
566
|
+
def compile_issues(node, rules, resolver)
|
|
567
|
+
entries = node[:issues]
|
|
568
|
+
waits = entries.map do |entry|
|
|
569
|
+
next [[]] unless entry.is_a?(Hash)
|
|
570
|
+
|
|
571
|
+
own = [entry[:path].is_a?(String) && !entry[:path].empty? ? entry[:path].split(".") : []]
|
|
572
|
+
Views.inputs_of(entry[:conditions]) { |path| own << path.split(".") }
|
|
573
|
+
own
|
|
574
|
+
end
|
|
575
|
+
lambda do |value, run, at|
|
|
576
|
+
before = run.shapes.length
|
|
577
|
+
rules.call(value, run, at)
|
|
578
|
+
next unless value.is_a?(Hash)
|
|
579
|
+
|
|
580
|
+
found = run.shapes[before..].map { |path| path.drop(at.path.length) }
|
|
581
|
+
reader = input_reader(value)
|
|
582
|
+
entries.each_with_index do |entry, index|
|
|
583
|
+
next if found.any? { |path| waits[index].any? { |wait| Paths.at_or_inside?(path, wait) } }
|
|
584
|
+
next unless entry.is_a?(Hash) && run.match.call(entry[:conditions], reader)
|
|
585
|
+
|
|
586
|
+
# The entry's own path, read once with the fields it waits for.
|
|
587
|
+
parts = waits[index].first
|
|
588
|
+
scopes = at.lookup.scopes.dup
|
|
589
|
+
field = node
|
|
590
|
+
parts.each_with_index do |part, depth|
|
|
591
|
+
field = resolve_node(field, resolver)&.dig(:properties, part.to_sym)
|
|
592
|
+
own_scopes(field, at.lookup.mpath.length + depth + 1, resolver, scopes)
|
|
593
|
+
end
|
|
594
|
+
title = parts.empty? ? at.lookup.title : title_of(field, resolver)
|
|
595
|
+
target = Position.new(at.path + parts, lookup(at.lookup.mpath + parts, scopes, title,
|
|
596
|
+
entry[:message].is_a?(String) ? entry[:message] : nil))
|
|
597
|
+
report(run, target, "refine", { refine: entry[:key] })
|
|
598
|
+
end
|
|
599
|
+
end
|
|
600
|
+
end
|
|
601
|
+
|
|
602
|
+
# Compiles a schema node into a validator, with its messages and title in scope.
|
|
603
|
+
def compile_node(node, resolver)
|
|
604
|
+
return NOTHING unless node.is_a?(Hash)
|
|
605
|
+
return compile_mutations(node, resolver) if mutations?(node)
|
|
606
|
+
|
|
607
|
+
rules = compile_rules(node, resolver)
|
|
608
|
+
rules = compile_issues(node, rules, resolver) if node[:issues].is_a?(Array) && !node[:issues].empty?
|
|
609
|
+
messages = node[:messages].is_a?(Hash) ? node[:messages] : nil
|
|
610
|
+
title = node.dig(:annotations, :title)
|
|
611
|
+
title = nil unless title.is_a?(String)
|
|
612
|
+
return rules if messages.nil? && title.nil?
|
|
613
|
+
|
|
614
|
+
lambda do |value, run, at|
|
|
615
|
+
scopes = messages ? at.lookup.scopes + [Messages::Scope.new(messages, at.lookup.mpath.length)] : at.lookup.scopes
|
|
616
|
+
# A reference's own title wins over the title of the schema it references.
|
|
617
|
+
found = lookup(at.lookup.mpath, scopes, at.lookup.title.nil? ? title : at.lookup.title, at.lookup.own)
|
|
618
|
+
rules.call(value, run, Position.new(at.path, found))
|
|
619
|
+
end
|
|
620
|
+
end
|
|
621
|
+
end
|
|
622
|
+
end
|
|
623
|
+
end
|