Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Sergey Kukunin
@Kukunin
yep. good clarification. dry-structs has constraints
Jan Stevens
@JanStevens
I get user input from a form, and want to validate the input matches the constraints
Sergey Kukunin
@Kukunin
and there is separate validation schema for an input outside of applciation
Piotr Solnica
@solnic
@JanStevens and then you want to build a struct from that input?
Jan Stevens
@JanStevens
yes indeed
Piotr Solnica
@solnic
yeah we have partial support for importing struct constraints into a dry-validation schema
Jan Stevens
@JanStevens
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
validation is different from constraints because it does not cause exceptions
Sergey Kukunin
@Kukunin
@solnic what is partial support btw?
Piotr Solnica
@solnic
@Kukunin it may or may not work :)
so it’s in experimental state
Jan Stevens
@JanStevens
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
so your types should also deal with the defaults?
Piotr Solnica
@solnic
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
So the order would be different then
Initialize struct -> validate using schema?
Piotr Solnica
@solnic
uhm no
schema would have to accept nils
Jan Stevens
@JanStevens
I should ignore the default fields in my struct :D
Piotr Solnica
@solnic
which is accurate
Jan Stevens
@JanStevens
oke great thanks :)
Jan Stevens
@JanStevens
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
@JanStevens it must be a constrained type
Jan Stevens
@JanStevens
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
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
Like that :)
Jan Stevens
@JanStevens
awesome
Igor
@svilenkov
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
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
One would expect that Cage.new(animal: Chicken.new) would work but animal is always Cow
Piotr Solnica
@solnic
@JanStevens animals don’t like cages ¯_(⊙ʖ⊙)
Jan Stevens
@JanStevens
LOL
Piotr Solnica
@solnic
@JanStevens srsly though, pls report an issue, maybe it’s a bug
Jan Stevens
@JanStevens
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