Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Tim Riley
@timriley
@ahmgeek not yet sorry, hopefully within the next month
Chris Richards
@cmrichards
Is there a good example app on github that uses dry
-rb?
Sergey Kukunin
@Kukunin
https://github.com/icelab/berg - canonical app =)
Chris Richards
@cmrichards
Thanks 😀
Does it use dry-validation / forms and all that jazz?
Piotr Solnica
@solnic
@cmrichards dry-validation. I'll be updating it soon to rom 3.0.0 but there won't be any form objects
Jan Stevens
@JanStevens
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
class Schema < Dry::Validation::Schema
  define! do
    required(:foo).filled
  end
end

my_schema = Schema.new
@JanStevens ^
Jan Stevens
@JanStevens
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
@JanStevens combine in what way?
Jan Stevens
@JanStevens
in the article custom types are created that behave as validation
Sergey Kukunin
@Kukunin
@JanStevens dry-type validates struct by default, without dry-validation
Piotr Solnica
@solnic
these should be treated like constraints, not validation
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