Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Ivo Benedito
@ivobenedito
& for example, the responsability of coercion for DateTime values should be done outside of the object itself?
just asking, because if I run something like: class User < Dry::Struct; attribute :created_at, Types::DateTime; end; User.new(created_at: "2016-10-06T10:03:03.785Z")
it won't coerce anything and will just put a string on the object state
Piotr Solnica
@solnic
@ivobenedito use dry-validation schema to process data before passing it to your structs
Ivo Benedito
@ivobenedito
@solnic I'm really interested in shifting to dry-rb, but I'm finding it to be a little hard to read and find docs / blog posts about how to connect all this sub-modules together
Piotr Solnica
@solnic
@ivobenedito just apply schema validation to api input, and pass result to struct constructor, there’s no need to “connect” anything
User.new(schema.call(some_data_from_api)) <= just this
of course validation can fail so you may want to handle this however you want
Ivo Benedito
@ivobenedito
yeah I've understood that, it's a great tip thx
but that forces the schema to be defined twice somehow, right?
on the struct, and on the schema definition
and the schema definition contains validation logic, also
and how will a DateTime coercion/parsing will be done on a Schema object :/
Piotr Solnica
@solnic
@ivobenedito use Dry::Validation.JSON and add type expections, like date_time? etc. it will infer coercions from that and try to apply them
I’d do something like this:
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>