These are chat archives for dry-rb/chat

4th
May 2016
Soumya Ray
@soumyaray
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(:name).required
  key(:age).maybe(:int?, gt?: 15, lt?: 150)
end;

validated_student = StudentSchema.call(params)

# 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
end

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

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

@student = Student.new(service_response)

# @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
@tak1n
May 04 2016 09:13
@jodosha awesome will take a look soon :)
Hannes Nevalainen
@kwando
May 04 2016 09:32
Does anyone know how to create a validator from a Dry::Types::Struct
?
Tim Riley
@timriley
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
@kwando
May 04 2016 09:54
@timriley yeah, exactly like that. Feels like it should be possible somehow
Pascal Betz
@pascalbetz
May 04 2016 09:58
It's probably also what @soumyaray might be interested?
Soumya Ray
@soumyaray
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
@jodosha
May 04 2016 11:31
@timriley Thanks for the reply :green_heart: I'll take a look soon :)
Hannes Nevalainen
@kwando
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
@cdennl
May 04 2016 12:20
what is the problem with activesupport?
Hannes Nevalainen
@kwando
May 04 2016 12:20
I dont wanna drag that dependency into my app =/
Oskar Szrajer
@gotar
May 04 2016 12:54
what is the problem with activesupport? good title for a book ;]
Luca Guidi
@jodosha
May 04 2016 16:41
@AMHOL ahahhahaha :D