Skip to content

Instantly share code, notes, and snippets.

@mariusz-blaszczak
Created February 4, 2021 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariusz-blaszczak/8d5811ad568e4ddb1e6fd79f9ce9d0c0 to your computer and use it in GitHub Desktop.
Save mariusz-blaszczak/8d5811ad568e4ddb1e6fd79f9ce9d0c0 to your computer and use it in GitHub Desktop.
Recreate issue with dry-validation
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", "6.1.0"
gem 'dry-validation'
gem "pry"
gem 'pry-doc', require: false
gem 'pry-nav'
gem 'pry-rails', require: false
end
require "action_controller/railtie"
class TestApp < Rails::Application
config.root = __dir__
config.hosts << "example.org"
secrets.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
routes.draw do
post "fax", to: "test#create"
end
end
class TestController < ActionController::Base
include Rails.application.routes.url_helpers
class BulkSendContract < Dry::Validation::Contract
params do
required(:send_params).array(:hash) do
required(:id).value(:integer)
required(:recipients).array(:string)
end
end
rule("send_params").each do
recipients = value[:recipients]
recipients.each do |recipient|
stripped_value = recipient.gsub(/[-()\s]/, '')
key.failure("Fax number '#{recipient}' has invalid format") unless stripped_value =~ /^\+?[0-9]{6,}$/
end
end
# rule("send_params.recipients").each do
# ::Kernel.binding.pry
# stripped_value = value.gsub(/[-()\s]/, '')
# key.failure("Fax number '#{recipient}' has invalid format") unless stripped_value =~ /^\+?[0-9]{6,}$/
# end
end
def create
input = BulkSendContract.new.call(params.to_unsafe_h)
return render json: { errors: input.errors.to_h } if input.errors.any?
render json: { status: "OK" }
end
end
require "minitest/autorun"
require "rack/test"
class BugTest < Minitest::Test
include Rack::Test::Methods
def test_returns_success
send_params = [{ id: 1, recipients: ["2", ["2", "1"]] }]
# send_params = [{ id: 1, recipients: ["2"] }]
post "fax", { send_params: send_params }.to_json, { "CONTENT_TYPE" => "application/json" }
expected = { "errors" => { "send_params" => { "0" => { "recipients" => ["must be a string"] } } } }
assert_equal expected, JSON.parse(last_response.body)
end
private
def app
Rails.application
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment