forked from Apipie/apipie-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.rb
541 lines (438 loc) · 13.2 KB
/
validator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# -*- coding: utf-8 -*-
module Apipie
module Validator
# to create new validator, inherit from Apipie::Validator::BaseValidator
# and implement class method build and instance method validate
class BaseValidator
attr_accessor :param_description
def initialize(param_description)
@param_description = param_description
end
def inspected_fields
[:param_description]
end
def inspect
string = "#<#{self.class.name}:#{self.object_id} "
fields = inspected_fields.map {|field| "#{field}: #{self.send(field)}"}
string << fields.join(", ") << ">"
end
def self.inherited(subclass)
@validators ||= []
@validators.insert 0, subclass
end
# find the right validator for given options
def self.find(param_description, argument, options, block)
@validators.each do |validator_type|
validator = validator_type.build(param_description, argument, options, block)
return validator if validator
end
return nil
end
# check if value is valid
def valid?(value)
if self.validate(value)
@error_value = nil
true
else
@error_value = value
false
end
end
def param_name
@param_description.name
end
# validator description
def description
"TODO: validator description"
end
def format_description_value(value)
"<code>#{CGI::escapeHTML(value.to_s)}</code>"
end
def error
ParamInvalid.new(param_name, @error_value, description)
end
def to_s
self.description
end
def to_json
self.description
end
# what type is expected, mostly string
# this information is used in cli client
# thor supported types :string, :hash, :array, :numeric, or :boolean
def expected_type
'string'
end
def ignore_allow_blank?
false
end
def merge_with(other_validator)
return self if self == other_validator
raise NotImplementedError, "Don't know how to merge #{self.inspect} with #{other_validator.inspect}"
end
def params_ordered
nil
end
def ==(other)
return false unless self.class == other.class
if param_description == other.param_description
true
else
false
end
end
end
# validate arguments type
class TypeValidator < BaseValidator
def initialize(param_description, argument)
super(param_description)
@type = argument
end
def validate(value)
return false if value.nil?
value.is_a? @type
end
def self.build(param_description, argument, options, block)
if argument.is_a?(Class) && (argument != Hash || block.nil?)
self.new(param_description, argument)
end
end
def description
"Must be a #{@type}"
end
def expected_type
if @type.ancestors.include? Hash
'hash'
elsif @type.ancestors.include? Array
'array'
elsif @type.ancestors.include? Numeric
'numeric'
elsif @type.ancestors.include? File
'file'
else
'string'
end
end
end
# validate arguments value with regular expression
class RegexpValidator < BaseValidator
def initialize(param_description, argument)
super(param_description)
@regexp = argument
end
def validate(value)
value =~ @regexp
end
def self.build(param_description, argument, options, proc)
self.new(param_description, argument) if argument.is_a? Regexp
end
def description
"Must match regular expression #{format_description_value("/#{@regexp.source}/")}."
end
end
# arguments value must be one of given in array
class EnumValidator < BaseValidator
def initialize(param_description, argument)
super(param_description)
@array = argument
end
def validate(value)
@array.include?(value)
end
def self.build(param_description, argument, options, proc)
self.new(param_description, argument) if argument.is_a?(Array)
end
def values
@array
end
def description
string = @array.map { |value| format_description_value(value) }.join(', ')
"Must be one of: #{string}."
end
end
# arguments value must be an array
class ArrayValidator < Apipie::Validator::BaseValidator
def initialize(param_description, argument, options={})
super(param_description)
@type = argument
@items_type = options[:of]
@items_enum = options[:in]
end
def validate(values)
return false unless process_value(values).respond_to?(:each) && !process_value(values).is_a?(String)
process_value(values).all? { |v| validate_item(v)}
end
def process_value(values)
values || []
end
def description
"Must be an array of #{items}"
end
def expected_type
"array"
end
def self.build(param_description, argument, options, block)
if argument == Array && !block.is_a?(Proc)
self.new(param_description, argument, options)
end
end
private
def enum
if @items_enum.kind_of?(Proc)
@items_enum = Array(@items_enum.call)
end
@items_enum
end
def validate_item(value)
has_valid_type?(value) &&
is_valid_value?(value)
end
def has_valid_type?(value)
if @items_type
item_validator = BaseValidator.find('', @items_type, nil, nil)
if item_validator
item_validator.valid?(value)
else
value.kind_of?(@items_type)
end
else
true
end
end
def is_valid_value?(value)
if enum
enum.include?(value)
else
true
end
end
def items
unless enum
@items_type || "any type"
else
enum.inspect
end
end
end
class ArrayClassValidator < BaseValidator
def initialize(param_description, argument)
super(param_description)
@array = argument
end
def validate(value)
@array.include?(value.class)
end
def self.build(param_description, argument, options, block)
if argument.is_a?(Array) && argument.first.class == Class && !block.is_a?(Proc)
self.new(param_description, argument)
end
end
def description
string = @array.map { |value| format_description_value(value) }.join(', ')
"Must be one of: #{string}."
end
end
class ProcValidator < BaseValidator
def initialize(param_description, argument)
super(param_description)
@proc = argument
end
def validate(value)
(@help = @proc.call(value)) === true
end
def self.build(param_description, argument, options, proc)
self.new(param_description, argument) if argument.is_a?(Proc) && argument.arity == 1
end
def error
ParamInvalid.new(param_name, @error_value, @help)
end
def description
""
end
end
class HashValidator < BaseValidator
include Apipie::DSL::Base
include Apipie::DSL::Param
def self.build(param_description, argument, options, block)
self.new(param_description, block, options[:param_group]) if block.is_a?(Proc) && block.arity <= 0 && argument == Hash
end
def initialize(param_description, argument, param_group)
super(param_description)
@proc = argument
@param_group = param_group
self.instance_exec(&@proc)
# specifying action_aware on Hash influences the child params,
# not the hash param itself: assuming it's required when
# updating as well
if param_description.options[:action_aware] && param_description.options[:required]
param_description.required = true
end
prepare_hash_params
end
def params_ordered
@params_ordered ||= _apipie_dsl_data[:params].map do |args|
options = args.find { |arg| arg.is_a? Hash }
options[:parent] = self.param_description
options[:param_group] = @param_group
Apipie::ParamDescription.from_dsl_data(param_description.method_description, args)
end
end
def validate(value)
return false if !value.is_a? Hash
if @hash_params
@hash_params.each do |k, p|
if Apipie.configuration.validate_presence?
raise ParamMissing.new(p) if p.required && !value.has_key?(k)
end
if Apipie.configuration.validate_value?
p.validate(value[k]) if value.has_key?(k)
end
end
end
return true
end
def process_value(value)
if @hash_params && value
return @hash_params.each_with_object({}) do |(key, param), api_params|
if value.has_key?(key)
api_params[param.as] = param.process_value(value[key])
end
end
end
end
def description
"Must be a Hash"
end
def expected_type
'hash'
end
# where the group definition should be looked up when no scope
# given. This is expected to return a controller.
def _default_param_group_scope
@param_group && @param_group[:scope]
end
def merge_with(other_validator)
if other_validator.is_a? HashValidator
@params_ordered = ParamDescription.unify(self.params_ordered + other_validator.params_ordered)
prepare_hash_params
else
super
end
end
def prepare_hash_params
@hash_params = params_ordered.reduce({}) do |h, param|
h.update(param.name.to_sym => param)
end
end
end
# special type of validator: we say that it's not specified
class UndefValidator < BaseValidator
def validate(value)
true
end
def self.build(param_description, argument, options, block)
if argument == :undef
self.new(param_description)
end
end
def description
nil
end
end
class DecimalValidator < BaseValidator
def validate(value)
self.class.validate(value)
end
def self.build(param_description, argument, options, block)
if argument == :decimal
self.new(param_description)
end
end
def description
"Must be a decimal number."
end
def expected_type
'numeric'
end
def self.validate(value)
value.to_s =~ /\A^[-+]?[0-9]+([,.][0-9]+)?\Z$/
end
end
class NumberValidator < BaseValidator
def validate(value)
self.class.validate(value)
end
def self.build(param_description, argument, options, block)
if argument == :number
self.new(param_description)
end
end
def description
"Must be a number."
end
def expected_type
'numeric'
end
def self.validate(value)
value.to_s =~ /\A(0|[1-9]\d*)\Z$/
end
end
class BooleanValidator < BaseValidator
def validate(value)
%w[true false 1 0].include?(value.to_s)
end
def self.build(param_description, argument, options, block)
if argument == :bool || argument == :boolean
self.new(param_description)
end
end
def expected_type
'boolean'
end
def description
string = %w(true false 1 0).map { |value| format_description_value(value) }.join(', ')
"Must be one of: #{string}."
end
def ignore_allow_blank?
true
end
end
class NestedValidator < BaseValidator
def initialize(param_description, argument, param_group)
super(param_description)
@validator = Apipie::Validator:: HashValidator.new(param_description, argument, param_group)
@type = argument
end
def validate(value)
value ||= [] # Rails convert empty array to nil
return false if value.class != Array
value.each do |child|
return false unless @validator.validate(child)
end
true
end
def process_value(value)
value ||= [] # Rails convert empty array to nil
@values = []
value.each do |child|
@values << @validator.process_value(child)
end
@values
end
def self.build(param_description, argument, options, block)
# in Ruby 1.8.x the arity on block without args is -1
# while in Ruby 1.9+ it is 0
self.new(param_description, block, options[:param_group]) if block.is_a?(Proc) && block.arity <= 0 && argument == Array
end
def expected_type
'array'
end
def description
"Must be an Array of nested elements"
end
def params_ordered
@validator.params_ordered
end
end
end
end