Skip to content

Commit

Permalink
add filter validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tycol7 committed Nov 6, 2024
1 parent 922332e commit 9ca7e3e
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def index
poa_codes = form_attributes['poaCodes']
page_size = form_attributes['pageSize']
page_index = form_attributes['pageIndex']
filter = form_attributes['filter']

unless poa_codes.is_a?(Array) && poa_codes.size.positive?
raise ::Common::Exceptions::ParameterMissing.new('poaCodes',
Expand All @@ -26,6 +27,8 @@ def index
detail: 'pageSize is required when pageIndex is present')
end

validate_filter!(filter)

service = ManageRepresentativeService.new(external_uid: 'power_of_attorney_request_uid',
external_key: 'power_of_attorney_request_key')

Expand Down Expand Up @@ -130,6 +133,40 @@ def build_bgs_attributes(form_attributes)
bgs_form_attributes
end

def validate_filter!(filter)
return nil if filter.blank?

valid_filters = %w[status state city country sensitivityLevel]

invalid_filters = filter.keys - valid_filters

if invalid_filters.any?
raise ::Common::Exceptions::UnprocessableEntity.new(
detail: "Invalid filter(s): #{invalid_filters.join(', ')}"
)
end

validate_statuses!(filter['status'])
end

def validate_statuses!(statuses)
return nil if statuses.blank?

unless statuses.is_a?(Array)
raise ::Common::Exceptions::UnprocessableEntity.new(
detail: 'filter status must be an array'
)
end

valid_statuses = %w[new accepted declined]

if statuses.any? { |status| valid_statuses.exclude?(status.downcase) }
raise ::Common::Exceptions::UnprocessableEntity.new(
detail: "Status(es) must be one of: #{valid_statuses.join(', ')}"
)
end
end

def normalize(item)
item.to_s.strip.downcase
end
Expand Down

0 comments on commit 9ca7e3e

Please sign in to comment.