Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Oskar Szrajer
@gotar
check berg repo too: https://github.com/icelab/berg
Vladimir Dralo
@vladra

Tbh I don't see how it's possible. Berg is tested using feature tests, they don't have my case there. Example:

require 'schema' 

class Service
  include App::Import['repositories.users']

  def call(params)
    Schema.(params)
    # ....
  end
end

RSpec.describe Service do
  let(:service) { described.class.new(users: users_repo) }
  let(:users_repo) { instance_double(Repositories::Users, count: 5 }
end

So I'm able to do this with Service, replacing repo with double. But I'm not able to replace repository of Schema from test. I can update my Service class to have Schema.with(users_repo: users).(params). But I thought maybe I can use validation via DI in Service class somehow

Vladimir Dralo
@vladra
and docs saying that using withrebuilds all the rules affecting performance, so I don't want this code to be in production to support tests :/
Tim Riley
@timriley
One idea: Once we work out how to make those schemas cached/singletons via dry-system's auto-registration, you could choose to have them registered in the container and then inject the schemas like other deps
Piotr Solnica
@solnic
@vladra just use with in tests and set repo to be a callable option ie option :repo, -> { ... } so that it does need to exist when you require the file with your schemas
Vladimir Dralo
@vladra

@solnic Not sure I understand what do you mean. Here is better example of what I'm trying to achieve

module Validations
  UserForm = Core::Validation.Form do
    configure do |config|
      option :users_repo, App::Container['repositories.users']
    end
    # ... validations
  end
end

require 'validations/user_form'
class CreateUser
  include App::Import[users_repo: 'repositories.users']

  def call(params)
    Validation::UserForm.(params)
    # ...
  end
end

RSpec.describe CreateUser do
  let(:service) { described_class.new(users_repo: users_repo) }
  let(:users_repo) { instance_double(Repositories::Users, count: 5 }

  let(:params) { ... }
  it 'foo bars' do
    result = service.(params)
    expect(result).to ....
  end
end

So I need somehow to double users repository in Validation::UserForm which is used in CreateUser#call method. And I don't see how can I do it without DI. I was thinking about wrapping schema in some method to be able to use it as DI for my service/operation -> Validation::UserForm.run.(params). Can't find other proper way to accomplish this.

updated above
Vladimir Dralo
@vladra
@timriley thats what I need, but based from your response it seems there are no way to do it now
@vladra ^
Piotr Solnica
@solnic
you could also define a schema class and include Singleton which is now supported by dry-system, so in runtime it would be created just once, and inject schemas with DI
Vladimir Dralo
@vladra
thanks for help, I will try it
Vladimir Dralo
@vladra
@solnic and can I use schemas via inheritance?
Piotr Solnica
@solnic
@vladra http://dry-rb.org/gems/dry-validation/basics/working-with-schemas/ see “Defining Base Schema Class”
Vladimir Dralo
@vladra
@solnic Ah, I see now - you propose to define dependecies/options in base schema class. But that will mean that my base class will include all repositories that needs to do uniq validations, right?
Piotr Solnica
@solnic
I don’t propose anything :) just showing how to use inheritance
Piotr Solnica
@solnic
both dry-validation and types passed 50k downloads on rubygems, that’s a lot given the age of these gems :)
Luca Guidi
@jodosha
:tada:
Oskar Szrajer
@gotar
:clap:
Sergey Kukunin
@Kukunin
@solnic don't you want to link to dry-rb on virtus readme page? considering there is no active development of virtus, and it's way more easier to find virtus comparing to dry-rb - it makes sense to point people to something better
Piotr Solnica
@solnic
@Kukunin yes I wanted to do that
there’s a new maintainer but there was no activity since April
Rafael George
@cored
do you guys know about a good article on manipulating the ast in Ruby ?
Piotr Solnica
@solnic
@cored what ast?
Maciej Mensfeld
@mensfeld

Hey @solnic I've updated to 0.10 (great job btw) and it brreaks when I have a subschema and it does not exist. Am I doing something wrong or is there an api change?

# define subschema:
required(:raw).schema(Raw)

and the spec:

    context 'when it is not valid' do
      before { input[:raw] = rand }

      it { expect { schema_result }.to raise_error(OffendingEngine::Errors::InvalidAttributes) }
    end

instead I get this:

  1) #<#<Class:0x00000003cb1af8>:0x00000003b6e420> #raw when it is not present should raise OffendingEngine::Errors::InvalidAttributes
     Failure/Error: it { expect { schema_result }.to raise_error(OffendingEngine::Errors::InvalidAttributes) }

       expected OffendingEngine::Errors::InvalidAttributes, got #<TypeError: no implicit conversion of Symbol into Integer> with backtrace:
         # /home/mencio/.rvm/gems/ruby-2.3.1@enjin/gems/dry-validation-0.10.3/lib/dry/validation/message_set.rb:111:in `[]'
         # /home/mencio/.rvm/gems/ruby-2.3.1@enjin/gems/dry-validation-0.10.3/lib/dry/validation/message_set.rb:111:in `block in initialize_placeholders!'
         # /home/mencio/.rvm/gems/ruby-2.3.1@enjin/gems/dry-validation-0.10.3/lib/dry/validation/message_set.rb:104:in `each'
         # /home/mencio/.rvm/gems/ruby-2.3.1@enjin/gems/dry-validation-0.10.3/lib/dry/validation/message_set.rb:104:in `reduce'
         # /home/mencio/.rvm/gems/ruby-2.3.1@enjin/gems/dry-validation-0.10.3/lib/dry/validation/message_set.rb:104:in `initialize_placeholders!'
         # /home/mencio/.rvm/gems/ruby-2.3.1@enjin/gems/dry-validation-0.10.3/lib/dry/validation/message_set.rb:25:in `initialize'
         # /home/mencio/.rvm/gems/ruby-2.3.1@enjin/gems/dry-validation-0.10.3/lib/dry/validation/message_set.rb:14:in `new'
         # /home/mencio/.rvm/gems/ruby-2.3.1@enjin/gems/dry-validation-0.10.3/lib/dry/validation/message_set.rb:14:in `[]'
         # /home/mencio/.rvm/gems/ruby-2.3.1@enjin/gems/dry-validation-0.10.3/lib/dry/validation/message_compiler.rb:39:in `call'
         # /home/mencio/.rvm/gems/ruby-2.3.1@enjin/gems/dry-validation-0.10.3/lib/dry/validation/result.rb:54:in `message_set'
         # /home/mencio/.rvm/gems/ruby-2.3.1@enjin/gems/dry-validation-0.10.3/lib/dry/validation/result.rb:42:in `messages'
         # ./lib/offending_engine/schemas/base.rb:30:in `call'
         # ./spec/lib/offending_engine/schemas/result_spec.rb:3:in `block (2 levels) in <top (required)>'
         # ./spec/lib/offending_engine/schemas/result_spec.rb:25:in `block (5 levels) in <top (required)>'
         # ./spec/lib/offending_engine/schemas/result_spec.rb:25:in `block (4 levels) in <top (required)>'
     # ./spec/lib/offending_engine/schemas/result_spec.rb:25:in `block (4 levels) in <top (required)>'
ah and the raw schema looks like that (but I think it is irrelevant):
# frozen_string_literal: true
module OffendingEngine
  module Schemas
    # Schema that is used to validate the raw validation result information that by default
    # should be equal or similar to the OffendingEngine::Shell execution result
    Raw = Dry::Validation.Schema(Base) do
      required(:stdout) { str? | array? | type?(Hash) }
      required(:stderr) { str? | array? | type?(Hash) }
      required(:exit_code) { int? & gteq?(0) & lteq?(255) }
    end
  end
end
Rafael George
@cored
@solnic manipulating the source code. I want to do some sort of anlysis
Piotr Solnica
@solnic
@mensfeld pls report an issue
Maciej Mensfeld
@mensfeld
@solnic ok
Maciej Mensfeld
@mensfeld
@solnic don't want to rush but any noews on that? dry-rb/dry-validation#266 ;) I want to be up 2 date
Egor Lynko
@flexoid
is it possible to have multiple high-level rules on the same field? (to have errors output by the same field)
I want to implement something like this:
schema = Dry::Validation.JSON do
  optional(:alarm).filled { bool? }
  optional(:alarm_in).maybe { int? }

  rule(alarm_in: [:alarm, :alarm_in]) do |alarm, alarm_in|
    alarm.eql?(true).then(alarm_in.filled?)
    alarm.eql?(false).then(alarm_in.none?)
  end
end

schema.call(alarm: true).messages
# => { alarm_in: ["must be filled"] }
schema.call(alarm: false).messages
# => {}
Egor Lynko
@flexoid
Found a solution by reading commits: looks like when rule is named with "?" in the end, the last dependency field will be used as message key, instead of rule name itself
So I can write:
rule(alarm_in_present?: [:alarm, :alarm_in]) do |alarm, alarm_in|
    alarm.eql?(true).then(alarm_in.filled?)
end
rule(alarm_in_absent?: [:alarm, :alarm_in]) do |alarm, alarm_in|
    alarm.eql?(false).then(alarm_in.none?)
end
Piotr Solnica
@solnic
@mensfeld i'm on holidays until Monday
Maciej Mensfeld
@mensfeld
@solnic enjoy :)
Sergey Kukunin
@Kukunin
do you know any name convention about methods that returns monad? We have bang ! for methods that raises exception - it's convenient. How is double bang sign for monads? find_customer!!(customer_id)
how other dynamic languages handle this?
Vladimir Dralo
@vladra

Hi all! When I'm providing configure block to specific Schema with some option set like this:

configure do
  option :test, -> { Test.new }
end

isn't it supposed to be lazily evaluated? Currently it is evaluated once file with schema definition is required (so as soon as schema is defined)

Nikita Shilnikov
@flash-gordon
@Kukunin usually monads are about statically typed languages so return type is always available. There is no naming convention in Ruby, also you can't have two bangs in a method name
Sergey Kukunin
@Kukunin
@flash-gordon yep, but we have dry-monads which is great. I want to use it globally in my project between components, but I predict that pain, when you don't know either method returns old-fashion value/nil or monad
@flash-gordon I didn't know about !! methods limitation =( ! and ? are occupied already =(
Tim Riley
@timriley
@vladra the configure block is run within the context of the schema’s class, so yes, it is run as soon as it is defined.
Adam Hawkins
@ahawkins
hello friends
what's the deal with dry-container. I don't see what role it's supposed to play. Isn't the same outcome achievable by using a struct like object?
Piotr Solnica
@solnic
@ahawkins it's a thread-safe registry with configurable resolvers, simple struct wouldn't cut it. This is used heavily by other gems, esp dry-system
Nick Sutterer
@apotonick
yo @AMHOL - say, dry-constructor is unreleased, right? isn't that being used in auto-inject?
@ahawkins :* also, dry-container can help you loading dependencies via resolvers. Trailblazer 1.2 supports it
@AMHOL the reason i'm asking is, i am thinking about making dry-constructor a dependency for trailblazer-operation