Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • May 12 21:46
    thevtm starred dry-rb/dry-monads
  • May 12 19:47
    silviogreuel starred dry-rb/dry-monads
  • May 12 16:17
    brookemckim starred dry-rb/dry-types
  • May 12 07:54
    solnic commented #422
  • May 11 18:57
  • May 11 18:50
    flash-gordon commented #422
  • May 11 18:48
    flash-gordon reopened #422
  • May 11 16:08
    BernardoMG starred dry-rb/dry-monads
  • May 11 13:01
    timriley commented #422
  • May 11 13:01
    timriley commented #422
  • May 11 08:52
    flash-gordon commented #422
  • May 11 07:08
    solnic commented #422
  • May 07 14:53
    flash-gordon commented #422
  • May 07 14:51
    solnic commented #422
  • May 07 14:44
    wuarmin closed #176
  • May 07 12:50
    flash-gordon closed #422
  • May 07 12:50
    flash-gordon commented #422
  • May 07 08:54
    sdalu labeled #422
  • May 07 08:54
    sdalu labeled #422
Ivo Benedito
@ivobenedito
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>
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)