interfacets 0.9.9 → 0.99999
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 +4 -4
- data/LICENSE +9 -17
- data/lib/interfacets/client/actor.rb +2 -1
- data/lib/interfacets/client/assets.rb +77 -63
- data/lib/interfacets/client/bus.rb +31 -8
- data/lib/interfacets/client/channels/api.rb +141 -35
- data/lib/interfacets/client/channels/audio.rb +13 -10
- data/lib/interfacets/client/channels/base.rb +2 -2
- data/lib/interfacets/client/channels/pubsub.rb +69 -0
- data/lib/interfacets/client/channels/react/builder.rb +42 -72
- data/lib/interfacets/client/channels/react/channel.rb +7 -5
- data/lib/interfacets/client/channels/react/evaluator.rb +6 -4
- data/lib/interfacets/client/channels/timer.rb +8 -7
- data/lib/interfacets/client/channels/url.rb +39 -6
- data/lib/interfacets/client/registry.rb +10 -70
- data/lib/interfacets/client/slots.rb +45 -0
- data/lib/interfacets/client/system.rb +20 -15
- data/lib/interfacets/client/template.rb +90 -0
- data/lib/interfacets/client/utils/mruby_patches.rb +8 -4
- data/lib/interfacets/client/utils/open_struct.rb +88 -77
- data/lib/interfacets/component_registry.rb +120 -0
- data/lib/interfacets/component_schema_parser.rb +88 -0
- data/lib/interfacets/mruby/build_config.rb +8 -4
- data/lib/interfacets/mruby/entrypoint.rb +2 -2
- data/lib/interfacets/server/api.rb +44 -27
- data/lib/interfacets/server/assets.rb +69 -28
- data/lib/interfacets/server/basic_router.rb +38 -23
- data/lib/interfacets/server/bus.rb +8 -14
- data/lib/interfacets/server/registry.rb +31 -185
- data/lib/interfacets/shared/basic_routable.rb +48 -0
- data/lib/interfacets/shared/entities/bus.rb +623 -177
- data/lib/interfacets/shared/entities/changeset.rb +432 -0
- data/lib/interfacets/shared/entities/collection_proxy.rb +132 -72
- data/lib/interfacets/shared/entities/specs/handlers.rb +74 -37
- data/lib/interfacets/shared/entities/specs.rb +223 -20
- data/lib/interfacets/shared/entity.rb +124 -110
- data/lib/interfacets/shared/entity_dsl.rb +272 -0
- data/lib/interfacets/shared/facet.rb +260 -0
- data/lib/interfacets/shared/generated_store.rb +74 -35
- data/lib/interfacets/shared/refreshable.rb +85 -0
- data/lib/interfacets/shared/utils.rb +36 -32
- data/lib/interfacets/shared/validations.rb +749 -25
- data/lib/interfacets/shared/view.rb +120 -0
- data/lib/interfacets/test/component_registry.rb +82 -0
- data/lib/interfacets/test/js/inline_bus.rb +68 -33
- data/lib/interfacets/test/js/nodo_bus.rb +37 -9
- data/lib/interfacets/test/js/receivers/api.rb +17 -4
- data/lib/interfacets/test/js/receivers/audio.rb +61 -0
- data/lib/interfacets/test/js/receivers/pubsub.rb +50 -0
- data/lib/interfacets/test/js/receivers/react/node/xml_parser.rb +103 -0
- data/lib/interfacets/test/js/receivers/react/node.rb +91 -71
- data/lib/interfacets/test/js/receivers/react.rb +15 -5
- data/lib/interfacets/test/js/receivers/timer.rb +81 -0
- data/lib/interfacets/test/js/receivers/url.rb +51 -10
- data/lib/interfacets/test/standard_elements.yml +173 -0
- data/lib/interfacets/test/ui_simulator.rb +145 -0
- data/lib/interfacets/test/validation_engine.rb +184 -0
- data/lib/interfacets/test.rb +1 -7
- data/lib/interfacets/version.rb +1 -1
- data/lib/interfacets.rb +13 -2
- metadata +51 -37
- data/.rubocop.yml +0 -180
- data/LICENSE.txt +0 -21
- data/Rakefile +0 -10
- data/lib/interfacets/client/channels/page_visibility.rb +0 -21
- data/lib/interfacets/client/channels/react/dom.rb +0 -91
- data/lib/interfacets/client/channels/speech_to_text.rb +0 -100
- data/lib/interfacets/client/config.rb +0 -22
- data/lib/interfacets/client/delegator.rb +0 -37
- data/lib/interfacets/client/facet.rb +0 -26
- data/lib/interfacets/client/facet2.rb +0 -15
- data/lib/interfacets/client/facets/attributes/accessor.rb +0 -28
- data/lib/interfacets/client/facets/attributes/association.rb +0 -50
- data/lib/interfacets/client/facets/attributes/bind.rb +0 -25
- data/lib/interfacets/client/facets/attributes/collection.rb +0 -47
- data/lib/interfacets/client/facets/attributes/readonly.rb +0 -19
- data/lib/interfacets/client/facets/deserializer.rb +0 -30
- data/lib/interfacets/client/facets/schema/deserializer.rb +0 -63
- data/lib/interfacets/client/facets/schema.rb +0 -63
- data/lib/interfacets/client/facets/serializer.rb +0 -18
- data/lib/interfacets/client/view.rb +0 -47
- data/lib/interfacets/server/assets/facet.rb +0 -61
- data/lib/interfacets/server/basic_routable.rb +0 -40
- data/lib/interfacets/server/config.rb +0 -87
- data/lib/interfacets/server/facet.rb +0 -51
- data/lib/interfacets/server/facets/deserializer.rb +0 -25
- data/lib/interfacets/server/facets/schema/serializer.rb +0 -54
- data/lib/interfacets/server/facets/serializer.rb +0 -50
- data/lib/interfacets/shared/entity_collection.rb +0 -88
- data/lib/interfacets/test/browser.rb +0 -63
|
@@ -3,29 +3,562 @@
|
|
|
3
3
|
module Interfacets
|
|
4
4
|
module Shared
|
|
5
5
|
module Validations
|
|
6
|
-
extend
|
|
6
|
+
extend(ActiveSupport::Concern)
|
|
7
|
+
|
|
8
|
+
class Error
|
|
9
|
+
NOT_GIVEN = Object.new
|
|
10
|
+
|
|
11
|
+
attr_reader(
|
|
12
|
+
:attribute,
|
|
13
|
+
:message,
|
|
14
|
+
:type,
|
|
15
|
+
:origin,
|
|
16
|
+
:value,
|
|
17
|
+
:value_captured
|
|
18
|
+
)
|
|
19
|
+
def initialize(
|
|
20
|
+
attribute:,
|
|
21
|
+
message:,
|
|
22
|
+
type: nil,
|
|
23
|
+
origin: :client,
|
|
24
|
+
value: NOT_GIVEN,
|
|
25
|
+
value_captured: NOT_GIVEN
|
|
26
|
+
)
|
|
27
|
+
@attribute = attribute.to_sym
|
|
28
|
+
@message = message.to_s
|
|
29
|
+
@type = type || :custom
|
|
30
|
+
@origin = origin.to_sym
|
|
31
|
+
@value = value.equal?(NOT_GIVEN) ? nil : value
|
|
32
|
+
@value_captured = (
|
|
33
|
+
if value_captured.equal?(NOT_GIVEN)
|
|
34
|
+
!value.equal?(NOT_GIVEN)
|
|
35
|
+
else
|
|
36
|
+
value_captured ? true : false
|
|
37
|
+
end
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def value_captured?
|
|
42
|
+
value_captured
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def server?
|
|
46
|
+
origin == :server
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def client?
|
|
50
|
+
origin == :client
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def to_s
|
|
54
|
+
message
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def ==(other)
|
|
58
|
+
if other.is_a?(Error)
|
|
59
|
+
attribute == other.attribute && message == other.message && origin == other.origin
|
|
60
|
+
else
|
|
61
|
+
message == other.to_s
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
7
65
|
|
|
8
66
|
class Errors
|
|
9
|
-
include
|
|
67
|
+
include(Enumerable)
|
|
68
|
+
|
|
69
|
+
attr_reader(:entity, :errors)
|
|
70
|
+
def initialize(entity = nil)
|
|
71
|
+
@entity = entity
|
|
72
|
+
@errors = {}
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def add(attribute, message_or_type = :invalid, **options)
|
|
76
|
+
attr_sym = attribute.to_sym
|
|
77
|
+
origin = (options[:origin] || :client).to_sym
|
|
78
|
+
type = options[:type] || (message_or_type.is_a?(Symbol) ? message_or_type : :custom)
|
|
79
|
+
message = (
|
|
80
|
+
if message_or_type.is_a?(Symbol)
|
|
81
|
+
options[:message] || default_message_for(
|
|
82
|
+
attr_sym,
|
|
83
|
+
message_or_type,
|
|
84
|
+
options
|
|
85
|
+
)
|
|
86
|
+
else
|
|
87
|
+
message_or_type.to_s
|
|
88
|
+
end
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
has_value = options.key?(:value) || (entity&.respond_to?(attr_sym))
|
|
92
|
+
val = (
|
|
93
|
+
if options.key?(:value)
|
|
94
|
+
options[:value]
|
|
95
|
+
elsif entity.respond_to?(attr_sym)
|
|
96
|
+
entity.send(attr_sym)
|
|
97
|
+
end
|
|
98
|
+
)
|
|
99
|
+
val = Utils.deep_dup(val) if has_value
|
|
100
|
+
|
|
101
|
+
error_kwargs = {
|
|
102
|
+
attribute: attr_sym,
|
|
103
|
+
message: message,
|
|
104
|
+
type: type,
|
|
105
|
+
origin: origin,
|
|
106
|
+
value_captured: has_value,
|
|
107
|
+
}
|
|
108
|
+
error_kwargs[:value] = val if has_value
|
|
109
|
+
|
|
110
|
+
list = (errors[attr_sym] ||= [])
|
|
111
|
+
existing_idx = list.find_index { |e| e.message == message }
|
|
112
|
+
if existing_idx
|
|
113
|
+
if origin == :server
|
|
114
|
+
error = Error.new(**error_kwargs)
|
|
115
|
+
list[existing_idx] = error
|
|
116
|
+
return error
|
|
117
|
+
end
|
|
118
|
+
return list[existing_idx]
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
error = Error.new(**error_kwargs)
|
|
122
|
+
list << error
|
|
123
|
+
error
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def clear
|
|
127
|
+
errors.clear
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def [](attribute)
|
|
131
|
+
messages_for(attribute)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def for(attribute)
|
|
135
|
+
errors[attribute.to_sym] || []
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def messages_for(attribute)
|
|
139
|
+
(errors[attribute.to_sym] || []).map(&:message)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def full_messages
|
|
143
|
+
messages = []
|
|
144
|
+
errors.each do |attr, err_list|
|
|
145
|
+
err_list.each do |err|
|
|
146
|
+
messages << full_message(attr, err.message)
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
messages
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def full_messages_for(attribute)
|
|
153
|
+
(errors[attribute.to_sym] || []).map do |err|
|
|
154
|
+
full_message(attribute, err.message)
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def full_message(attribute, message)
|
|
159
|
+
return message if attribute.to_sym == :base
|
|
160
|
+
|
|
161
|
+
"#{human_attribute_name(attribute)} #{message}".strip
|
|
162
|
+
end
|
|
10
163
|
|
|
11
|
-
def
|
|
12
|
-
|
|
164
|
+
def each(&block)
|
|
165
|
+
if block
|
|
166
|
+
errors.each do |attr, err_list|
|
|
167
|
+
err_list.each do |err|
|
|
168
|
+
yield(attr, err.message)
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
else
|
|
172
|
+
to_enum(:each)
|
|
173
|
+
end
|
|
13
174
|
end
|
|
14
175
|
|
|
15
|
-
def
|
|
16
|
-
|
|
176
|
+
def to_h
|
|
177
|
+
res = {}
|
|
178
|
+
errors.each do |k, v|
|
|
179
|
+
next if v.empty?
|
|
180
|
+
|
|
181
|
+
res[k] = v.map(&:message)
|
|
182
|
+
end
|
|
183
|
+
res
|
|
17
184
|
end
|
|
185
|
+
alias_method(:to_hash, :to_h)
|
|
18
186
|
|
|
19
|
-
def
|
|
20
|
-
|
|
187
|
+
def server_errors
|
|
188
|
+
errors
|
|
189
|
+
.values
|
|
190
|
+
.flatten
|
|
191
|
+
.select(&:server?)
|
|
21
192
|
end
|
|
22
193
|
|
|
23
|
-
def
|
|
24
|
-
|
|
194
|
+
def client_errors
|
|
195
|
+
errors
|
|
196
|
+
.values
|
|
197
|
+
.flatten
|
|
198
|
+
.select(&:client?)
|
|
25
199
|
end
|
|
26
200
|
|
|
27
201
|
def empty?
|
|
28
|
-
|
|
202
|
+
errors.empty? || errors.values.all?(&:empty?)
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def include?(attribute)
|
|
206
|
+
attr_sym = attribute.to_sym
|
|
207
|
+
errors.key?(attr_sym) && !errors[attr_sym].empty?
|
|
208
|
+
end
|
|
209
|
+
alias_method(:key?, :include?)
|
|
210
|
+
alias_method(:has_key?, :include?)
|
|
211
|
+
|
|
212
|
+
def any?(attribute = nil)
|
|
213
|
+
if attribute
|
|
214
|
+
include?(attribute)
|
|
215
|
+
else
|
|
216
|
+
!empty?
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def count
|
|
221
|
+
errors.values.reduce(0) { |acc, errs| acc + errs.size }
|
|
222
|
+
end
|
|
223
|
+
alias_method(:size, :count)
|
|
224
|
+
|
|
225
|
+
def delete(attribute)
|
|
226
|
+
errors.delete(attribute.to_sym)
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def visible_for?(attribute)
|
|
230
|
+
attr_sym = attribute.to_sym
|
|
231
|
+
errs = errors[attr_sym]
|
|
232
|
+
return false if errs.nil? || errs.empty?
|
|
233
|
+
|
|
234
|
+
errs.any? { |err| visible_error?(err) }
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def visible_errors_for(attribute)
|
|
238
|
+
attr_sym = attribute.to_sym
|
|
239
|
+
errs = errors[attr_sym]
|
|
240
|
+
return [] if errs.nil? || errs.empty?
|
|
241
|
+
|
|
242
|
+
errs.select { |err| visible_error?(err) }.map(&:message)
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
private
|
|
246
|
+
|
|
247
|
+
def visible_error?(error)
|
|
248
|
+
if error.server?
|
|
249
|
+
if entity.respond_to?(error.attribute)
|
|
250
|
+
current_val = entity.send(error.attribute)
|
|
251
|
+
error.value_captured? ? (error.value == current_val) : true
|
|
252
|
+
else
|
|
253
|
+
true
|
|
254
|
+
end
|
|
255
|
+
else
|
|
256
|
+
return false unless entity
|
|
257
|
+
return true if entity.respond_to?(:submitted?) && entity.submitted?
|
|
258
|
+
return true if entity.respond_to?(:touched?) && entity.touched?(error.attribute)
|
|
259
|
+
return true if entity.respond_to?(:changed?) && entity.changed?(error.attribute)
|
|
260
|
+
|
|
261
|
+
false
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
def human_attribute_name(attribute)
|
|
266
|
+
attribute
|
|
267
|
+
.to_s
|
|
268
|
+
.tr("_", " ")
|
|
269
|
+
.capitalize
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def default_message_for(attribute, type, options = {})
|
|
273
|
+
case type
|
|
274
|
+
when :blank
|
|
275
|
+
"can't be blank"
|
|
276
|
+
when :too_short
|
|
277
|
+
"is too short (minimum is #{options[:count]})"
|
|
278
|
+
when :too_long
|
|
279
|
+
"is too long (maximum is #{options[:count]})"
|
|
280
|
+
when :wrong_length
|
|
281
|
+
"is the wrong length (should be #{options[:count]} characters)"
|
|
282
|
+
when :not_a_number
|
|
283
|
+
"is not a number"
|
|
284
|
+
when :greater_than
|
|
285
|
+
"must be greater than #{options[:count]}"
|
|
286
|
+
when :greater_than_or_equal_to
|
|
287
|
+
"must be greater than or equal to #{options[:count]}"
|
|
288
|
+
when :less_than
|
|
289
|
+
"must be less than #{options[:count]}"
|
|
290
|
+
when :less_than_or_equal_to
|
|
291
|
+
"must be less than or equal to #{options[:count]}"
|
|
292
|
+
when :equal_to
|
|
293
|
+
"must be equal to #{options[:count]}"
|
|
294
|
+
when :not_an_integer
|
|
295
|
+
"must be an integer"
|
|
296
|
+
when :inclusion
|
|
297
|
+
"is not included in the list"
|
|
298
|
+
when :exclusion
|
|
299
|
+
"is reserved"
|
|
300
|
+
when :confirmation
|
|
301
|
+
"doesn't match #{human_attribute_name(
|
|
302
|
+
options[:attribute] || attribute
|
|
303
|
+
)}"
|
|
304
|
+
when :invalid
|
|
305
|
+
"is invalid"
|
|
306
|
+
else
|
|
307
|
+
raise(
|
|
308
|
+
ArgumentError.new(
|
|
309
|
+
"Unknown error type #{type.inspect} for #{attribute.inspect}: " \
|
|
310
|
+
"pass message: or a String message to add a custom error"
|
|
311
|
+
)
|
|
312
|
+
)
|
|
313
|
+
end
|
|
314
|
+
end
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
class ValidatorRule
|
|
318
|
+
attr_reader(:type)
|
|
319
|
+
attr_reader(:attributes, :options, :block)
|
|
320
|
+
def initialize(attributes, options = {}, &block)
|
|
321
|
+
@attributes = Array(attributes).map(&:to_sym)
|
|
322
|
+
@options = options
|
|
323
|
+
@block = block
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
def validate(entity)
|
|
327
|
+
return if condition_skips?(entity)
|
|
328
|
+
|
|
329
|
+
if block
|
|
330
|
+
Utils.permissive_exec(entity, entity, &block)
|
|
331
|
+
else
|
|
332
|
+
attributes.each do |attr|
|
|
333
|
+
val = entity.respond_to?(attr) ? entity.send(attr) : nil
|
|
334
|
+
validate_attribute(entity, attr, val)
|
|
335
|
+
end
|
|
336
|
+
end
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
private
|
|
340
|
+
|
|
341
|
+
def condition_skips?(entity)
|
|
342
|
+
return true if options[:if] && !condition_result(entity, options[:if])
|
|
343
|
+
return true if options[:unless] && condition_result(entity, options[:unless])
|
|
344
|
+
|
|
345
|
+
false
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
def condition_result(entity, cond)
|
|
349
|
+
if cond.is_a?(Proc)
|
|
350
|
+
Utils.permissive_exec(entity, entity, &cond)
|
|
351
|
+
else
|
|
352
|
+
entity.send(cond)
|
|
353
|
+
end
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
def validate_attribute(entity, attr, val)
|
|
357
|
+
return if options[:allow_nil] && val.nil?
|
|
358
|
+
return if options[:allow_blank] && Utils.blank?(val)
|
|
359
|
+
|
|
360
|
+
validate_rules(entity, attr, val)
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
def validate_rules(entity, attr, val)
|
|
364
|
+
validate_presence(entity, attr, val)
|
|
365
|
+
validate_format(entity, attr, val)
|
|
366
|
+
validate_length(entity, attr, val)
|
|
367
|
+
validate_inclusion(entity, attr, val)
|
|
368
|
+
validate_exclusion(entity, attr, val)
|
|
369
|
+
|
|
370
|
+
if options[:confirmation]
|
|
371
|
+
opts = options[:confirmation].is_a?(Hash) ? options[:confirmation] : {}
|
|
372
|
+
validate_confirmation(entity, attr, val, opts)
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
if options[:numericality]
|
|
376
|
+
opts = options[:numericality].is_a?(Hash) ? options[:numericality] : {}
|
|
377
|
+
validate_numericality(entity, attr, val, opts)
|
|
378
|
+
end
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
def validate_presence(entity, attr, val)
|
|
382
|
+
return unless options[:presence] && Utils.blank?(val)
|
|
383
|
+
|
|
384
|
+
msg = options[:presence].is_a?(Hash) ? options[:presence][:message] : nil
|
|
385
|
+
entity.errors.add(attr, :blank, message: msg)
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
def validate_format(entity, attr, val)
|
|
389
|
+
return unless options[:format]
|
|
390
|
+
|
|
391
|
+
opts = options[:format].is_a?(Hash) ? options[:format] : { with: options[:format] }
|
|
392
|
+
val_str = val.to_s
|
|
393
|
+
invalid = (opts[:with] && !opts[:with].match(val_str)) ||
|
|
394
|
+
(opts[:without]&.match(val_str))
|
|
395
|
+
entity.errors.add(
|
|
396
|
+
attr,
|
|
397
|
+
:invalid,
|
|
398
|
+
message: opts[:message]
|
|
399
|
+
) if invalid
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
def validate_length(entity, attr, val)
|
|
403
|
+
return unless options[:length]
|
|
404
|
+
|
|
405
|
+
opts = options[:length]
|
|
406
|
+
len = val ? val.to_s.length : 0
|
|
407
|
+
if opts[:minimum] && len < opts[:minimum]
|
|
408
|
+
entity.errors.add(
|
|
409
|
+
attr,
|
|
410
|
+
:too_short,
|
|
411
|
+
count: opts[:minimum],
|
|
412
|
+
message: opts[:message]
|
|
413
|
+
)
|
|
414
|
+
elsif opts[:maximum] && len > opts[:maximum]
|
|
415
|
+
entity.errors.add(
|
|
416
|
+
attr,
|
|
417
|
+
:too_long,
|
|
418
|
+
count: opts[:maximum],
|
|
419
|
+
message: opts[:message]
|
|
420
|
+
)
|
|
421
|
+
elsif opts[:is] && len != opts[:is]
|
|
422
|
+
entity.errors.add(
|
|
423
|
+
attr,
|
|
424
|
+
:wrong_length,
|
|
425
|
+
count: opts[:is],
|
|
426
|
+
message: opts[:message]
|
|
427
|
+
)
|
|
428
|
+
elsif opts[:in] && !opts[:in].include?(len)
|
|
429
|
+
if len < opts[:in].min
|
|
430
|
+
entity.errors.add(
|
|
431
|
+
attr,
|
|
432
|
+
:too_short,
|
|
433
|
+
count: opts[:in].min,
|
|
434
|
+
message: opts[:message]
|
|
435
|
+
)
|
|
436
|
+
else
|
|
437
|
+
entity.errors.add(
|
|
438
|
+
attr,
|
|
439
|
+
:too_long,
|
|
440
|
+
count: opts[:in].max,
|
|
441
|
+
message: opts[:message]
|
|
442
|
+
)
|
|
443
|
+
end
|
|
444
|
+
end
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
def validate_inclusion(entity, attr, val)
|
|
448
|
+
return unless options[:inclusion]
|
|
449
|
+
|
|
450
|
+
raw_inc = options[:inclusion]
|
|
451
|
+
opts = raw_inc.is_a?(Hash) ? raw_inc : { in: raw_inc }
|
|
452
|
+
list = opts[:in]
|
|
453
|
+
entity.errors.add(
|
|
454
|
+
attr,
|
|
455
|
+
:inclusion,
|
|
456
|
+
|
|
457
|
+
message: opts[:message]
|
|
458
|
+
) if list && !list.include?(val)
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
def validate_exclusion(entity, attr, val)
|
|
462
|
+
return unless options[:exclusion]
|
|
463
|
+
|
|
464
|
+
raw_exc = options[:exclusion]
|
|
465
|
+
opts = raw_exc.is_a?(Hash) ? raw_exc : { in: raw_exc }
|
|
466
|
+
list = opts[:in]
|
|
467
|
+
entity.errors.add(
|
|
468
|
+
attr,
|
|
469
|
+
:exclusion,
|
|
470
|
+
message: opts[:message]
|
|
471
|
+
) if list&.include?(val)
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
def validate_confirmation(entity, attr, val, opts)
|
|
475
|
+
confirmed_attr = (opts[:attribute] || :"#{attr}_confirmation").to_sym
|
|
476
|
+
return unless entity.respond_to?(confirmed_attr)
|
|
477
|
+
|
|
478
|
+
confirm_val = entity.send(confirmed_attr)
|
|
479
|
+
return if confirm_val.nil?
|
|
480
|
+
|
|
481
|
+
matches = (
|
|
482
|
+
if opts[:case_sensitive] == false && val.is_a?(String)
|
|
483
|
+
val.casecmp(confirm_val.to_s).zero?
|
|
484
|
+
else
|
|
485
|
+
val == confirm_val
|
|
486
|
+
end
|
|
487
|
+
)
|
|
488
|
+
|
|
489
|
+
return if matches
|
|
490
|
+
|
|
491
|
+
entity.errors.add(
|
|
492
|
+
attr,
|
|
493
|
+
:confirmation,
|
|
494
|
+
attribute: confirmed_attr,
|
|
495
|
+
message: opts[:message] || options[:message],
|
|
496
|
+
)
|
|
497
|
+
end
|
|
498
|
+
|
|
499
|
+
def validate_numericality(entity, attr, val, opts)
|
|
500
|
+
raw = val.to_s.strip
|
|
501
|
+
num = (
|
|
502
|
+
begin
|
|
503
|
+
if opts[:only_integer]
|
|
504
|
+
Integer(raw)
|
|
505
|
+
else
|
|
506
|
+
Float(raw)
|
|
507
|
+
end
|
|
508
|
+
rescue ArgumentError, TypeError
|
|
509
|
+
nil
|
|
510
|
+
end
|
|
511
|
+
)
|
|
512
|
+
|
|
513
|
+
if num.nil?
|
|
514
|
+
entity.errors.add(
|
|
515
|
+
attr,
|
|
516
|
+
opts[:only_integer] ? :not_an_integer : :not_a_number,
|
|
517
|
+
message: opts[:message],
|
|
518
|
+
)
|
|
519
|
+
return
|
|
520
|
+
end
|
|
521
|
+
|
|
522
|
+
if opts[:greater_than] && num <= opts[:greater_than]
|
|
523
|
+
entity.errors.add(
|
|
524
|
+
attr,
|
|
525
|
+
:greater_than,
|
|
526
|
+
count: opts[:greater_than],
|
|
527
|
+
message: opts[:message],
|
|
528
|
+
)
|
|
529
|
+
end
|
|
530
|
+
if opts[:greater_than_or_equal_to] && num < opts[:greater_than_or_equal_to]
|
|
531
|
+
entity.errors.add(
|
|
532
|
+
attr,
|
|
533
|
+
:greater_than_or_equal_to,
|
|
534
|
+
count: opts[:greater_than_or_equal_to],
|
|
535
|
+
message: opts[:message],
|
|
536
|
+
)
|
|
537
|
+
end
|
|
538
|
+
if opts[:less_than] && num >= opts[:less_than]
|
|
539
|
+
entity.errors.add(
|
|
540
|
+
attr,
|
|
541
|
+
:less_than,
|
|
542
|
+
count: opts[:less_than],
|
|
543
|
+
message: opts[:message]
|
|
544
|
+
)
|
|
545
|
+
end
|
|
546
|
+
if opts[:less_than_or_equal_to] && num > opts[:less_than_or_equal_to]
|
|
547
|
+
entity.errors.add(
|
|
548
|
+
attr,
|
|
549
|
+
:less_than_or_equal_to,
|
|
550
|
+
count: opts[:less_than_or_equal_to],
|
|
551
|
+
message: opts[:message],
|
|
552
|
+
)
|
|
553
|
+
end
|
|
554
|
+
if opts[:equal_to] && num != opts[:equal_to]
|
|
555
|
+
entity.errors.add(
|
|
556
|
+
attr,
|
|
557
|
+
:equal_to,
|
|
558
|
+
count: opts[:equal_to],
|
|
559
|
+
message: opts[:message]
|
|
560
|
+
)
|
|
561
|
+
end
|
|
29
562
|
end
|
|
30
563
|
end
|
|
31
564
|
|
|
@@ -34,38 +567,229 @@ module Interfacets
|
|
|
34
567
|
@validators ||= []
|
|
35
568
|
end
|
|
36
569
|
|
|
37
|
-
def self.
|
|
38
|
-
|
|
570
|
+
def self.reject_unsupported_validator_options!(options)
|
|
571
|
+
return unless options.key?(:on)
|
|
572
|
+
|
|
573
|
+
raise(
|
|
574
|
+
ArgumentError.new(
|
|
575
|
+
"The `on:` validator option is not supported (entities have no validation contexts)"
|
|
576
|
+
)
|
|
577
|
+
)
|
|
578
|
+
end
|
|
579
|
+
|
|
580
|
+
def self.validate(*args, &block)
|
|
581
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
|
582
|
+
reject_unsupported_validator_options!(options)
|
|
583
|
+
if block
|
|
584
|
+
validators << ValidatorRule.new(nil, options, &block)
|
|
585
|
+
else
|
|
586
|
+
args.each do |method_name|
|
|
587
|
+
validators << ValidatorRule.new(nil, options) do
|
|
588
|
+
send(method_name)
|
|
589
|
+
end
|
|
590
|
+
end
|
|
591
|
+
end
|
|
592
|
+
end
|
|
593
|
+
|
|
594
|
+
def self.validates(*attributes, **options)
|
|
595
|
+
raise(
|
|
596
|
+
ArgumentError.new(
|
|
597
|
+
"At least one attribute must be specified"
|
|
598
|
+
)
|
|
599
|
+
) if attributes.empty?
|
|
39
600
|
|
|
40
|
-
|
|
601
|
+
reject_unsupported_validator_options!(options)
|
|
602
|
+
validators << ValidatorRule.new(attributes, options)
|
|
41
603
|
end
|
|
42
604
|
|
|
43
605
|
def errors
|
|
44
|
-
@errors ||= Errors.new
|
|
606
|
+
@errors ||= Errors.new(self)
|
|
607
|
+
end
|
|
608
|
+
|
|
609
|
+
def touched?(attribute)
|
|
610
|
+
@touched_attributes ||= {}
|
|
611
|
+
@touched_attributes.key?(attribute.to_sym)
|
|
612
|
+
end
|
|
613
|
+
|
|
614
|
+
def touch!(*attributes)
|
|
615
|
+
@touched_attributes ||= {}
|
|
616
|
+
validated_children.each(&:touch!) if attributes.empty?
|
|
617
|
+
if attributes.empty?
|
|
618
|
+
attrs = (
|
|
619
|
+
if self.class.respond_to?(:attributes)
|
|
620
|
+
self.class.attributes
|
|
621
|
+
elsif self.class.respond_to?(:accessors)
|
|
622
|
+
self.class.accessors
|
|
623
|
+
else
|
|
624
|
+
{}
|
|
625
|
+
end
|
|
626
|
+
)
|
|
627
|
+
attrs.each_key { |acc| @touched_attributes[acc.to_sym] = true }
|
|
628
|
+
else
|
|
629
|
+
attributes.each { |attr| @touched_attributes[attr.to_sym] = true }
|
|
630
|
+
end
|
|
631
|
+
self
|
|
632
|
+
end
|
|
633
|
+
|
|
634
|
+
def untouched!(*attributes)
|
|
635
|
+
@touched_attributes ||= {}
|
|
636
|
+
validated_children.each(&:untouched!) if attributes.empty?
|
|
637
|
+
if attributes.empty?
|
|
638
|
+
@touched_attributes.clear
|
|
639
|
+
else
|
|
640
|
+
attributes.each { |attr| @touched_attributes.delete(attr.to_sym) }
|
|
641
|
+
end
|
|
642
|
+
self
|
|
643
|
+
end
|
|
644
|
+
|
|
645
|
+
def submitted?
|
|
646
|
+
@submitted == true
|
|
647
|
+
end
|
|
648
|
+
|
|
649
|
+
def submitted!
|
|
650
|
+
@submitted = true
|
|
651
|
+
validated_children.each(&:submitted!)
|
|
652
|
+
self
|
|
653
|
+
end
|
|
654
|
+
|
|
655
|
+
def reset_submission!
|
|
656
|
+
@submitted = false
|
|
657
|
+
validated_children.each(&:reset_submission!)
|
|
658
|
+
self
|
|
45
659
|
end
|
|
46
660
|
|
|
47
661
|
def errors_if_changed(attr)
|
|
48
|
-
errors
|
|
662
|
+
errors.visible_for?(attr) ? errors[attr] : nil
|
|
49
663
|
end
|
|
50
664
|
|
|
51
665
|
def valid?
|
|
52
|
-
|
|
666
|
+
validate
|
|
667
|
+
end
|
|
668
|
+
|
|
669
|
+
# Entities of the loaded children of every `validated` association.
|
|
670
|
+
def validated_children
|
|
671
|
+
self
|
|
672
|
+
.class
|
|
673
|
+
.associations
|
|
674
|
+
.each_value
|
|
675
|
+
.select(&:validated?)
|
|
676
|
+
.flat_map do |spec|
|
|
677
|
+
handler = association(spec.name)
|
|
678
|
+
handler.collection? ? handler.get.to_a : [handler.get].compact
|
|
679
|
+
end
|
|
53
680
|
end
|
|
54
681
|
|
|
55
682
|
def validate
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
683
|
+
preserved_server_errors = (
|
|
684
|
+
if errors.respond_to?(:server_errors)
|
|
685
|
+
errors.server_errors.select do |err|
|
|
686
|
+
if respond_to?(err.attribute)
|
|
687
|
+
current_val = send(err.attribute)
|
|
688
|
+
err.value_captured? ? (err.value == current_val) : true
|
|
689
|
+
else
|
|
690
|
+
true
|
|
691
|
+
end
|
|
692
|
+
end
|
|
693
|
+
else
|
|
694
|
+
[]
|
|
695
|
+
end
|
|
696
|
+
)
|
|
697
|
+
|
|
698
|
+
errors.clear
|
|
699
|
+
|
|
700
|
+
preserved_server_errors.each do |err|
|
|
701
|
+
opts = {
|
|
702
|
+
origin: :server,
|
|
703
|
+
type: err.type,
|
|
704
|
+
}
|
|
705
|
+
opts[:value] = err.value if err.value_captured?
|
|
706
|
+
errors.add(
|
|
707
|
+
err.attribute,
|
|
708
|
+
err.message,
|
|
709
|
+
**opts,
|
|
710
|
+
)
|
|
59
711
|
end
|
|
712
|
+
|
|
713
|
+
self
|
|
714
|
+
.class
|
|
715
|
+
.validators
|
|
716
|
+
.each do |rule|
|
|
717
|
+
rule.validate(self)
|
|
718
|
+
end
|
|
719
|
+
|
|
720
|
+
children_valid = validated_children.map(&:validate).all?
|
|
721
|
+
|
|
722
|
+
errors.empty? && children_valid
|
|
60
723
|
end
|
|
61
724
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
725
|
+
def merge_record_errors(rec = nil)
|
|
726
|
+
rec ||= (respond_to?(:record) ? record : nil)
|
|
727
|
+
return if rec.nil?
|
|
728
|
+
|
|
729
|
+
if rec.respond_to?(:model) && rec.model && rec.model.respond_to?(:errors)
|
|
730
|
+
rec_errors_empty = (
|
|
731
|
+
if rec.respond_to?(:errors) && rec.errors.respond_to?(:empty?)
|
|
732
|
+
rec.errors.empty?
|
|
733
|
+
else
|
|
734
|
+
false
|
|
735
|
+
end
|
|
736
|
+
)
|
|
737
|
+
model_has_errors = (
|
|
738
|
+
rec
|
|
739
|
+
.model
|
|
740
|
+
.errors
|
|
741
|
+
.respond_to?(:empty?) ? !rec
|
|
742
|
+
.model
|
|
743
|
+
.errors
|
|
744
|
+
.empty? : true
|
|
745
|
+
)
|
|
746
|
+
if !rec.respond_to?(:errors) || rec_errors_empty || model_has_errors
|
|
747
|
+
rec = rec.model
|
|
748
|
+
end
|
|
749
|
+
end
|
|
66
750
|
|
|
67
|
-
|
|
751
|
+
return if rec.nil? || !rec.respond_to?(:errors)
|
|
752
|
+
|
|
753
|
+
rec_errors = rec.errors
|
|
754
|
+
if rec_errors.respond_to?(:each)
|
|
755
|
+
rec_errors.each do |error|
|
|
756
|
+
if error.respond_to?(:attribute) && error.respond_to?(:message)
|
|
757
|
+
opts = { origin: :server }
|
|
758
|
+
opts[:value] = send(error.attribute) if respond_to?(error.attribute)
|
|
759
|
+
errors.add(
|
|
760
|
+
error.attribute,
|
|
761
|
+
error.message,
|
|
762
|
+
**opts
|
|
763
|
+
)
|
|
764
|
+
elsif error.is_a?(Array)
|
|
765
|
+
attr, msgs = error
|
|
766
|
+
opts = { origin: :server }
|
|
767
|
+
opts[:value] = send(attr) if respond_to?(attr)
|
|
768
|
+
Array(msgs).each do |msg|
|
|
769
|
+
errors.add(attr, msg, **opts)
|
|
770
|
+
end
|
|
771
|
+
end
|
|
772
|
+
end
|
|
773
|
+
elsif rec_errors.respond_to?(:to_hash)
|
|
774
|
+
rec_errors.to_hash.each do |attr, messages|
|
|
775
|
+
opts = { origin: :server }
|
|
776
|
+
opts[:value] = send(attr) if respond_to?(attr)
|
|
777
|
+
Array(messages).each do |msg|
|
|
778
|
+
errors.add(attr, msg, **opts)
|
|
779
|
+
end
|
|
780
|
+
end
|
|
781
|
+
elsif rec_errors.respond_to?(:to_h)
|
|
782
|
+
rec_errors.to_h.each do |attr, messages|
|
|
783
|
+
opts = { origin: :server }
|
|
784
|
+
opts[:value] = send(attr) if respond_to?(attr)
|
|
785
|
+
Array(messages).each do |msg|
|
|
786
|
+
errors.add(attr, msg, **opts)
|
|
787
|
+
end
|
|
788
|
+
end
|
|
789
|
+
end
|
|
790
|
+
end
|
|
68
791
|
end
|
|
792
|
+
|
|
69
793
|
end
|
|
70
794
|
end
|
|
71
795
|
end
|