Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Piotr Solnica
@solnic
UserSchema = Dry::Validation.JSON do
  required(:created_at).value(:date_time?)
end

class User < Dry::Struct
  attribute :created_at, Types::Strict::DateTime
end

user_attrs = UserSchema.(user_data)

User.new(user_attrs)
in 1.0.0 dry-validation will support infering schemas from struct definitions, for now you need to have it defined explicitly in both places
Ivo Benedito
@ivobenedito
and that JSON validation enforces the coercion? cool
Screen Shot 2016-10-14 at 17.33.33.png
this is the service object where I'm trying to inject that Schema logic you're talking about
so that you understand the context a little better
Piotr Solnica
@solnic
@ivobenedito aah so the structure is different (camel-cases keys)
anyhow, yes it will enforce coercions
Ivo Benedito
@ivobenedito
yah ... camel-cased but I don't want to make the case switch automatically
Piotr Solnica
@solnic
why do you have a guard for an empty string? is it an allowed value?
Ivo Benedito
@ivobenedito
the external API is returning unexpected values in some cases
let's say ... users that never logged in, the last_sign_in_at datetime doesn't exist
don't even know if I just tell the API owners to returning an empty field or just skip the field on the response
Piotr Solnica
@solnic
yeah this is exactly why I recommend using schemas instead of just blindly passing input to domain structs
so, uhm, you can do all sorts of things to make it easier to work with potentially garbage-data
unfortunately JSON schemas don’t turn empty strings to nils
(Form schemas do)
I’ve been thinking about changing that, it’s not gonna be pedantically correct, but it’d be very pragmatic
if you need more control over input and coercions, you can turn on explicit type specs, and define custom types where you need them
it’s gonna be more verbose but you’d get full control
Piotr Solnica
@solnic
ie:
DateOrNil = Types::Strict::Date.optional.constructor { |v|
  v ==  '' ? nil : Types::Json::Date[v]
}

schema = Dry::Validation.JSON do
  configure { config.type_specs = true }

  required(:date, DateOrNil).maybe(:date?)
end

irb(main):038:0> schema.(date:  '')
=> #<Dry::Validation::Result output={:date=>nil} errors={}>
irb(main):039:0> schema.(date: '2010-10-12')
=> #<Dry::Validation::Result output={:date=>#<Date: 2010-10-12 ((2455482j,0s,0n),+0s,2299161j)>} errors={}>
Ivo Benedito
@ivobenedito
hmmm yes!
Ivo Benedito
@ivobenedito
what's the config.type_specs for?
Yurii Danyliak
@yuriidanyliak
to specify type for coercion explicitly, as far as I know
Piotr Solnica
@solnic
Yes. What Yurii said. It'll be default behaviour in 1.0.0
Nick Sutterer
@apotonick
@solnic i will be at RubyDay, too, that means we can hack! (or drink)
Piotr Solnica
@solnic
@apotonick w00000t
(We shall do both)
Nick Sutterer
@apotonick
i do agree!
Ivo Benedito
@ivobenedito
@solnic @yuriidanyliak thx guys!
Philip Arndt
@parndt
@apotonick and rubyconf AU 2017?
Nick Sutterer
@apotonick
@parndt not sure, darling!
how are you?????? i miss you!
Yurii Danyliak
@yuriidanyliak
hey @apotonick, can I use pass OpenStruct or smth similar to form object initializer in reform? Currently using OpenStruct instead of model object and getting undefined method 'model_name' for #<ContactInfoForm:0x007fb7d4e19018>
Nick Sutterer
@apotonick
@yuriidanyliak only if you use bullshit-activemodel extensions you get this error, i use OpenStruct with reform everywhere
for example, simple_form. don't use it
Maciej Mensfeld
@mensfeld
@apotonick told you few times that you shoukd stop using open source ;;)
hehehe
Nick Sutterer
@apotonick
i did stop using open source, i only use my own stuff and dry hahahahahaha
Maciej Mensfeld
@mensfeld
good morning btw everyone :)
hahha :D
open struct ;)
it is still 2 early in poland for me it seems :D
Nick Sutterer
@apotonick
early to bed, early to rise, makes a man healthy, and wealthy, and wise. --benjamin franklin
it's my life motto (unless i have to party)
Andrew Kozin
@nepalez

Hey, guys, what do you think about this candidate to dry family?

https://github.com/nepalez/dry-client

tbh, I'm wondering whether it dry enough ;) Though it deals with immutability, and heavily back on dry-initializer and dry-types, still it's sort of a "micro-framework", so I need a second opinion.

Philip Arndt
@parndt
@apotonick doing well, you?
Tim Riley
@timriley
@nepalez yeah, it definitely feels “framework-y”
Tim Riley
@timriley
And a little too high-level, at least from the synopsis. versus e.g. rom-rb which gives you access to build things up from lower-level pieces if you needed/wanted too