Sha256: 631ebb7d5b2d5f99c9260211afdfb7aa0c450c202c352b905525763897d1ac4b

Contents?: true

Size: 1.2 KB

Versions: 8

Compression:

Stored size: 1.2 KB

Contents

require 'benchmark/ips'
require 'active_model'

require 'dry-validation'

I18n.locale = :en
I18n.backend.load_translations

class User
  include ActiveModel::Validations

  attr_reader :email, :age

  validates :email, :age, presence: true
  validates :age, numericality: { greater_than: 18 }

  def initialize(attrs)
    @email, @age = attrs.values_at('email', 'age')
  end
end

schema = Dry::Validation.Schema do
  configure do
    config.messages = :i18n
  end

  required(:email).filled
  required(:age).filled(:int?, gt?: 18)
end

form = Dry::Validation.Params do
  configure do
    config.messages = :i18n
    config.type_specs = true
  end

  required(:email, :string).filled
  required(:age, :integer).filled(:int?, gt?: 18)
end

params = { 'email' => 'foo@bar.baz', 'age' => '19' }
coerced = { email: 'foo@bar.baz', age: 19 }

puts schema.(coerced).inspect
puts form.(params).inspect
puts User.new(params).validate

Benchmark.ips do |x|
  x.report('ActiveModel::Validations') do
    user = User.new(params)
    user.validate
    user.errors
  end

  x.report('dry-validation / schema') do
    schema.(coerced).messages
  end

  x.report('dry-validation / form') do
    form.(params).messages
  end

  x.compare!
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
dry-validation-0.13.3 benchmarks/benchmark_form_valid.rb
dry-validation-0.13.2 benchmarks/benchmark_form_valid.rb
dry-validation-0.13.1 benchmarks/benchmark_form_valid.rb
dry-validation-0.12.3 benchmarks/benchmark_form_valid.rb
dry-validation-0.13.0 benchmarks/benchmark_form_valid.rb
dry-validation-0.12.2 benchmarks/benchmark_form_valid.rb
dry-validation-0.12.1 benchmarks/benchmark_form_valid.rb
dry-validation-0.12.0 benchmarks/benchmark_form_valid.rb