Class: RuleBasedValidator::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/rule_based_validator/validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Validator

Returns a new instance of Validator.



6
7
8
# File 'lib/rule_based_validator/validator.rb', line 6

def initialize(attributes = {})
  @attributes = attributes.to_hash&.symbolize_keys
end

Class Method Details

.parse_rules_for(action, rules) ⇒ Hash

This method analyzes the rules for a specific action and returns the modified rules.

Parameters:

  • action (Symbol)

    The action for which the rules are analyzed. Example: :create

  • rules (Hash)

    The original rules received. Example: { name: [‘on_create|required’, ‘string’] }

Returns:

  • (Hash)

    The modified rules according to the specified action. Example: { name: [‘required’, ‘string’] }



621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/rule_based_validator/validator.rb', line 621

def self.parse_rules_for(action, rules)
  rules_parsed = rules.deep_dup
  case action
  when :create
    rules.each do |attribute, rule_list|
      if rules_present?(rule_list, 'on_create')
        rules_parsed[attribute] = remove_markers(rule_list, 'on_create|')
      end
      if rules_present?(rules_parsed[attribute], 'on_update')
        rules_parsed[attribute] = remove_rules(rules_parsed[attribute], 'on_update')
      end
    end
  when :update
    rules.each do |attribute, rule_list|
      if rules_present?(rule_list, 'on_update')
        rules_parsed[attribute] = remove_markers(rule_list, 'on_update|')
      end
      if rules_present?(rules_parsed[attribute], 'on_create')
        rules_parsed[attribute] = remove_rules(rules_parsed[attribute], 'on_create')
      end
    end
  end

  return rules_parsed
end

.remove_markers(rule_list, marker) ⇒ Array

Removes a specific marker from a list of rules.

Parameters:

  • rule_list (Array)

    The list of rules to modify. Example: [‘on_create|required’, ‘string’]

  • action (String)

    Marker to remove. Example: ‘on_create’

Returns:

  • (Array)

    The list of rules without the marker. Example: [‘required’, ‘string’]



663
664
665
# File 'lib/rule_based_validator/validator.rb', line 663

def self.remove_markers(rule_list, marker) :array
  rule_list.map { |rule| rule.sub(marker, '') }
end

.remove_rules(rule_list, marker) ⇒ Array

Removes rules that contain a specific marker.

Parameters:

  • rule_list (Array)

    The list of rules to modify. Example: [‘on_create|required’, ‘string’]

  • action (String)

    Marker to search for to remove the rule. Example: ‘on_create’

Returns:

  • (Array)

    The list of rules without the rules containing the marker. Example: [‘string’]



673
674
675
# File 'lib/rule_based_validator/validator.rb', line 673

def self.remove_rules(rule_list, marker) :array
  rule_list.select {|rule| !rule.index(marker)}
end

.rules_present?(rule_list, marker) ⇒ Boolean

Checks within a list of rules if any of them contains a specific marker.

Parameters:

  • rule_list (Array)

    The list of rules to check. Example: [‘on_create|required’, ‘string’]

  • marker (String)

    Marker to search for. Example: ‘on_create’

Returns:

  • (Boolean)

    True if the rule is present, false otherwise. Example: true



653
654
655
# File 'lib/rule_based_validator/validator.rb', line 653

def self.rules_present?(rule_list, marker) :boolean
  rule_list.any? { |rule| rule.to_s.include?(marker) }
end

Instance Method Details

#after?(key, date) ⇒ Boolean

Returns:

  • (Boolean)


467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/rule_based_validator/validator.rb', line 467

def after?(key, date)
  value = @attributes[key]

   = date?(key)
  date_to_compare = date?(key, date)

  if  <= date_to_compare
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_after_date', value: value, parameter: key, after_date: date) })
  end

  return true
end

#after_or_equal?(key, date) ⇒ Boolean

Returns:

  • (Boolean)


493
494
495
496
497
498
499
500
501
502
503
# File 'lib/rule_based_validator/validator.rb', line 493

def after_or_equal?(key, date)
  value = @attributes[key]

   = date?(key)
  date_to_compare = date?(key, date)

  if  < date_to_compare
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_after_or_equal', value: value, parameter: key, after_or_equal: date) })
  end
  return true
end

#after_than_field?(key, key2) ⇒ Boolean

Returns:

  • (Boolean)


480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/rule_based_validator/validator.rb', line 480

def after_than_field?(key, key2)
  value = @attributes[key]

   = date?(key)
  date_to_compare = date?(key2.to_sym)

  if  <= date_to_compare
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_after_than_field', value: value, parameter: key, after_than_field: key2) })
  end

  return true
end

#array?(key, type = nil) ⇒ Boolean

Checks if a parameter is an array.

This method verifies if a given parameter is an array by checking its type.

Parameters:

  • key (Symbol)

    The name of the parameter to check.

  • type (String) (defaults to: nil)

    The type of the elements of the array.

Returns:

  • (Boolean)

    ‘true` if the parameter is an array, `false` otherwise.

Raises:



209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/rule_based_validator/validator.rb', line 209

def array?(key, type = nil)
  value = @attributes[key]
  
  unless value.instance_of?(Array)
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_array', parameter: key, value: value) })
  end

  unless type.nil?
    value.each_with_index do |element, index|
      send(type + '?', key, element)
    end
  end
  return true
end

#before?(key, date) ⇒ Boolean

Returns:

  • (Boolean)


428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/rule_based_validator/validator.rb', line 428

def before?(key, date)
  value = @attributes[key]

   = date?(key)
  date_to_compare = date?(key, date)

  if  >= date_to_compare
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_before_date', value: value, parameter: key, before_date: date) })
  end

  return true
end

#before_or_equal?(key, date) ⇒ Boolean

Returns:

  • (Boolean)


454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/rule_based_validator/validator.rb', line 454

def before_or_equal?(key, date)
  value = @attributes[key]

   = date?(key)
  date_to_compare = date?(key, date)

  if  > date_to_compare
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_before_or_equal', value: value, parameter: key, before_or_equal: date) })
  end

  return true
end

#before_than_field?(key, key2) ⇒ Boolean

Returns:

  • (Boolean)


441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/rule_based_validator/validator.rb', line 441

def before_than_field?(key, key2)
  value = @attributes[key]

   = date?(key)
  date_to_compare = date?(key2.to_sym)

  if  >= date_to_compare
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_before_than_field', value: value, parameter: key, before_than_field: key2) })
  end

  return true
end

#between?(key, min, max) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/rule_based_validator/validator.rb', line 344

def between?(key, min, max)
  value = @attributes[key]

  value = Float(value) rescue value
  min = Float(min) rescue min
  max = Float(max) rescue max

  if value.class != min.class || value.class != max.class
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_both_same_type') })
  end

  raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_between', value: value, parameter: key, min: min, max: max) }) if !value.between?(min, max)

  return true
end

#boolean?(key, value = nil) ⇒ Boolean

Checks if a parameter is a boolean.

This method verifies if a given parameter is a boolean by checking its type or if it’s a string containing ‘true’ or ‘false’.

Parameters:

  • key (Symbol)

    The name of the parameter to check.

  • value (String) (defaults to: nil)

    This is used to verify elements of another element. (Array, Hash)

Returns:

  • (Boolean)

    ‘true` if the parameter is a boolean, `false` otherwise.

Raises:



191
192
193
194
195
196
197
198
199
# File 'lib/rule_based_validator/validator.rb', line 191

def boolean?(key, value = nil)
  value = @attributes[key] if value.nil?

  unless [true, false, "true", "false", 1, 0, '1', '0', ].include?(value)
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_boolean', parameter: key, value: value) })
  end

  return true
end

#date?(key, value = nil) ⇒ Boolean

Returns:

  • (Boolean)


360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/rule_based_validator/validator.rb', line 360

def date?(key, value = nil)
  if value.nil?
    value = @attributes[key]
  end
  if (Date.parse(value) rescue false)
    return Date.parse(value)
  elsif (Date.strptime(value, '%d-%m-%Y') rescue false)
    return Date.strptime(value, '%d-%m-%Y')
  elsif value.instance_of? Date
    return value
  else
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_date', value: value, parameter: key) })
  end

  return true
end

#date_format?(key, format) ⇒ Boolean

Returns:

  • (Boolean)


377
378
379
380
381
382
383
384
385
386
387
# File 'lib/rule_based_validator/validator.rb', line 377

def date_format?(key, format)
  value = @attributes[key]

  if (Date.strptime(value, format) rescue false)
    return Date.strptime(value, format)
  else
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_date_format', value: value, parameter: key, date_format: format) })
  end

  return true
end

#email?(key) ⇒ Boolean

Returns:

  • (Boolean)


532
533
534
535
536
537
538
539
540
541
542
# File 'lib/rule_based_validator/validator.rb', line 532

def email?(key)
  value = @attributes[key]

  value = string?(key, value)

  unless value =~ /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_email', parameter: key, value: value) })
  end

  return true
end

#exists?(key, field, model) ⇒ Boolean

Returns:

  • (Boolean)


606
607
608
609
610
611
612
# File 'lib/rule_based_validator/validator.rb', line 606

def exists?(key, field, model)
  value = @attributes[key]
  if model.constantize.where(field.to_sym => value).count == 0
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.exists', parameter: key, value: value) })
  end
  return true
end

#filter_required_if(rules) ⇒ Object



505
506
507
# File 'lib/rule_based_validator/validator.rb', line 505

def filter_required_if(rules)
  rules.find { |r| r.include?('required_if') }
end

#find_rule(rules, rule) ⇒ Object



84
85
86
# File 'lib/rule_based_validator/validator.rb', line 84

def find_rule(rules, rule)
  rules.find { |r| r.include?(rule) }
end

#float?(key, value = nil) ⇒ Boolean

Checks if a parameter is a float.

This method verifies if a given parameter is a float by checking its type or if it’s a string containing a float.

Parameters:

  • key (Symbol)

    The name of the parameter to check.

  • value (String) (defaults to: nil)

    This is used to verify elements of another element. (Array, Hash)

Returns:

  • (Boolean)

    ‘true` if the parameter is a float, `false` otherwise.

Raises:



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rule_based_validator/validator.rb', line 155

def float?(key, value = nil)
  value = @attributes[key] if value.nil?

  value = value.to_s unless value.is_a?(String)

  if value =~ /\A-?\d+\.\d+\z/
    true
  else
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_float', parameter: key, value: value) })
  end

  true
end

#gt?(key, comparator) ⇒ Boolean

Returns:

  • (Boolean)


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/rule_based_validator/validator.rb', line 238

def gt?(key, comparator)
  value = @attributes[key]

  value = Float(value) rescue value

  if comparator !~ /\A\d+(\.\d+)?\z/
    comparator_value = @attributes[comparator.to_sym]
    comparator = Float(comparator_value) rescue comparator_value
  else
    comparator = Float(comparator)
  end

  if value.class != comparator.class
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_both_same_type') })
  elsif (value.is_a?(Hash) || value.is_a?(Array)) && (comparator.is_a?(Float))
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_greater_than', parameter: key, value: value, greater_than: comparator) }) unless value.count > comparator
  elsif (value.is_a?(Hash) || value.is_a?(Array)) && (comparator.is_a?(Hash) || comparator.is_a?(Array))
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_greater_than', parameter: key, value: value, greater_than: comparator) }) unless value.count > comparator.count
  else
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_greater_than', parameter: key, value: value, greater_than: comparator) }) unless value > comparator
  end

  true
end

#gte?(key, comparator) ⇒ Boolean

Returns:

  • (Boolean)


263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/rule_based_validator/validator.rb', line 263

def gte?(key, comparator)
  value = @attributes[key]

  value = Float(value) rescue value

  if comparator !~ /\A\d+(\.\d+)?\z/
    comparator_value = @attributes[comparator.to_sym]
    comparator = Float(comparator_value) rescue comparator_value
  else
    comparator = Float(comparator)
  end

  if value.class != comparator.class
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_both_same_type') })
  elsif (value.is_a?(Hash) || value.is_a?(Array)) && (comparator.is_a?(Float))
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_greater_than_or_equal_to', parameter: key, value: value, greater_than_or_equal_to: comparator) }) unless value.count >= comparator
  elsif (value.is_a?(Hash) || value.is_a?(Array)) && (comparator.is_a?(Hash) || comparator.is_a?(Array))
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_greater_than_or_equal_to', parameter: key, value: value, greater_than_or_equal_to: comparator) }) unless value.count >= comparator.count
  else
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_greater_than_or_equal_to', parameter: key, value: value, greater_than_or_equal_to: comparator) }) unless value >= comparator
  end

  true
end

#hash?(key, type = nil) ⇒ Boolean

Checks if the value of the specified key is a hash.

Parameters:

  • key (Symbol)

    The key to check in the attributes hash.

  • type (Class) (defaults to: nil)

    (optional) The expected class of the value.

Returns:

  • (Boolean)

    Returns true if the value is a hash.

Raises:



230
231
232
233
234
235
236
# File 'lib/rule_based_validator/validator.rb', line 230

def hash?(key, type = nil)
  value = @attributes[key]
  if (!value.instance_of? Hash)
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_hash', parameter: key, value: value) })
  end
  return true
end

#integer?(key, value = nil) ⇒ Boolean

Checks if a parameter is an integer.

This method verifies if a given parameter is an integer by checking its type or if it’s a string containing only digits.

Parameters:

  • key (Symbol)

    The name of the parameter to check.

  • value (String) (defaults to: nil)

    This is used to verify elements of another element. (Array, Hash)

Returns:

  • (Boolean)

    ‘true` if the parameter is an integer, `false` otherwise.

Raises:



137
138
139
140
141
142
143
144
145
# File 'lib/rule_based_validator/validator.rb', line 137

def integer?(key, value = nil)
  value = @attributes[key] if value.nil?

  unless value.is_a?(Integer) || (value.is_a?(String) && value =~ /\A\d+\z/)
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_integer', parameter: key, value: value) })
  end

  true
end

#lt?(key, comparator) ⇒ Boolean

Checks if the value of a given key is less than a comparator.

Parameters:

  • key (Symbol)

    The key of the attribute to compare.

  • comparator (Object)

    The value or attribute to compare against.

Returns:

  • (Boolean)

    Returns true if the value is less than the comparator, otherwise raises an InvalidParametersException.

Raises:

  • (InvalidParametersException)

    Raises an exception if the value is not less than the comparator or if the types are not compatible.



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/rule_based_validator/validator.rb', line 294

def lt?(key, comparator)
  value = @attributes[key]

  value = Float(value) rescue value

  if comparator !~ /\A\d+(\.\d+)?\z/
    comparator_value = @attributes[comparator.to_sym]
    comparator = Float(comparator_value) rescue comparator_value
  else
    comparator = Float(comparator)
  end

  if value.class != comparator.class
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_both_same_type') })
  elsif (value.is_a?(Hash) || value.is_a?(Array)) && (comparator.is_a?(Float))
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_less_than', parameter: key, value: value, less_than: comparator) }) unless value.count < comparator
  elsif (value.is_a?(Hash) || value.is_a?(Array)) && (comparator.is_a?(Hash) || comparator.is_a?(Array))
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_less_than', parameter: key, value: value, less_than: comparator) }) unless value.count < comparator.count
  else
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_less_than', parameter: key, value: value, less_than: comparator) }) unless value < comparator
  end

  true
end

#lte?(key, comparator) ⇒ Boolean

Returns:

  • (Boolean)


319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/rule_based_validator/validator.rb', line 319

def lte?(key, comparator)
  value = @attributes[key]

  value = Float(value) rescue value

  if comparator !~ /\A\d+(\.\d+)?\z/
    comparator_value = @attributes[comparator.to_sym]
    comparator = Float(comparator_value) rescue comparator_value
  else
    comparator = Float(comparator)
  end

  if value.class != comparator.class
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_both_same_type') })
  elsif (value.is_a?(Hash) || value.is_a?(Array)) && (comparator.is_a?(Float))
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_less_than_or_equal_to', parameter: key, value: value, less_than_or_equal_to: comparator) }) unless value.count <= comparator
  elsif (value.is_a?(Hash) || value.is_a?(Array)) && (comparator.is_a?(Hash) || comparator.is_a?(Array))
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_less_than_or_equal_to', parameter: key, value: value, less_than_or_equal_to: comparator) }) unless value.count <= comparator.count
  else
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_less_than_or_equal_to', parameter: key, value: value, less_than_or_equal_to: comparator) }) unless value <= comparator
  end

  true
end

#max_digits?(key, max_digits) ⇒ Boolean

Returns:

  • (Boolean)


544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/rule_based_validator/validator.rb', line 544

def max_digits?(key, max_digits)
  value = @attributes[key]

  unless value.is_a?(Numeric)
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_numeric', parameter: key, value: value) })
  end

  unless value.to_s.length <= max_digits.to_i
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.max_digits_exceeded', parameter: key, value: value, max_digits: max_digits) })
  end

  return true
end

#min_digits?(key, min_digits) ⇒ Boolean

Returns:

  • (Boolean)


558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/rule_based_validator/validator.rb', line 558

def min_digits?(key, min_digits)
  value = @attributes[key]

  unless value.is_a?(Numeric)
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_numeric', parameter: key, value: value) })
  end

  unless value.to_s.length >= min_digits.to_i
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.min_digits_not_reached', parameter: key, value: value, min_digits: min_digits) })
  end

  return true
end

#nullable?(key) ⇒ Boolean

Checks if a parameter is nullable.

This method verifies if a given parameter is nullable by checking if it is not present in the attributes hash.

Parameters:

  • key (Symbol)

    The name of the parameter to check.

Returns:

  • (Boolean)

    ‘true` if the parameter is nullable (not present), `false` otherwise.



107
108
109
# File 'lib/rule_based_validator/validator.rb', line 107

def nullable?(key)
  !@attributes[key].present?
end

#numeric?(key, value = nil) ⇒ Boolean

Checks if a parameter is a numeric.

This method verifies if a given parameter is a numeric by checking its type or if it’s a string containing a numeric.

Parameters:

  • key (Symbol)

    The name of the parameter to check.

  • value (String) (defaults to: nil)

    This is used to verify elements of another element. (Array, Hash)

Returns:

  • (Boolean)

    ‘true` if the parameter is a numeric, `false` otherwise.

Raises:



177
178
179
180
181
# File 'lib/rule_based_validator/validator.rb', line 177

def numeric?(key, value = nil)
  value = @attributes[key] if value.nil?

  !Float(value).nil? rescue raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_numeric', parameter: key, value: value) })
end

#one_of?(key, array) ⇒ Boolean

Returns:

  • (Boolean)


572
573
574
575
576
577
578
579
580
# File 'lib/rule_based_validator/validator.rb', line 572

def one_of?(key, array)
  value = @attributes[key]

  array_parsed = JSON.parse(array).map(&:to_s)
  if !array_parsed.include? value.to_s
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.one_of', parameter: key, value: value, values: array) })
  end
  return true
end

#regex_required?(key, date) ⇒ Boolean

Returns:

  • (Boolean)


389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/rule_based_validator/validator.rb', line 389

def regex_required?(key, date)
  value = @attributes[key]

   = date?(key)
  date_to_compare = date?(key, date)

  if  >= date_to_compare
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_regex_required_date', value: value, parameter: key, regex_required_date: date) })
  end

  return true
end

#regex_required_or_equal?(key, date) ⇒ Boolean

Returns:

  • (Boolean)


415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/rule_based_validator/validator.rb', line 415

def regex_required_or_equal?(key, date)
  value = @attributes[key]

   = date?(key)
  date_to_compare = date?(key, date)

  if  > date_to_compare
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_regex_required_or_equal', value: value, parameter: key, regex_required_or_equal: date) })
  end

  return true
end

#regex_required_than_field?(key, key2) ⇒ Boolean

Returns:

  • (Boolean)


402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/rule_based_validator/validator.rb', line 402

def regex_required_than_field?(key, key2)
  value = @attributes[key]

   = date?(key)
  date_to_compare = date?(key2.to_sym)

  if  >= date_to_compare
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_regex_required_than_field', value: value, parameter: key, regex_required_than_field: key2) })
  end

  return true
end

#required?(key) ⇒ Boolean

Checks if a parameter is required.

Parameters:

  • key (Symbol)

    The name of the parameter to check.

Returns:

  • (Boolean)

    ‘true` if the parameter is present.

Raises:



93
94
95
96
97
98
99
# File 'lib/rule_based_validator/validator.rb', line 93

def required?(key)
  unless @attributes[key].present?
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.required', parameter: key) })
  end

  true
end

#required_if?(key, field, value = nil) ⇒ Boolean

Returns:

  • (Boolean)


513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/rule_based_validator/validator.rb', line 513

def required_if?(key, field, value = nil)
  if @attributes[key].nil?
    if field == :true
      required?(key) 
      return true
    elsif field.to_s.include?(',')
      key_to_compare, value_to_compare = field.to_s.split(',')

      raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.required_if', parameter: key, required_if: field) }) if @attributes[key_to_compare.to_sym] != value_to_compare
      
    elsif !@attributes[field].nil?
      raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.required_if', parameter: key, required_if: field) })
    else
      return true
    end
  end
  return false
end

#required_if_present?(rules) ⇒ Boolean

Returns:

  • (Boolean)


509
510
511
# File 'lib/rule_based_validator/validator.rb', line 509

def required_if_present?(rules)
  filter_required_if(rules).present?
end

#rule_exists?(rules, rule) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/rule_based_validator/validator.rb', line 80

def rule_exists?(rules, rule)
  rules.any? { |element| element.include?(rule) }
end

#string?(key, value = nil) ⇒ Boolean

Checks if a parameter is a string.

This method verifies if a given parameter is a string by checking its type.

Parameters:

  • key (Symbol)

    The name of the parameter to check.

  • value (String) (defaults to: nil)

    This is used to verify elements of another element. (Array, Hash)

Returns:

  • (Boolean)

    ‘true` if the parameter is a string, `false` otherwise.

Raises:



119
120
121
122
123
124
125
126
127
# File 'lib/rule_based_validator/validator.rb', line 119

def string?(key, value = nil)
  value = @attributes[key] if value.nil?

  unless value.instance_of?(String)
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.must_be_string', parameter: key, value: value) })
  end

  value
end

#subset_of?(key, array) ⇒ Boolean

Returns:

  • (Boolean)


582
583
584
585
586
587
588
589
590
591
592
# File 'lib/rule_based_validator/validator.rb', line 582

def subset_of?(key, array)
  array?(key)
  value = @attributes[key]
  array_parsed = JSON.parse(array)

  value.each do |element|
    if !array_parsed.include? element
      raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.subset_of', parameter: key, value: value, values: array) })
    end
  end
end

#uniqueness?(key, model) ⇒ Boolean

Returns:

  • (Boolean)


594
595
596
597
598
599
600
601
602
603
604
# File 'lib/rule_based_validator/validator.rb', line 594

def uniqueness?(key, model)
  value = @attributes[key]
  
  duplicated_instances = model.constantize.where(key => value)
  duplicated_instances = duplicated_instances.where.not(key => @instance[key]) if @instance.present?

  if duplicated_instances.count > 0
    raise InvalidParametersException.new({ key => I18n.t('errors.messages.invalid_parameter.uniqueness', parameter: key, value: value) })
  end
  return true
end

#validate_attributes_by_rules(rules_formatted) ⇒ Object



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
# File 'lib/rule_based_validator/validator.rb', line 34

def validate_attributes_by_rules(rules_formatted)
  rules_formatted.each do |rule|
    if (@attributes.keys.include? rule[0]) || (rule[1].include? 'required') || required_if_present?(rule[1])
      if (rule[1].include? 'nullable')
        next if send('nullable?', rule[0])
      end
      if required_if_present?(rule[1])
        rule_parsed = filter_required_if(rule[1]).split(':')
        next if send('required_if?', rule[0], rule_parsed[1].to_sym)
      end
      if (rule[1].include? 'required')
        send('required?', rule[0])
      end

      rule[1].each do |nested_rules|
        if nested_rules.instance_of? Hash
          if @attributes[rule[0]].instance_of? Array
            @attributes[rule[0]].each do |element|
              RuleBasedValidator::Validator.new(element).verify(nested_rules)
            end
          elsif @attributes[rule[0]].instance_of? Hash
            RuleBasedValidator::Validator.new(@attributes[rule[0]]).verify(nested_rules)
          else
            raise Exceptions::Api::InvalidParametersException.new({ key => 'El campo ' + rule[0].to_s + ' debe ser un objeto o un arreglo de objetos' })
          end
        else
          if !nested_rules.index(':')
            send(nested_rules + '?', rule[0])
          elsif !nested_rules.index(',')
            rule_parsed = nested_rules.split(':')
            send(rule_parsed[0] + '?', rule[0], rule_parsed[1])
          elsif !nested_rules.index('[')
            rule_parsed = nested_rules.split(':')
            comparators_parsed = rule_parsed[1].split(',')
            send(rule_parsed[0] + '?', rule[0], comparators_parsed[0], comparators_parsed[1])
          else
            rule_parsed = nested_rules.split(':')
            send(rule_parsed[0] + '?', rule[0], rule_parsed[1])
          end
        end
      end
    end
  end
  {}
end

#verify(rules, opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rule_based_validator/validator.rb', line 10

def verify(rules, opts = {})
  @instance = opts[:instance]

  raise InvalidRulesException.new if rules.empty?

  rules_formatted = if @instance.nil? || @instance.id.nil?
    RuleBasedValidator::Validator.parse_rules_for(:create, rules)
  else
    RuleBasedValidator::Validator.parse_rules_for(:update, rules)
  end

  errors = rules_formatted.map do |rule_formatted|
    begin
      validate_attributes_by_rules({ rule_formatted.first => rule_formatted.second })
    rescue InvalidParametersException => exception
      exception.body[:errors]
    end
  end

  errors = errors.reduce(:merge)

  raise InvalidParametersException.new(errors) unless errors.empty?
end