field_struct-json_schema 1.0.0.pre1

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.
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../char_set'
4
+
5
+ module FieldStruct
6
+ module JSONSchema
7
+ module Pattern
8
+ module Safety
9
+ # JS-B62: expands an alternative sequence into flat paths of nodes, which {Chains} then evaluates. Internal.
10
+ module Paths
11
+ # One thing a path matches: a set of characters with a count. +kind+ is +:atom+ or +:whole+ (a variable-count
12
+ # group taken as one unit, carrying its first and last sets and, per alternative, the sets its iteration must match).
13
+ Node = Struct.new(:kind, :set, :low, :high, :starts, :ends, :needs)
14
+ # A flat path; +ended+ is true once a lookaround body has been entered (what follows starts again elsewhere).
15
+ Path = Struct.new(:nodes, :ended)
16
+ LIMIT = 1024
17
+ COPIES = 3
18
+ # The virtual item of JS-B63: an unanchored pattern may start matching anywhere.
19
+ RETRY = Node.new(:atom, CharSet::ALL, 0, Float::INFINITY).freeze
20
+
21
+ module_function
22
+
23
+ # @param sequence [::Array<Item>] one top-level alternative
24
+ # @return [::Array<Path>] its paths, each starting with the retry item
25
+ def expand_top(sequence)
26
+ expand(sequence, top: true).map { |path| Path.new([RETRY, *path.nodes], path.ended) }
27
+ end
28
+
29
+ # @param sequence [::Array<Item>]
30
+ # @param top [::Boolean] whether the sequence is a top-level alternative (a +\A+ there cuts the retry item off)
31
+ # @return [::Array<Path>]
32
+ def expand(sequence, top:)
33
+ sequence.reduce([Path.new([], false)]) do |paths, item|
34
+ variants = variants_of(item, top)
35
+ variants ? join(paths, variants) : paths
36
+ end
37
+ end
38
+
39
+ # @return [::Array<Path>, nil] nil for an item that adds nothing to a path
40
+ def variants_of(item, top)
41
+ case item.kind
42
+ when :atom then [Path.new([Node.new(:atom, item.set, item.min, item.max)], false)]
43
+ when :start then top ? [Path.new([Node.new(:atom, 0, 1, 1)], false)] : nil
44
+ when :group then group_variants(item)
45
+ when :look then look_variants(item)
46
+ end
47
+ end
48
+
49
+ # Every path so far followed by every variant; a path that already ended stays as it is.
50
+ def join(paths, variants)
51
+ joined = paths.flat_map do |path|
52
+ next [path] if path.ended
53
+
54
+ variants.map { |variant| Path.new(path.nodes + variant.nodes, variant.ended) }
55
+ end
56
+ throw :unsafe if joined.size > LIMIT
57
+ joined.uniq
58
+ end
59
+
60
+ # A lookaround adds the path that skips it plus, for each way through its body, a path that ends there.
61
+ def look_variants(item)
62
+ bodies = item.alternatives.flat_map { |sequence| expand(sequence, top: false) }
63
+ [Path.new([], false), *bodies.map { |path| Path.new(path.nodes, true) }]
64
+ end
65
+
66
+ # A group is unrolled up to three copies; a variable-count one is also taken whole (and, when unbounded, whole next
67
+ # to one unrolled iteration).
68
+ def group_variants(item)
69
+ iterations = item.alternatives.flat_map { |sequence| expand(sequence, top: false) }
70
+ top_count = item.max.infinite? ? COPIES : [item.max, COPIES].min
71
+ variants = (([item.min, COPIES].min)..top_count).flat_map { |count| copies(iterations, count) }
72
+ variants.concat(whole_variants(item, iterations)) if item.min != item.max
73
+ variants
74
+ end
75
+
76
+ def copies(iterations, count)
77
+ count.times.reduce([Path.new([], false)]) { |paths, _| join(paths, iterations) }
78
+ end
79
+
80
+ def whole_variants(item, iterations)
81
+ whole = Path.new([whole_node(item)], false)
82
+ return [whole] unless item.max.infinite?
83
+
84
+ [whole, *join([whole], iterations), *join(iterations, [whole])]
85
+ end
86
+
87
+ def whole_node(item)
88
+ sequences = item.alternatives
89
+ Node.new(:whole, union_of(sequences) { |s| chars_of_sequence(s) }, item.min, item.max,
90
+ union_of(sequences) { |s| first_of(s) }, union_of(sequences) { |s| first_of(s.reverse) },
91
+ sequences.map { |s| s.reject { |i| zero_width?(i) || i.min.zero? }.map { |i| chars_of(i) } })
92
+ end
93
+
94
+ def union_of(sequences, &)
95
+ sequences.map(&).reduce(0, :|)
96
+ end
97
+
98
+ def zero_width?(item)
99
+ %i[anchor start look].include?(item.kind)
100
+ end
101
+
102
+ # The union of the characters an item can match (a lookaround matches nothing itself).
103
+ def chars_of(item)
104
+ return 0 if item.kind == :look
105
+ return item.set unless item.alternatives
106
+
107
+ item.alternatives.reduce(0) { |set, sequence| set | chars_of_sequence(sequence) }
108
+ end
109
+
110
+ def chars_of_sequence(sequence)
111
+ sequence.reduce(0) { |set, item| set | chars_of(item) }
112
+ end
113
+
114
+ def first_of(sequence)
115
+ set = 0
116
+ sequence.each do |item|
117
+ set |= chars_of(item)
118
+ break unless item.min.positive? && !zero_width?(item)
119
+ end
120
+ set
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'safety/groups'
4
+ require_relative 'safety/paths'
5
+ require_relative 'safety/chains'
6
+
7
+ module FieldStruct
8
+ module JSONSchema
9
+ module Pattern
10
+ # The ReDoS analysis (JS-B60..B65): refuses a translated pattern a backtracking engine could run in exponential time,
11
+ # or in more than quadratic time, on a crafted input. Conservative: a false refusal is acceptable, a false acceptance
12
+ # is not (JS-I3). Internal.
13
+ module Safety
14
+ module_function
15
+
16
+ # @param alternatives [::Array<::Array<Item>>] the top-level alternatives the translator read
17
+ # @return [::Boolean] true when the pattern is safe to hand to a JavaScript engine
18
+ def safe?(alternatives)
19
+ catch(:unsafe) do
20
+ Groups.check(alternatives)
21
+ alternatives.each { |sequence| Chains.check(Paths.expand_top(sequence)) }
22
+ return true
23
+ end
24
+ false
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,296 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'char_class'
4
+ require_relative 'char_set'
5
+ require_relative 'escape'
6
+ require_relative 'item'
7
+
8
+ module FieldStruct
9
+ module JSONSchema
10
+ module Pattern
11
+ # The single left-to-right pass over a Regexp source (JS-B52): reads it into a sequence of {Item}s while emitting the
12
+ # ECMA-262 text. Anything without an exact equivalent, or malformed, makes {#call} answer nil. Internal.
13
+ class Translator
14
+ # The letter pairs Ruby folds to one character under +i+ (JS-B54): ss, ff, fi, fl, st.
15
+ MERGES = %w[ss ff fi fl st].freeze
16
+ INTERVAL = /\G\{(\d+)(?:(,)(\d*))?\}/
17
+ BAD_INTERVAL = /\G\{,\d*\}/
18
+ GROUP_NAME = /\G<([A-Za-z_][A-Za-z0-9_]*)>/
19
+ MAX_COUNT = 100_000
20
+ private_constant :MERGES, :INTERVAL, :BAD_INTERVAL, :GROUP_NAME, :MAX_COUNT
21
+
22
+ # @return [::Array<::Array<Item>>, nil] the top-level alternatives of the last successful {#call}
23
+ attr_reader :alternatives
24
+
25
+ # @param source [::String] a Regexp's source
26
+ # @param fold_case [::Boolean] whether the Regexp has the +i+ option
27
+ def initialize(source, fold_case:)
28
+ @src = source
29
+ @fold = fold_case
30
+ @pos = 0
31
+ @names = []
32
+ end
33
+
34
+ # @return [::String, nil] the translated pattern, or nil when there is none
35
+ def call
36
+ return nil if @fold && (!@src.ascii_only? || @src.match?(/ss|ff|fi|fl|st/i))
37
+
38
+ catch(:refuse) do
39
+ @alternatives = read_alternatives(0)
40
+ refuse if @pos < @src.size
41
+ Item.render(@alternatives)
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def refuse
48
+ throw :refuse
49
+ end
50
+
51
+ def peek(offset = 0)
52
+ @src[@pos + offset]
53
+ end
54
+
55
+ def advance
56
+ char = @src[@pos]
57
+ refuse if char.nil?
58
+ @pos += 1
59
+ char
60
+ end
61
+
62
+ def take?(text)
63
+ return false unless @src[@pos, text.size] == text
64
+
65
+ @pos += text.size
66
+ true
67
+ end
68
+
69
+ def read_alternatives(depth)
70
+ alternatives = [[]]
71
+ loop do
72
+ char = peek
73
+ break if char.nil?
74
+ break if char == ')' && depth.positive?
75
+
76
+ refuse if char == ')'
77
+ if take?('|')
78
+ alternatives << []
79
+ else
80
+ alternatives.last << read_term(alternatives.last.last, depth)
81
+ end
82
+ end
83
+ alternatives
84
+ end
85
+
86
+ def read_term(previous, depth)
87
+ item = read_atom(depth)
88
+ merge_check(previous, item) if @fold
89
+ read_quantifier(item) if item.quantifiable?
90
+ item
91
+ end
92
+
93
+ # JS-B54: one-letter atoms side by side that Ruby would fold into one string.
94
+ def merge_check(previous, item)
95
+ return unless previous&.letter && item.letter && previous.quantifier.empty?
96
+
97
+ refuse if MERGES.include?("#{previous.letter}#{item.letter}")
98
+ end
99
+
100
+ def read_atom(depth)
101
+ char = advance
102
+ case char
103
+ when '\\' then read_escape
104
+ when '[' then read_class
105
+ when '(' then read_group(depth)
106
+ when '*', '+', '?' then refuse
107
+ else read_plain(char)
108
+ end
109
+ end
110
+
111
+ def read_plain(char)
112
+ case char
113
+ when '.' then atom('[^\n]', CharSet::DOT)
114
+ when '^' then Item.new(kind: :anchor, body: '(?:^|(?<=\n)(?!$))')
115
+ when '$' then Item.new(kind: :anchor, body: '(?![^\n])')
116
+ when '{' then brace
117
+ when '}', ']' then atom("\\#{char}", CharSet.bit(char))
118
+ else literal(char)
119
+ end
120
+ end
121
+
122
+ def atom(text, set, letter: nil)
123
+ Item.new(kind: :atom, body: text, set: set, letter: letter)
124
+ end
125
+
126
+ # A { is a literal unless it starts a valid interval, which here has nothing to quantify.
127
+ def brace
128
+ refuse if @src.match?(INTERVAL, @pos - 1) || @src.match?(BAD_INTERVAL, @pos - 1)
129
+
130
+ atom('\{', CharSet.bit('{'))
131
+ end
132
+
133
+ def literal(char)
134
+ return atom("(?:#{char})", CharSet::NON_ASCII) if char.ord > 0xFFFF
135
+ return folded_letter(char) if @fold && char.match?(/\A[A-Za-z]\z/)
136
+
137
+ atom(char, CharSet.bit(char))
138
+ end
139
+
140
+ def folded_letter(char)
141
+ extra = CharSet::EXTRAS[char.downcase]
142
+ set = CharSet.bit(char) | CharSet.bit(char.swapcase)
143
+ set |= CharSet::NON_ASCII if extra
144
+ atom("[#{char.downcase}#{char.upcase}#{extra}]", set, letter: char.downcase)
145
+ end
146
+
147
+ def read_class
148
+ return take_anything if @src[@pos - 1, 6] == '[\s\S]'
149
+
150
+ item, @pos = CharClass.new(@src, @pos, fold_case: @fold).call
151
+ item
152
+ end
153
+
154
+ # [\s\S] means "any character" and is identical in both engines.
155
+ def take_anything
156
+ @pos += 5
157
+ atom('[\s\S]', CharSet::ALL)
158
+ end
159
+
160
+ def read_escape
161
+ char = advance
162
+ case char
163
+ when 'A' then Item.new(kind: :start, body: '^')
164
+ when 'z' then Item.new(kind: :anchor, body: '$')
165
+ when 's', 'S', 'h', 'H' then shorthand(char)
166
+ when 'd', 'D', 'w', 'W' then atom("\\#{char}", class_set(char))
167
+ when *Escape::CONTROL.keys then atom("\\#{char}", CharSet.bit(Escape::CONTROL.fetch(char)))
168
+ else other_escape(char)
169
+ end
170
+ end
171
+
172
+ def class_set(char)
173
+ set = char.downcase == 'd' ? CharSet::DIGIT : CharSet::WORD
174
+ char == char.downcase ? set : CharSet.negate(set)
175
+ end
176
+
177
+ def shorthand(char)
178
+ text = { 's' => '[\t\n\v\f\r ]', 'S' => '[^\t\n\v\f\r ]', 'h' => '[0-9a-fA-F]', 'H' => '[^0-9a-fA-F]' }.fetch(char)
179
+ set = char.downcase == 's' ? CharSet::SPACE : CharSet::HEX
180
+ atom(text, char == char.downcase ? set : CharSet.negate(set))
181
+ end
182
+
183
+ def other_escape(char)
184
+ case char
185
+ when '0' then zero_escape
186
+ when 'x', 'u', 'c' then numeric(char)
187
+ else punctuation_escape(char)
188
+ end
189
+ end
190
+
191
+ def zero_escape
192
+ refuse if peek&.match?(/\A[0-9]\z/)
193
+
194
+ atom('\0', CharSet.bit("\0"))
195
+ end
196
+
197
+ def punctuation_escape(char)
198
+ refuse if Escape.alphanumeric?(char) || char.ord > 127
199
+
200
+ atom(Escape::SYNTAX.include?(char) ? "\\#{char}" : char, CharSet.bit(char))
201
+ end
202
+
203
+ def numeric(letter)
204
+ refuse if @fold && letter != 'c'
205
+ text, char = Escape.numeric(letter, @src, @pos) || refuse
206
+ @pos += text.size - 2
207
+ atom(text, CharSet.bit(char))
208
+ end
209
+
210
+ def read_group(depth)
211
+ prefix, kind = group_prefix
212
+ alternatives = read_alternatives(depth + 1)
213
+ refuse unless take?(')')
214
+
215
+ Item.new(kind: kind, body: prefix, alternatives: alternatives)
216
+ end
217
+
218
+ def group_prefix
219
+ return ['(', :group] unless take?('?')
220
+ return ['(?:', :group] if take?(':')
221
+
222
+ %w[= ! <= <!].each { |mark| return ["(?#{mark}", :look] if take?(mark) }
223
+ refuse unless (match = @src.match(GROUP_NAME, @pos))
224
+
225
+ name = match[1]
226
+ refuse if @names.include?(name)
227
+
228
+ @names << name
229
+ @pos += match[0].size
230
+ ["(?<#{name}>", :group]
231
+ end
232
+
233
+ def read_quantifier(item)
234
+ min, max, text = quantifier_head
235
+ return unless text
236
+
237
+ refuse if @fold && merge_repeat?(item, text, min)
238
+
239
+ item.quantify(min, max, quantifier_tail(text))
240
+ refuse if stacked_quantifier?
241
+ end
242
+
243
+ # JS-B54: {n}, n >= 2, repeating a one-letter s or f atom, which Ruby may fold into one string.
244
+ def merge_repeat?(item, text, min)
245
+ text.start_with?('{') && min >= 2 && %w[s f].include?(item.letter)
246
+ end
247
+
248
+ # Possessive (a*+), stacked (a**, a{2}{3}): different meanings in Ruby and JavaScript.
249
+ def stacked_quantifier?
250
+ peek&.match?(/\A[*+?]\z/) || @src.match?(INTERVAL, @pos) || @src.match?(BAD_INTERVAL, @pos)
251
+ end
252
+
253
+ def quantifier_head
254
+ case peek
255
+ when '*' then take_quantifier(0, Float::INFINITY, '*')
256
+ when '+' then take_quantifier(1, Float::INFINITY, '+')
257
+ when '?' then take_quantifier(0, 1, '?')
258
+ when '{' then interval
259
+ end
260
+ end
261
+
262
+ def take_quantifier(min, max, text)
263
+ @pos += text.size
264
+ [min, max, text]
265
+ end
266
+
267
+ def interval
268
+ match = @src.match(INTERVAL, @pos)
269
+ return nil unless match
270
+
271
+ min = Integer(match[1], 10)
272
+ max = interval_max(match, min)
273
+ refuse if min > MAX_COUNT || max < min || (max.finite? && max > MAX_COUNT)
274
+
275
+ take_quantifier(min, max, match[0])
276
+ end
277
+
278
+ def interval_max(match, min)
279
+ return min unless match[2]
280
+ return Float::INFINITY if match[3].empty?
281
+
282
+ Integer(match[3], 10)
283
+ end
284
+
285
+ # A lazy ? after the quantifier; {n}? is "optional" in Ruby and lazy in JavaScript, so it is refused.
286
+ def quantifier_tail(text)
287
+ return text unless peek == '?'
288
+
289
+ refuse if text.match?(/\A\{\d+\}\z/)
290
+ @pos += 1
291
+ "#{text}?"
292
+ end
293
+ end
294
+ end
295
+ end
296
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'pattern/translator'
4
+ require_relative 'pattern/safety'
5
+
6
+ module FieldStruct
7
+ module JSONSchema
8
+ # The Regexp-to-ECMA-262 translator (JS-B50..B55). A Regexp is translated only when the result matches exactly the
9
+ # same strings under the +u+ flag; otherwise {translate} answers nil and the generator falls back to the
10
+ # +x-ruby-pattern+ annotation (JS-I2).
11
+ module Pattern
12
+ ENCODINGS = [::Encoding::UTF_8, ::Encoding::US_ASCII].freeze
13
+ private_constant :ENCODINGS
14
+
15
+ module_function
16
+
17
+ # @param regexp [::Regexp]
18
+ # @return [::String, nil] the pattern, or nil when there is no exact equivalent
19
+ def translate(regexp)
20
+ return nil if regexp.options.anybits?(::Regexp::EXTENDED | ::Regexp::MULTILINE)
21
+
22
+ source = regexp.source
23
+ return nil unless source.valid_encoding? && (ENCODINGS.include?(source.encoding) || source.ascii_only?)
24
+
25
+ translator = Translator.new(source, fold_case: regexp.options.anybits?(::Regexp::IGNORECASE))
26
+ text = translator.call
27
+ text if text && Safety.safe?(translator.alternatives)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FieldStruct
4
+ module JSONSchema
5
+ # The schema of a scalar type (JS-B8..B10) and the schema of a custom type without a hook (JS-B12).
6
+ module Scalar
7
+ T = FieldStruct::Types
8
+ MAPPERS = [
9
+ [T::Boolean, :boolean], [T::Integer, :integer], [T::Float, :float], [T::BigDecimal, :decimal],
10
+ [T::DateTime, :datetime], [T::Time, :datetime], [T::Date, :date], [T::Symbol, :symbol],
11
+ [T::String, :string], [T::Value, :value]
12
+ ].freeze
13
+ private_constant :MAPPERS
14
+
15
+ module_function
16
+
17
+ # @param type [FieldStruct::Types::Base]
18
+ # @param options [::Hash{::Symbol=>::Object}] the options the value is checked with
19
+ # @return [::Hash{::String=>::Object}]
20
+ def call(type, options)
21
+ _, mapper = MAPPERS.find { |klass, _| type.is_a?(klass) }
22
+ mapper ? send(mapper, type, options) : { 'x-ruby-type' => ruby_type_names(type) }
23
+ end
24
+
25
+ # @param type [FieldStruct::Types::Base]
26
+ # @return [::String]
27
+ def ruby_type_names(type)
28
+ [type.ruby_type].flatten.map { |klass| klass.name || klass.inspect }.join(' | ')
29
+ end
30
+
31
+ # @param range [::Range, ::Array, nil]
32
+ # @return [::Hash{::String=>::Object}]
33
+ def bounds(range)
34
+ case range
35
+ when ::Array then { 'enum' => range }
36
+ when ::Range then range_bounds(range)
37
+ else {}
38
+ end
39
+ end
40
+
41
+ # @param range [::Range]
42
+ # @return [::Hash{::String=>::Object}]
43
+ def range_bounds(range)
44
+ keys = {}
45
+ keys['minimum'] = number(range.begin) if finite?(range.begin)
46
+ keys[range.exclude_end? ? 'exclusiveMaximum' : 'maximum'] = number(range.end) if finite?(range.end)
47
+ keys
48
+ end
49
+
50
+ # @param value [::Object]
51
+ # @return [::Boolean]
52
+ def finite?(value)
53
+ !value.nil? && !(value.respond_to?(:infinite?) && value.infinite?)
54
+ end
55
+
56
+ # @param value [::Numeric]
57
+ # @return [::Integer, ::Float]
58
+ def number(value)
59
+ value.is_a?(::Integer) || value.is_a?(::Float) ? value : value.to_f
60
+ end
61
+
62
+ # @return [::Hash{::String=>::Object}]
63
+ def boolean(*) = { 'type' => 'boolean' }
64
+
65
+ # @return [::Hash{::String=>::Object}]
66
+ def integer(_type, options) = { 'type' => 'integer' }.merge!(bounds(options[:in]))
67
+
68
+ # @return [::Hash{::String=>::Object}]
69
+ def float(_type, options) = { 'type' => 'number' }.merge!(bounds(options[:in]))
70
+
71
+ # A JSON number, as +to_json+ writes it, unless the field's +json:+ or its type's default keeps the string (JS-B24).
72
+ #
73
+ # @param type [FieldStruct::Types::BigDecimal]
74
+ # @param options [::Hash{::Symbol=>::Object}]
75
+ # @return [::Hash{::String=>::Object}]
76
+ def decimal(type, options)
77
+ return { 'type' => 'number' } unless options.fetch(:json) { type.class.json_default } == :string
78
+
79
+ { 'type' => 'string', 'pattern' => DECIMAL_PATTERN }
80
+ end
81
+
82
+ # @return [::Hash{::String=>::Object}]
83
+ def value(*) = {}
84
+
85
+ # @return [::Hash{::String=>::Object}]
86
+ def datetime(_type, options) = temporal('date-time', options)
87
+
88
+ # @return [::Hash{::String=>::Object}]
89
+ def date(_type, options) = temporal('date', options)
90
+
91
+ # @param name [::String]
92
+ # @param options [::Hash{::Symbol=>::Object}]
93
+ # @return [::Hash{::String=>::Object}]
94
+ def temporal(name, options)
95
+ format = options[:format]
96
+ format.nil? || format == :iso8601 ? { 'type' => 'string', 'format' => name } : { 'type' => 'string' }
97
+ end
98
+
99
+ # @return [::Hash{::String=>::Object}]
100
+ def symbol(_type, options)
101
+ schema = { 'type' => 'string' }
102
+ schema['enum'] = options[:enum].map(&:to_s) if options[:enum]
103
+ schema
104
+ end
105
+
106
+ # @param type [FieldStruct::Types::String]
107
+ # @param options [::Hash{::Symbol=>::Object}]
108
+ # @return [::Hash{::String=>::Object}]
109
+ def string(type, options)
110
+ schema = { 'type' => 'string' }
111
+ format = options[:format]
112
+ schema.merge!(format_keys(type, format)) if format.is_a?(::Regexp)
113
+ schema['enum'] = options[:enum] if options[:enum]
114
+ schema.merge!(length_keys(type, options[:length])) if options[:length]
115
+ schema
116
+ end
117
+
118
+ # +minLength+/+maxLength+ from +length:+ (JS-B10); +:binary+ counts bytes, so it keeps only the maximum.
119
+ #
120
+ # @param type [FieldStruct::Types::String]
121
+ # @param bounds [::Range]
122
+ # @return [::Hash{::String=>::Object}]
123
+ def length_keys(type, bounds)
124
+ keys = {}
125
+ keys['minLength'] = bounds.begin if bounds.begin && !type.is_a?(T::Binary)
126
+ last = bounds.end
127
+ keys['maxLength'] = bounds.exclude_end? ? last - 1 : last if last
128
+ keys
129
+ end
130
+
131
+ # @param type [FieldStruct::Types::String]
132
+ # @param regexp [::Regexp]
133
+ # @return [::Hash{::String=>::Object}]
134
+ def format_keys(type, regexp)
135
+ return { 'format' => 'uuid' } if type.is_a?(T::UUID) && regexp == T::UUID.default_format
136
+
137
+ pattern = FieldStruct::JSONSchema.pattern_for(regexp)
138
+ pattern ? { 'pattern' => pattern } : { 'x-ruby-pattern' => regexp.source }
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FieldStruct
4
+ module JSONSchema
5
+ # What a cached schema depends on in one class (JS-B41): its name, its +unknown_attributes+, its Field objects
6
+ # and its +serialize+ mapping for the format, the latter two compared by identity.
7
+ class Stamp
8
+ # Reads the stamp of +klass+ now.
9
+ #
10
+ # @param klass [::Class]
11
+ # @param serialization [::Symbol]
12
+ # @return [Stamp]
13
+ def self.of(klass, serialization)
14
+ metadata = klass.metadata
15
+ new(klass.name, klass.unknown_attributes, metadata.to_a, metadata.serialization(serialization))
16
+ end
17
+
18
+ # @param name [::String, nil]
19
+ # @param unknown [::Symbol]
20
+ # @param fields [::Array<FieldStruct::Field>]
21
+ # @param mapping [::Hash{::Symbol=>::Object}]
22
+ def initialize(name, unknown, fields, mapping)
23
+ @name = name
24
+ @unknown = unknown
25
+ @fields = fields
26
+ @mapping = mapping
27
+ freeze
28
+ end
29
+
30
+ # Compares with the class as it is now, without building a second stamp.
31
+ #
32
+ # @param klass [::Class]
33
+ # @param serialization [::Symbol]
34
+ # @return [::Boolean] whether nothing the schema depends on changed
35
+ def current?(klass, serialization)
36
+ metadata = klass.metadata
37
+ @name == klass.name && @unknown == klass.unknown_attributes && @mapping.equal?(metadata.serialization(serialization)) &&
38
+ same_fields?(metadata)
39
+ end
40
+
41
+ private
42
+
43
+ def same_fields?(metadata)
44
+ index = 0
45
+ metadata.each do |field|
46
+ return false unless @fields[index].equal?(field)
47
+
48
+ index += 1
49
+ end
50
+ index == @fields.size
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FieldStruct
4
+ # JSON Schema generation for field_struct classes.
5
+ module JSONSchema
6
+ # The gem version.
7
+ # @return [::String]
8
+ VERSION = '1.0.0.pre1'
9
+ end
10
+ end