Skip to content

Instantly share code, notes, and snippets.

@jarosluv
Last active June 19, 2017 10:29
Show Gist options
  • Save jarosluv/947d6d30f85554681eb3d1ee9c8e22cd to your computer and use it in GitHub Desktop.
Save jarosluv/947d6d30f85554681eb3d1ee9c8e22cd to your computer and use it in GitHub Desktop.
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
gem "rake"
gem "hanami", github: "hanami"
gem "minitest"
end
require "minitest/autorun"
require "hanami/controller"
FooSchema = Dry::Validation.Schema do
configure do
def self.messages
super.merge(
{ en: { errors: { foo?: "WTF?" } } }
)
end
end
required(:title).filled
validate foo?: %i(title) do |title|
false
end
end
class CreateFoo
include Hanami::Action
params do
required(:foo).schema(FooSchema)
end
def call(params)
params.errors
halt 400 unless params.valid?
end
end
class BugTest < Minitest::Test
def test_controller_action
params = Hash[foo: { title: "OK" }]
action = CreateFoo.new
response = action.call(params)
assert_equal 400, response[0]
end
end
@flash-gordon
Copy link

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"

  gem "rake"
  gem "hanami", github: "hanami"
  gem "minitest"
end

require "minitest/autorun"
require "hanami/controller"

FooSchema = Dry::Validation.Schema do
  required(:title).filled

  validate foo?: %i(title) do |title|
    false
  end
end

class CreateFoo
  include Hanami::Action

  params do
    configure do
      config.messages_file = './errors.yml'
    end

    required(:foo).schema(FooSchema)
  end

  def call(params)
    params.errors
    halt 400 unless params.valid?
  end
end

class BugTest < Minitest::Test
  def test_controller_action
    params = Hash[foo: { title: "OK" }]
    action = CreateFoo.new
    response = action.call(params)

    assert_equal 400, response[0]
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment