Sha256: c0c36334f8c4e795f471ecc21ede6d9f06b8b7834300ab0122ab7f7e0e5dcb94

Contents?: true

Size: 1.19 KB

Versions: 18

Compression:

Stored size: 1.19 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.Form do
  configure do
    config.messages = :i18n
    config.type_specs = true
  end

  required(:email, :string).filled
  required(:age, :int).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

18 entries across 18 versions & 1 rubygems

Version Path
dry-validation-0.11.2 benchmarks/benchmark_form_valid.rb
dry-validation-0.11.1 benchmarks/benchmark_form_valid.rb
dry-validation-0.11.0 benchmarks/benchmark_form_valid.rb
dry-validation-0.10.7 benchmarks/benchmark_form_valid.rb
dry-validation-0.10.6 benchmarks/benchmark_form_valid.rb
dry-validation-0.10.5 benchmarks/benchmark_form_valid.rb
dry-validation-0.10.4 benchmarks/benchmark_form_valid.rb
dry-validation-0.10.3 benchmarks/benchmark_form_valid.rb
dry-validation-0.10.2 benchmarks/benchmark_form_valid.rb
dry-validation-0.10.1 benchmarks/benchmark_form_valid.rb
dry-validation-0.10.0 benchmarks/benchmark_form_valid.rb
dry-validation-0.9.5 benchmarks/benchmark_form_valid.rb
dry-validation-0.9.4 benchmarks/benchmark_form_valid.rb
dry-validation-0.9.3 benchmarks/benchmark_form_valid.rb
dry-validation-0.9.2 benchmarks/benchmark_form_valid.rb
dry-validation-0.9.1 benchmarks/benchmark_form_valid.rb
dry-validation-0.9.0 benchmarks/benchmark_form_valid.rb
dry-validation-0.8.0 benchmarks/benchmark_form_valid.rb