These are chat archives for dry-rb/chat

May 2016
Soumya Ray
May 04 2016 04:56

Greetings everyone! I'm playing with dry-rb and I'm still missing a few things. Here's a toy example:

I have a web application that takes posted form input from users and sends it for persistence to an outside service:

require 'dry-validation'

params = { 'name' => 'Adam Smith', 'age' => '22' }

StudentSchema = Dry::Validation.Form do
  key(:age).maybe(:int?, gt?: 15, lt?: 150)

validated_student =

# send validated_student.output to a service if no errors

Elsewhere, I am getting data from the outside service and creating a struct out of it that will go to a view:

require 'dry-types'

module Types
  include Dry::Types.module

class Student < Dry::Types::Struct
  attribute :id, Types::Int
  attribute :name, Types::String
  attribute :age, Types::Maybe::Strict::Int

service_response = { id: 124, name: 'Joe Shmo', age: nil }

@student =

# @student will be used by a view

I'm duplicating what a student is in the definition of the form validation and also the struct. What's a better way of doing these two things? Should I be defining all the attributes just once in the Types module?

Benjamin Klotz
May 04 2016 09:13
@jodosha awesome will take a look soon :)
Hannes Nevalainen
May 04 2016 09:32
Does anyone know how to create a validator from a Dry::Types::Struct
Tim Riley
May 04 2016 09:49
Like creating a dry validation schema from a struct's attributes schema?
I think @coop might've done something like that?
Hannes Nevalainen
May 04 2016 09:54
@timriley yeah, exactly like that. Feels like it should be possible somehow
Pascal Betz
May 04 2016 09:58
It's probably also what @soumyaray might be interested?
Soumya Ray
May 04 2016 10:03
@pascalbetz yes, that would be awesome. i kinda got into dry-rb thinking that was the direction i would go. that said, it could raise some strange usage. for example, a struct would presumably require an id if it reflects a persisted model, but during form validation we would expressly not want an id returned by the form. (@kwando : let me know if you stumble across what you're looking for!)
Luca Guidi
May 04 2016 11:31
@timriley Thanks for the reply :green_heart: I'll take a look soon :)
Hannes Nevalainen
May 04 2016 12:12
I've asked before, but I ask again: Do you know about a date/time math library in ruby?
The datetime math functionality i ActiveSupport is convenient but it is ActiveSupport xD
Christopher Dennl-Ortega Arrieta
May 04 2016 12:20
what is the problem with activesupport?
Hannes Nevalainen
May 04 2016 12:20
I dont wanna drag that dependency into my app =/
Oskar Szrajer
May 04 2016 12:54
what is the problem with activesupport? good title for a book ;]
Luca Guidi
May 04 2016 16:41
@AMHOL ahahhahaha :D