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,446 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Featurevisor
|
|
6
|
+
module Schemas
|
|
7
|
+
# Field paths as form libraries write them: address.city, items.0.name, items[].name, labels.*.
|
|
8
|
+
module Paths
|
|
9
|
+
Step = Struct.new(:kind, :key, :index) do
|
|
10
|
+
def part
|
|
11
|
+
case kind
|
|
12
|
+
when :key then key
|
|
13
|
+
when :index then index.to_s
|
|
14
|
+
when :items then "[]"
|
|
15
|
+
else "*"
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
INDEX = /\A(?:0|[1-9][0-9]{0,8})\z/
|
|
21
|
+
ITEMS = Step.new(:items, nil, nil).freeze
|
|
22
|
+
VALUES = Step.new(:values, nil, nil).freeze
|
|
23
|
+
|
|
24
|
+
module_function
|
|
25
|
+
|
|
26
|
+
# A step's text is *, an index or a key, read before a bracket's quotes go, so ["0"] is the key 0.
|
|
27
|
+
def step(text, name = text)
|
|
28
|
+
return VALUES if text == "*"
|
|
29
|
+
return Step.new(:index, nil, text.to_i) if INDEX.match?(text)
|
|
30
|
+
|
|
31
|
+
Step.new(:key, name, nil)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Whether parse reads a key as that one key, so 2fa does and 2, * and a.b do not.
|
|
35
|
+
def reads_as_key?(key)
|
|
36
|
+
!key.empty? && key != "*" && !INDEX.match?(key) && !key.match?(/[.\[\]]/)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# A number after a dot or in brackets is an index, so items.0 and items[0] are the same.
|
|
40
|
+
def parse(path)
|
|
41
|
+
steps = []
|
|
42
|
+
key = +""
|
|
43
|
+
index = 0
|
|
44
|
+
path = path.to_s
|
|
45
|
+
while index < path.length
|
|
46
|
+
character = path[index]
|
|
47
|
+
if character == "."
|
|
48
|
+
steps << step(key) unless key.empty?
|
|
49
|
+
key = +""
|
|
50
|
+
index += 1
|
|
51
|
+
elsif character == "["
|
|
52
|
+
steps << step(key) unless key.empty?
|
|
53
|
+
key = +""
|
|
54
|
+
finish = path.index("]", index)
|
|
55
|
+
inside = finish ? path[(index + 1)...finish] : path[(index + 1)..]
|
|
56
|
+
steps << (inside.empty? ? ITEMS : step(inside, inside.delete_prefix('"').delete_prefix("'").delete_suffix('"').delete_suffix("'")))
|
|
57
|
+
index = finish ? finish + 1 : path.length
|
|
58
|
+
else
|
|
59
|
+
key << character
|
|
60
|
+
index += 1
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
steps << step(key) unless key.empty?
|
|
64
|
+
steps
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def format_steps(steps)
|
|
68
|
+
format_path(steps.map(&:part)).gsub(".[]", "[]")
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# The keys and indexes of a path joined with dots.
|
|
72
|
+
def format_path(path)
|
|
73
|
+
path.map(&:to_s).join(".")
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# A field's message path: keys, [] for items and * for an open object's values.
|
|
77
|
+
def message_path(field)
|
|
78
|
+
parse(field).map { |step| step.kind == :key ? step.key : (step.kind == :values ? "*" : "[]") }
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def at_or_inside?(inner, outer)
|
|
82
|
+
inner.length >= outer.length && outer.each_with_index.all? { |part, index| inner[index].to_s == part.to_s }
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def matches_steps?(path, steps)
|
|
86
|
+
return false if path.length < steps.length
|
|
87
|
+
|
|
88
|
+
steps.each_with_index.all? do |step, index|
|
|
89
|
+
case step.kind
|
|
90
|
+
when :index then path[index].to_s == step.index.to_s
|
|
91
|
+
when :key then path[index].to_s == step.key
|
|
92
|
+
else true
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def steps_of(path)
|
|
98
|
+
path.map { |part| part.is_a?(Integer) ? Step.new(:index, nil, part) : Step.new(:key, part.to_s, nil) }
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# How issues are worded: the form's messages, then the schema's, then the application's, then the default.
|
|
103
|
+
module Messages
|
|
104
|
+
Scope = Struct.new(:messages, :base)
|
|
105
|
+
Lookup = Struct.new(:mpath, :scopes, :title, :own)
|
|
106
|
+
Sources = Struct.new(:form, :app, :info)
|
|
107
|
+
SEPARATOR = "\u001f"
|
|
108
|
+
PLACEHOLDER = /\{\{|\}\}|\{([^{}]*)\}/
|
|
109
|
+
LIST_PLACEHOLDER = /\{(?:options|titles)\}/
|
|
110
|
+
TYPE_NAMES = {
|
|
111
|
+
"string" => "a string", "integer" => "an integer", "number" => "a number", "boolean" => "a boolean",
|
|
112
|
+
"object" => "an object", "array" => "an array", "primitive" => "a string, number, boolean or null"
|
|
113
|
+
}.freeze
|
|
114
|
+
|
|
115
|
+
module_function
|
|
116
|
+
|
|
117
|
+
def form_messages(messages)
|
|
118
|
+
(messages || {}).each_with_object({}) do |(field, codes), by_path|
|
|
119
|
+
(by_path[Paths.message_path(field.to_s).join(SEPARATOR)] ||= {}).merge!(codes.transform_keys(&:to_s))
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def navigate(messages, steps, from)
|
|
124
|
+
node = messages
|
|
125
|
+
steps[from..].each do |step|
|
|
126
|
+
return nil unless node.is_a?(Hash)
|
|
127
|
+
|
|
128
|
+
node = Values.child(node, step)
|
|
129
|
+
end
|
|
130
|
+
node.is_a?(Hash) ? node : nil
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def format_param(value)
|
|
134
|
+
case value
|
|
135
|
+
when Array then value.map { |item| format_param(item) }.join(", ")
|
|
136
|
+
when String then value
|
|
137
|
+
when nil then "null"
|
|
138
|
+
when true, false then value.to_s
|
|
139
|
+
when Integer, Float then Values.number_text(value)
|
|
140
|
+
else Values.stringify(value)
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# Fills {name} from params. Lists are joined with commas, and {{ and }} write braces.
|
|
145
|
+
def fill(template, params)
|
|
146
|
+
template.gsub(PLACEHOLDER) do |match|
|
|
147
|
+
next "{" if match == "{{"
|
|
148
|
+
next "}" if match == "}}"
|
|
149
|
+
|
|
150
|
+
name = Regexp.last_match(1).to_sym
|
|
151
|
+
# A name the params lack fills nothing, as undefined does in JavaScript.
|
|
152
|
+
params.key?(name) ? format_param(params[name]) : ""
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def apply(message, params, info)
|
|
157
|
+
message.respond_to?(:call) ? message.call(params, info) : fill(message.to_s, params)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def param(params, name)
|
|
161
|
+
params.key?(name) ? format_param(params[name]) : "undefined"
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def subject(path)
|
|
165
|
+
path.empty? ? "Value" : "Value at #{JSON.generate(Paths.format_path(path))}"
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# The default wording of each issue, in lint's English.
|
|
169
|
+
def default_message(code, params, path)
|
|
170
|
+
at = subject(path)
|
|
171
|
+
case code
|
|
172
|
+
when "type" then "#{at} must be #{TYPE_NAMES.fetch(params[:type].to_s) { format_param(params[:type]) }} (got #{param(params, :received)})."
|
|
173
|
+
when "const" then "#{at} must equal #{Values.stringify(params[:const])}."
|
|
174
|
+
when "enum"
|
|
175
|
+
listed = params.key?(:titles) ? params[:titles] : params[:options]
|
|
176
|
+
# An option source's lookup knows no list to name.
|
|
177
|
+
return "#{at} is not one of its choices." unless listed.is_a?(Array)
|
|
178
|
+
|
|
179
|
+
"#{at} must be one of #{listed.map { |option| option.is_a?(String) ? option : Values.stringify(option) }.join(", ")}."
|
|
180
|
+
when "minimum" then "#{at} must be at least #{param(params, :minimum)} (got #{param(params, :value)})."
|
|
181
|
+
when "maximum" then "#{at} must be at most #{param(params, :maximum)} (got #{param(params, :value)})."
|
|
182
|
+
when "minLength" then "#{at} must be at least #{param(params, :minLength)} characters long (got #{param(params, :length)})."
|
|
183
|
+
when "maxLength" then "#{at} must be at most #{param(params, :maxLength)} characters long (got #{param(params, :length)})."
|
|
184
|
+
when "pattern" then "#{at} does not match the expected pattern."
|
|
185
|
+
when "format"
|
|
186
|
+
params[:format] == "date-time" ? "#{at} must be a timestamp such as 2026-01-31T09:00:00Z." : "#{at} must be a version such as 1.2.3."
|
|
187
|
+
when "minItems" then "#{at} must have at least #{param(params, :minItems)} #{params[:minItems] == 1 ? "item" : "items"} (got #{param(params, :count)})."
|
|
188
|
+
when "maxItems" then "#{at} must have at most #{param(params, :maxItems)} #{params[:maxItems] == 1 ? "item" : "items"} (got #{param(params, :count)})."
|
|
189
|
+
when "uniqueItems" then "#{at} must not repeat items (item #{param(params, :index)} repeats an earlier one)."
|
|
190
|
+
when "required" then "#{at} is required."
|
|
191
|
+
when "additionalProperties" then "#{at} is not allowed."
|
|
192
|
+
when "oneOf"
|
|
193
|
+
params[:matches] == 0 ? "#{at} does not match any of the allowed shapes." : "#{at} must match exactly one of the allowed shapes (matched #{param(params, :matches)})."
|
|
194
|
+
when "refine" then "#{at} is not valid."
|
|
195
|
+
when "refineError" then "#{at} could not be checked."
|
|
196
|
+
when "schemaNotFound"
|
|
197
|
+
if params.key?(:field)
|
|
198
|
+
"Field #{Values.stringify(params[:field])} was not found in schema #{Values.stringify(params[:schema])}."
|
|
199
|
+
else
|
|
200
|
+
"Schema #{Values.stringify(params[:schema])} was not found."
|
|
201
|
+
end
|
|
202
|
+
else "#{at} is not valid."
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# Only text or a function words an issue, so any other value from the application or a form is passed over.
|
|
207
|
+
def wording?(message)
|
|
208
|
+
message.is_a?(String) || message.is_a?(Symbol) || message.respond_to?(:call)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def fits?(message, listless)
|
|
212
|
+
!listless || !message.is_a?(String) || !LIST_PLACEHOLDER.match?(message)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# The message an issue carries, the first of: a check's own message, the
|
|
216
|
+
# form's for the field, the schema's from the outside in, an issue entry's
|
|
217
|
+
# own, the application's for the code, and the default. A message is for the
|
|
218
|
+
# field it names, never for fields inside it.
|
|
219
|
+
def choose(code, params, path, lookup, sources, own_message = nil)
|
|
220
|
+
return fill(own_message, params) unless own_message.nil?
|
|
221
|
+
|
|
222
|
+
# An option source's enum names no list, so a wording that fills one in is passed over.
|
|
223
|
+
listless = code == "enum" && !params.key?(:options) && !params.key?(:titles)
|
|
224
|
+
form = sources.form.dig(lookup.mpath.join(SEPARATOR), code)
|
|
225
|
+
return apply(form, params, sources.info) if wording?(form) && fits?(form, listless)
|
|
226
|
+
|
|
227
|
+
lookup.scopes.each do |scope|
|
|
228
|
+
next if scope.base > lookup.mpath.length
|
|
229
|
+
|
|
230
|
+
template = navigate(scope.messages, lookup.mpath, scope.base)&.dig(code.to_sym)
|
|
231
|
+
return fill(template, params) if template.is_a?(String) && fits?(template, listless)
|
|
232
|
+
end
|
|
233
|
+
return fill(lookup.own, params) if lookup.own.is_a?(String) && fits?(lookup.own, listless)
|
|
234
|
+
|
|
235
|
+
app = sources.app[code]
|
|
236
|
+
return apply(app, params, sources.info) if wording?(app) && fits?(app, listless)
|
|
237
|
+
|
|
238
|
+
default_message(code, params, path)
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# Schema patterns, written for JavaScript's RegExp with the u flag, compiled to
|
|
243
|
+
# mean the same in Ruby's Onigmo. Both read code points, so ^.$ takes an emoji
|
|
244
|
+
# in each. Without the rewrites, Onigmo's ^ and $ match at every line, its .
|
|
245
|
+
# takes \r, its \s misses NBSP and the BOM, \h is a hex digit, and && inside a
|
|
246
|
+
# class intersects. What the u flag rejects, such as an escaped letter without
|
|
247
|
+
# a meaning or a lone brace, does not compile here either, so it fails every
|
|
248
|
+
# value as it does in JavaScript.
|
|
249
|
+
module Patterns
|
|
250
|
+
SPACES = "\\t\\n\\x0B\\f\\r \\u00A0\\u1680\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000\\uFEFF"
|
|
251
|
+
NOT_SPACES = "\\x00-\\x08\\x0E-\\x1F\\x21-\\x7F\\u0080-\\u009F\\u00A1-\\u167F\\u1681-\\u1FFF\\u200B-\\u2027\\u202A-\\u202E" \
|
|
252
|
+
"\\u2030-\\u205E\\u2060-\\u2FFF\\u3001-\\uFEFE\\uFF00-\\u{10FFFF}"
|
|
253
|
+
NOT_DIGITS = "\\x00-\\x2F\\x3A-\\x7F\\u0080-\\u{10FFFF}"
|
|
254
|
+
NOT_WORD = "\\x00-\\x2F\\x3A-\\x40\\x5B-\\x5E\\x60\\x7B-\\x7F\\u0080-\\u{10FFFF}"
|
|
255
|
+
FAILED = Object.new.freeze
|
|
256
|
+
@cache = {}
|
|
257
|
+
@mutex = Mutex.new
|
|
258
|
+
|
|
259
|
+
module_function
|
|
260
|
+
|
|
261
|
+
# The pattern compiled, or nil when Ruby cannot compile it.
|
|
262
|
+
def compile(source)
|
|
263
|
+
cached = @mutex.synchronize { @cache[source] }
|
|
264
|
+
if cached.nil?
|
|
265
|
+
cached = begin
|
|
266
|
+
translated = translate(source)
|
|
267
|
+
translated ? Regexp.new(translated) : FAILED
|
|
268
|
+
rescue RegexpError, ArgumentError
|
|
269
|
+
FAILED
|
|
270
|
+
end
|
|
271
|
+
@mutex.synchronize { @cache[source] = cached if @cache.size < 4096 }
|
|
272
|
+
end
|
|
273
|
+
cached.equal?(FAILED) ? nil : cached
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
def quantifier_end(source, start)
|
|
277
|
+
index = start + 1
|
|
278
|
+
index += 1 while index < source.length && source[index].match?(/[0-9]/)
|
|
279
|
+
return nil if index == start + 1 || index >= source.length
|
|
280
|
+
return index if source[index] == "}"
|
|
281
|
+
return nil unless source[index] == ","
|
|
282
|
+
|
|
283
|
+
index += 1
|
|
284
|
+
index += 1 while index < source.length && source[index].match?(/[0-9]/)
|
|
285
|
+
index < source.length && source[index] == "}" ? index : nil
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
# Characters the u flag lets an escape name as themselves, besides - in a class.
|
|
289
|
+
SYNTAX = "^$\\.*+?()[]{}|/"
|
|
290
|
+
|
|
291
|
+
# JavaScript's pattern in Onigmo's syntax, or nil for one the u flag rejects.
|
|
292
|
+
def translate(source)
|
|
293
|
+
out = +""
|
|
294
|
+
in_class = false
|
|
295
|
+
# The class's last item, which a dash can make the low end of a range, and whether one waits for its high end.
|
|
296
|
+
last_atom = nil
|
|
297
|
+
range_pending = false
|
|
298
|
+
# Notes an item of a class, refusing a range bounded by a class such as \d, as u does.
|
|
299
|
+
class_item = lambda do |atom|
|
|
300
|
+
next true unless in_class
|
|
301
|
+
|
|
302
|
+
if range_pending
|
|
303
|
+
next false if atom == :class_escape || last_atom == :class_escape
|
|
304
|
+
|
|
305
|
+
last_atom = nil
|
|
306
|
+
range_pending = false
|
|
307
|
+
else
|
|
308
|
+
last_atom = atom
|
|
309
|
+
end
|
|
310
|
+
true
|
|
311
|
+
end
|
|
312
|
+
index = 0
|
|
313
|
+
while index < source.length
|
|
314
|
+
current = source[index]
|
|
315
|
+
if current == "\\"
|
|
316
|
+
return nil if index + 1 >= source.length
|
|
317
|
+
|
|
318
|
+
code = source[index + 1]
|
|
319
|
+
index += 2
|
|
320
|
+
return nil unless class_item.call("dDwWsS".include?(code) ? :class_escape : :character)
|
|
321
|
+
|
|
322
|
+
out << case code
|
|
323
|
+
when "d" then in_class ? "0-9" : "[0-9]"
|
|
324
|
+
when "D" then in_class ? NOT_DIGITS : "[^0-9]"
|
|
325
|
+
when "w" then in_class ? "A-Za-z0-9_" : "[A-Za-z0-9_]"
|
|
326
|
+
when "W" then in_class ? NOT_WORD : "[^A-Za-z0-9_]"
|
|
327
|
+
when "s" then in_class ? SPACES : "[#{SPACES}]"
|
|
328
|
+
when "S" then in_class ? NOT_SPACES : "[^#{SPACES}]"
|
|
329
|
+
when "v" then "\\x0B"
|
|
330
|
+
when "b" then in_class ? "\\x08" : "\\b"
|
|
331
|
+
when "B"
|
|
332
|
+
return nil if in_class
|
|
333
|
+
|
|
334
|
+
"\\B"
|
|
335
|
+
when "n", "r", "t", "f" then "\\#{code}"
|
|
336
|
+
when "u"
|
|
337
|
+
hexadecimal = source[index, 4]
|
|
338
|
+
if hexadecimal&.match?(/\A\h{4}\z/)
|
|
339
|
+
index += 4
|
|
340
|
+
"\\u#{hexadecimal}"
|
|
341
|
+
elsif source[index] == "{" && (finish = source.index("}", index)) &&
|
|
342
|
+
source[(index + 1)...finish].match?(/\A\h+\z/) &&
|
|
343
|
+
source[(index + 1)...finish].hex <= 0x10FFFF
|
|
344
|
+
digits = source[(index + 1)...finish]
|
|
345
|
+
index = finish + 1
|
|
346
|
+
"\\u{#{digits}}"
|
|
347
|
+
else
|
|
348
|
+
return nil
|
|
349
|
+
end
|
|
350
|
+
when "x"
|
|
351
|
+
hexadecimal = source[index, 2]
|
|
352
|
+
return nil unless hexadecimal&.match?(/\A\h{2}\z/)
|
|
353
|
+
|
|
354
|
+
index += 2
|
|
355
|
+
"\\x#{hexadecimal}"
|
|
356
|
+
when "c"
|
|
357
|
+
letter = source[index]
|
|
358
|
+
return nil unless letter&.match?(/\A[A-Za-z]\z/)
|
|
359
|
+
|
|
360
|
+
index += 1
|
|
361
|
+
format("\\x%02X", letter.ord % 32)
|
|
362
|
+
when "0"
|
|
363
|
+
return nil if source[index]&.match?(/[0-9]/)
|
|
364
|
+
|
|
365
|
+
"\\x00"
|
|
366
|
+
when /\A[1-9]\z/
|
|
367
|
+
# A back reference, which lint refuses in delivered schemas.
|
|
368
|
+
"\\#{code}"
|
|
369
|
+
else
|
|
370
|
+
# The u flag rejects an escape without a meaning, such as \a or \- outside a class.
|
|
371
|
+
return nil unless SYNTAX.include?(code) || (in_class && code == "-")
|
|
372
|
+
|
|
373
|
+
Regexp.escape(code).then { |escaped| escaped == code ? "\\#{code}" : escaped }
|
|
374
|
+
end
|
|
375
|
+
next
|
|
376
|
+
end
|
|
377
|
+
if in_class
|
|
378
|
+
if current == "]"
|
|
379
|
+
in_class = false
|
|
380
|
+
out << "]"
|
|
381
|
+
index += 1
|
|
382
|
+
next
|
|
383
|
+
end
|
|
384
|
+
ranges = false
|
|
385
|
+
if current == "-" && last_atom && !range_pending && index + 1 < source.length && source[index + 1] != "]"
|
|
386
|
+
range_pending = true
|
|
387
|
+
ranges = true
|
|
388
|
+
elsif !class_item.call(:character)
|
|
389
|
+
return nil
|
|
390
|
+
end
|
|
391
|
+
if current == "[" || current == "&" || (current == "-" && !ranges)
|
|
392
|
+
# Onigmo nests classes on [ and intersects them on &&, where JavaScript reads the characters.
|
|
393
|
+
# A dash that is no range is escaped too, since Onigmo warns about one it must guess at.
|
|
394
|
+
out << "\\" << current
|
|
395
|
+
else
|
|
396
|
+
out << current
|
|
397
|
+
end
|
|
398
|
+
index += 1
|
|
399
|
+
next
|
|
400
|
+
end
|
|
401
|
+
case current
|
|
402
|
+
when "["
|
|
403
|
+
if source[index, 3] == "[^]"
|
|
404
|
+
# Any character, as JavaScript reads an empty negated class.
|
|
405
|
+
out << "[\\x00-\\u{10FFFF}]"
|
|
406
|
+
index += 3
|
|
407
|
+
next
|
|
408
|
+
elsif source[index, 2] == "[]"
|
|
409
|
+
# Nothing, as JavaScript reads an empty class.
|
|
410
|
+
out << "(?!)"
|
|
411
|
+
index += 2
|
|
412
|
+
next
|
|
413
|
+
end
|
|
414
|
+
in_class = true
|
|
415
|
+
last_atom = nil
|
|
416
|
+
range_pending = false
|
|
417
|
+
out << "["
|
|
418
|
+
if source[index + 1] == "^"
|
|
419
|
+
out << "^"
|
|
420
|
+
index += 1
|
|
421
|
+
end
|
|
422
|
+
when "^" then out << "\\A"
|
|
423
|
+
when "$" then out << "\\z"
|
|
424
|
+
when "." then out << "[^\\n\\r\\u2028\\u2029]"
|
|
425
|
+
when "{"
|
|
426
|
+
finish = quantifier_end(source, index)
|
|
427
|
+
return nil unless finish
|
|
428
|
+
|
|
429
|
+
out << source[index..finish]
|
|
430
|
+
index = finish
|
|
431
|
+
when "}", "]" then return nil
|
|
432
|
+
else out << current
|
|
433
|
+
end
|
|
434
|
+
index += 1
|
|
435
|
+
end
|
|
436
|
+
out
|
|
437
|
+
end
|
|
438
|
+
|
|
439
|
+
def match?(regex, text)
|
|
440
|
+
regex.match?(text)
|
|
441
|
+
rescue ArgumentError, Encoding::CompatibilityError
|
|
442
|
+
false
|
|
443
|
+
end
|
|
444
|
+
end
|
|
445
|
+
end
|
|
446
|
+
end
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Featurevisor
|
|
6
|
+
module Schemas
|
|
7
|
+
# Values as JSON has them, the way the JavaScript package reads them. Schema
|
|
8
|
+
# nodes come from the SDK with symbol keys at every depth, and values being
|
|
9
|
+
# validated have string keys.
|
|
10
|
+
module Values
|
|
11
|
+
ABSENT = Featurevisor::Conditions.missing_value
|
|
12
|
+
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
def number?(value)
|
|
16
|
+
value.is_a?(Integer) || value.is_a?(Float)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def finite_number?(value)
|
|
20
|
+
value.is_a?(Integer) || (value.is_a?(Float) && value.finite?)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# A finite number with no fraction, so 2.0 is an integer, as in JavaScript.
|
|
24
|
+
def integer?(value)
|
|
25
|
+
value.is_a?(Integer) || (value.is_a?(Float) && value.finite? && value == value.floor)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def primitive?(value)
|
|
29
|
+
value.nil? || value.is_a?(String) || value == true || value == false || finite_number?(value)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def boolean?(value)
|
|
33
|
+
value == true || value == false
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Deep equality of JSON values: arrays item by item, objects key by key in any order, numbers as doubles.
|
|
37
|
+
def same?(first, second)
|
|
38
|
+
return first.nil? && second.nil? if first.nil? || second.nil?
|
|
39
|
+
return boolean?(second) && first == second if boolean?(first)
|
|
40
|
+
return number?(second) && first.to_f == second.to_f if number?(first)
|
|
41
|
+
|
|
42
|
+
case first
|
|
43
|
+
when String then second.is_a?(String) && first == second
|
|
44
|
+
when Array
|
|
45
|
+
second.is_a?(Array) && first.length == second.length && first.each_with_index.all? { |item, index| same?(item, second[index]) }
|
|
46
|
+
when Hash
|
|
47
|
+
return false unless second.is_a?(Hash) && first.length == second.length
|
|
48
|
+
|
|
49
|
+
other = second.transform_keys(&:to_s)
|
|
50
|
+
first.all? { |key, item| other.key?(key.to_s) && same?(item, other[key.to_s]) }
|
|
51
|
+
else false
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# The kind of a value, as an issue names what it received.
|
|
56
|
+
def kind_of(value)
|
|
57
|
+
case value
|
|
58
|
+
when nil then "null"
|
|
59
|
+
when true, false then "boolean"
|
|
60
|
+
when Array then "array"
|
|
61
|
+
when Hash then "object"
|
|
62
|
+
when String then "string"
|
|
63
|
+
when Integer then "number"
|
|
64
|
+
when Float
|
|
65
|
+
return "number" if value.finite?
|
|
66
|
+
|
|
67
|
+
value.nan? ? "NaN" : (value.positive? ? "Infinity" : "-Infinity")
|
|
68
|
+
else value.class.name.to_s.split("::").last.downcase
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# A value as JSON decodes it, with string keys at every depth. Symbols are
|
|
73
|
+
# strings, and a Struct or anything with to_h becomes a Hash.
|
|
74
|
+
def plain(value, depth = 0)
|
|
75
|
+
return value if depth > 256
|
|
76
|
+
|
|
77
|
+
case value
|
|
78
|
+
when nil, String, Integer, Float, true, false then value
|
|
79
|
+
when Symbol then value.to_s
|
|
80
|
+
when Hash then value.each_with_object({}) { |(key, item), result| result[key.to_s] = plain(item, depth + 1) }
|
|
81
|
+
when Array then value.map { |item| plain(item, depth + 1) }
|
|
82
|
+
when Struct then plain(value.to_h, depth + 1)
|
|
83
|
+
else
|
|
84
|
+
value.respond_to?(:to_h) && !value.is_a?(Numeric) ? plain(value.to_h, depth + 1) : value
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# A number as JavaScript writes it: 2 for 2.0, 0.0001, 1e+21. The digits are the shortest that read
|
|
89
|
+
# back as the same double, which Float#to_s gives, placed as JavaScript places them.
|
|
90
|
+
def number_text(value)
|
|
91
|
+
return value.to_s if value.is_a?(Integer)
|
|
92
|
+
return "NaN" if value.nan?
|
|
93
|
+
return (value.positive? ? "Infinity" : "-Infinity") if value.infinite?
|
|
94
|
+
return "0" if value.zero?
|
|
95
|
+
|
|
96
|
+
match = /\A(-?)(\d+)(?:\.(\d+))?(?:e([+-]?\d+))?\z/.match(value.to_s)
|
|
97
|
+
return value.to_s unless match
|
|
98
|
+
|
|
99
|
+
sign = match[1]
|
|
100
|
+
digits = "#{match[2]}#{match[3]}"
|
|
101
|
+
# Where the decimal point sits among the digits, once leading zeros are gone.
|
|
102
|
+
point = match[2].length + match[4].to_i
|
|
103
|
+
trimmed = digits.sub(/\A0+/, "")
|
|
104
|
+
point -= digits.length - trimmed.length
|
|
105
|
+
digits = trimmed.sub(/0+\z/, "")
|
|
106
|
+
count = digits.length
|
|
107
|
+
return "#{sign}#{digits}#{"0" * (point - count)}" if count <= point && point <= 21
|
|
108
|
+
return "#{sign}#{digits[0, point]}.#{digits[point..]}" if point.positive? && point <= 21
|
|
109
|
+
return "#{sign}0.#{"0" * -point}#{digits}" if point > -6 && point <= 0
|
|
110
|
+
|
|
111
|
+
exponent = point - 1
|
|
112
|
+
mantissa = count == 1 ? digits : "#{digits[0]}.#{digits[1..]}"
|
|
113
|
+
"#{sign}#{mantissa}e#{exponent.negative? ? "-" : "+"}#{exponent.abs}"
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# JSON text as JavaScript's JSON.stringify writes it, for messages.
|
|
117
|
+
def stringify(value)
|
|
118
|
+
case value
|
|
119
|
+
when nil then "null"
|
|
120
|
+
when true, false then value.to_s
|
|
121
|
+
when Integer then value.to_s
|
|
122
|
+
when Float then value.finite? ? number_text(value) : "null"
|
|
123
|
+
when String then JSON.generate(value)
|
|
124
|
+
when Symbol then JSON.generate(value.to_s)
|
|
125
|
+
when Array then "[#{value.map { |item| stringify(item) }.join(",")}]"
|
|
126
|
+
when Hash then "{#{value.map { |key, item| "#{JSON.generate(key.to_s)}:#{stringify(item)}" }.join(",")}}"
|
|
127
|
+
else JSON.generate(value.to_s)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# A key of a schema node, which the SDK decodes as a symbol.
|
|
132
|
+
def child(node, key)
|
|
133
|
+
node.is_a?(Hash) ? node[key.to_sym] : nil
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|