Class: ReeValidator::ValidatePaging

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_validator/package/ree_validator/functions/validate_paging.rb

Constant Summary collapse

PagingErr =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#call(page:, per_page:, min_per_page:, max_per_page:, error: nil, **opts) ⇒ Object



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
# File 'lib/ree_lib/packages/ree_validator/package/ree_validator/functions/validate_paging.rb', line 24

def call(page:, per_page:, min_per_page:, max_per_page:, error: nil, **opts)
  if page < 1
    error ||= PagingErr.new(
      t('validator.paging.min_page', default_by_locale: :en)
    )

    raise error
  end

  if per_page < min_per_page
    error ||= PagingErr.new(
      t(
        'validator.paging.min_per_page',
        {min_per_page: min_per_page},
        default_by_locale: :en
      )
    )

    raise error
  end

  if per_page > max_per_page
    error ||= PagingErr.new(
      t(
        'validator.paging.max_per_page',
        {max_per_page: max_per_page},
        default_by_locale: :en
      )
    )

    raise error
  end

  max_result_window = opts[:max_result_window]

  if max_result_window && page * per_page > max_result_window
    error ||= PagingErr.new(
      t(
        'validator.paging.max_result_window',
        {max_result_window: max_result_window},
        default_by_locale: :en
      )
    )

    raise error
  end

  true
end