These are chat archives for dry-rb/chat

16th
Jan 2017
Jan Stevens
@JanStevens
Jan 16 2017 12:38
Is there any other way to define Dry::Validation.Schema in a ruby syntax way? I’m not really a fan of the assign to constant that looks like a class
class Devices
  extend Dry::Initializer::Mixin
  Schema = Dry::Validation.Schema do
    required(:status_update_interval_s).filled
  end
end
Also I’m trying to combine Dry Initializer (or struct) with Validation
Piotr Solnica
@solnic
Jan 16 2017 12:57
class Schema < Dry::Validation::Schema
  define! do
    required(:foo).filled
  end
end

my_schema = Schema.new
@JanStevens ^
Jan Stevens
@JanStevens
Jan 16 2017 12:57
Ha nice :0
and any guide / wiki on how to combine Dry-struct + dry validations? I was reading the Invalid object is an anti pattern and now I’m a bit puzzled where to place my validations (for example value should be larger then 20)
Piotr Solnica
@solnic
Jan 16 2017 12:58
@JanStevens combine in what way?
Jan Stevens
@JanStevens
Jan 16 2017 12:59
in the article custom types are created that behave as validation
Sergey Kukunin
@Kukunin
Jan 16 2017 12:59
@JanStevens dry-type validates struct by default, without dry-validation
Piotr Solnica
@solnic
Jan 16 2017 12:59
these should be treated like constraints, not validation
Sergey Kukunin
@Kukunin
Jan 16 2017 12:59
yep. good clarification. dry-structs has constraints
Jan Stevens
@JanStevens
Jan 16 2017 12:59
I get user input from a form, and want to validate the input matches the constraints
Sergey Kukunin
@Kukunin
Jan 16 2017 13:00
and there is separate validation schema for an input outside of applciation
Piotr Solnica
@solnic
Jan 16 2017 13:00
@JanStevens and then you want to build a struct from that input?
Jan Stevens
@JanStevens
Jan 16 2017 13:00
yes indeed
Piotr Solnica
@solnic
Jan 16 2017 13:01
yeah we have partial support for importing struct constraints into a dry-validation schema
Jan Stevens
@JanStevens
Jan 16 2017 13:01
Ha I see, so the contraints make sure the struct is valid object, the validations are more for different kind of inputs
Piotr Solnica
@solnic
Jan 16 2017 13:01
validation is different from constraints because it does not cause exceptions
Sergey Kukunin
@Kukunin
Jan 16 2017 13:01
@solnic what is partial support btw?
Piotr Solnica
@solnic
Jan 16 2017 13:01
@Kukunin it may or may not work :)
so it’s in experimental state
Jan Stevens
@JanStevens
Jan 16 2017 13:02
so the flow would be, Get user input -> Validate using schema -> initialize object -> work it
you can also just reuse types in both dry-v and dry-s
so ie Age = Types::Int.constrained(gt: 18) and use it in a validation schema like required(:age).filled(:int?, Age)
Jan Stevens
@JanStevens
Jan 16 2017 13:04
so your types should also deal with the defaults?
Piotr Solnica
@solnic
Jan 16 2017 13:05
ah for defaults you’d have to enable type_specs and do this:
uhm, ok nevermind, defaults don’t work yet
tbh I don’t even know if it’s a good idea to make them work in dry-v
they will work in structs though, so if you want to treat structs as an intermediate step to prepare data then you’ll get defaults
Jan Stevens
@JanStevens
Jan 16 2017 13:10
So the order would be different then
Initialize struct -> validate using schema?
Piotr Solnica
@solnic
Jan 16 2017 13:11
uhm no
schema would have to accept nils
Jan Stevens
@JanStevens
Jan 16 2017 13:11
I should ignore the default fields in my struct :D
Piotr Solnica
@solnic
Jan 16 2017 13:11
which is accurate
Jan Stevens
@JanStevens
Jan 16 2017 13:11
oke great thanks :)
Jan Stevens
@JanStevens
Jan 16 2017 13:42
hmm getting the following
> Devices.new({})
NoMethodError: undefined method 'to_ast' for #<Dry::Types::Default:0x007faa472bad38>
Did you mean?  to_s

module Types
  include Dry::Types.module
  StatusUpdateInterval = Types::Form::Int.default(90)
end

class Devices < Dry::Struct
  constructor_type :schema
  attribute :status_update_interval_s, Types::StatusUpdateInterval
end
Piotr Solnica
@solnic
Jan 16 2017 13:44
@JanStevens it must be a constrained type
Jan Stevens
@JanStevens
Jan 16 2017 13:47
Ha
The previous code does work, it’s the reusing in the schema that doenst :D
  class Schema < Dry::Validation::Schema::Form
    configure { config.messages = :i18n }
    define! do
      required(:status_update_interval_s).maybe(:int?, Types::StatusUpdateInterval)
    end
  end
Jan Stevens
@JanStevens
Jan 16 2017 13:57
And how do I create a constrained Form::Int type? I’m using StatusUpdateInterval = Form::Int.default(90).constrained(int: true) it seems to work allright
Piotr Solnica
@solnic
Jan 16 2017 14:37
Like that :)
Jan Stevens
@JanStevens
Jan 16 2017 14:37
awesome
Igor
@svilenkov
Jan 16 2017 14:49
Is it possible to easily duplicate a Dry::Initialize object
i want to replaced an initializer object in an array with a new one with some values changes
it would be possible with attr_writer on those params, and to mutate the existing object
but if I wanted to do it in an immutable way, is there an easy way to do with initializer?
Jan Stevens
@JanStevens
Jan 16 2017 16:01
I’m trying to do a nested Dry::Struct, the only problem is that the attribute can be of different possible Dry::Struct types
Example
class Cow < Dry::Struct
  attribute :sound, Types::Strict::String.default("MEEEU")
end

class Chicken < Dry::Struct
  attribute :food, Types::Strict::String.default("corn")
end

class Cage < Dry::Struct
  attribute :animal, Cow | Chicken
end
I cannot set Cage to have a chicken :D
Jan Stevens
@JanStevens
Jan 16 2017 16:07
One would expect that Cage.new(animal: Chicken.new) would work but animal is always Cow
Piotr Solnica
@solnic
Jan 16 2017 16:17
@JanStevens animals don’t like cages ¯_(⊙ʖ⊙)
Jan Stevens
@JanStevens
Jan 16 2017 16:17
LOL
Piotr Solnica
@solnic
Jan 16 2017 16:17
@JanStevens srsly though, pls report an issue, maybe it’s a bug
Jan Stevens
@JanStevens
Jan 16 2017 16:18
Ha dry-rb/dry-struct#24
My example post here does work, but my code not :/
Haaa with constructor_type :schema it doesn’t work :D
Chris Richards
@cmrichards
Jan 16 2017 17:18
Berg::Form looks strange...
Chris Richards
@cmrichards
Jan 16 2017 17:33
Shouldn't this validation be two different validations? EditValidation and NewValidation (or whatever)? There are required fields marked as optional.
https://github.com/icelab/berg/blob/master/apps/admin/lib/admin/posts/validation/form.rb
Piotr Solnica
@solnic
Jan 16 2017 18:18
@cmrichards it’s a simple bridge between formalist and dry-v IIRC
@cmrichards they could be separated
Jan Stevens
@JanStevens
Jan 16 2017 19:33
Is it possible to combine multiple Dry::Struct’s? Basically I’m going to define a couple of them and then I want to inhert multiple in a different dry struct.
I was thinking on merging the underlying schema (as it seem to contain the most important parts)
or can I easily extend `include Dry::Structs :D
Piotr Solnica
@solnic
Jan 16 2017 19:50
@JanStevens inheritance works but you can’t override attributes
Jan Stevens
@JanStevens
Jan 16 2017 20:03
I need to inherent from multiple classes 😀
Piotr Solnica
@solnic
Jan 16 2017 20:03
really?
Jan Stevens
@JanStevens
Jan 16 2017 20:06
Hmm yea my usecase is rather messed up. I basically need a kind of template attribute set that can be applied to multiple classes and can be combined depending on the provided params
Piotr Solnica
@solnic
Jan 16 2017 20:07
so just use a module and class_eval attributes in included hook
Jan Stevens
@JanStevens
Jan 16 2017 20:07
For example Animal -> Bird -> Eagle and Food -> Rats -> Eagle
Hmm that might work
And then I initialse eagle with a type 'bold' and it should have Food: mise