Skip to content

Commit

Permalink
fix params validation
Browse files Browse the repository at this point in the history
  • Loading branch information
omohokcoj committed Jan 21, 2024
1 parent fdc3d77 commit a3d351f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 4 additions & 2 deletions lib/params/base_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def self.call(...)
raise e unless validator.dry_run?
rescue StandardError => e
Rollbar.error(e) if defined?(Rollbar)

raise e unless Rails.env.production?
end

attr_reader :params, :dry_run
Expand Down Expand Up @@ -50,9 +52,9 @@ def type(params, key, type, message: nil)
return if params.blank?
return unless params.key?(key)

return if params[key].is_a?(type)
return if params[key].is_a?(type) || (type == Hash && params[key].is_a?(ActionController::Parameters))

type = 'Object' if type == 'Hash'
type = 'Object' if type == Hash

raise_error(message || "#{key} must be a #{type}")
end
Expand Down
12 changes: 7 additions & 5 deletions lib/params/submission_create_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ def validate_creation_from_submission(params)
required(params, :template_id)
required(params, %i[submission submissions])

in_path(params, :submission) do |submission_params|
required(submission_params, :submitters) if params[:submission]
end

boolean(params, :send_email)
boolean(params, :send_sms)
type(params, :order, String)
Expand All @@ -53,8 +49,14 @@ def validate_creation_from_submission(params)

value_in(params, :order, %w[preserved random], allow_nil: true)

return true if params[:submission].is_a?(Array)

in_path(params, :submission) do |submission_params|
required(submission_params, :submitters) if params[:submission]
end

in_path_each(params, %i[submission submitters]) do |submitter_params|
required(submitter_params, :email)
required(submitter_params, %i[email phone name])

type(submitter_params, :name, String)
type(submitter_params, :email, String)
Expand Down

0 comments on commit a3d351f

Please sign in to comment.